Bug 2127 – inliner turns struct "return *this" from by-value into by-ref

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
All
Creation time
2008-05-24T06:08:00Z
Last change time
2014-02-24T15:33:26Z
Keywords
patch, wrong-code
Assigned to
nobody
Creator
fvbommel
Blocks
3761

Attachments

IDFilenameSummaryContent-TypeSize
642bug2127.difftentative fix for this for the d2 branch (might work on d1 as well, untested)text/plain1562
643bug2127.diffd2 fix, version 2text/plain1527

Comments

Comment #0 by fvbommel — 2008-05-24T06:08:16Z
The following code: ----- module evbug; import std.stdio; struct S { int last = 0; S opCall(int i) { writefln("%s %s", last, i); last = i; return *this; } } void main() { S t; t(1)(2); t(3); } ----- should output ===== 0 1 1 2 1 3 ===== (Note that opCall returns a copy of the original struct, so the first and last calls will be passed &t as 'this', but the second is passed a pointer to a copy) With "dmd -run test.d", this all works fine. However, when run with "dmd -inline -run test.d": ===== 0 1 1 2 2 3 ===== (note the last line) The "return *this" seems suddenly to have compiled as a reference return instead of a value return. (This can be verified by making opCall print the address as well; the uninlined version will only report the same address for the first and last call, while the inlined version reports the same address every time) GDC doesn't exhibit this problem, presumably because it uses the GCC inliner.
Comment #1 by braddr — 2010-05-25T01:59:48Z
Created attachment 642 tentative fix for this for the d2 branch (might work on d1 as well, untested) This bug exists in the d2 code base as well. This patch fixes the test case and passes the test suite.
Comment #2 by braddr — 2010-05-25T03:05:16Z
Created attachment 643 d2 fix, version 2 Oops.. the version I attached previously was an older copy. This is the newer version that moves the changes down from FuncDeclaration::inlineScan to doInline and fixes a problem with the first version.
Comment #3 by bugzilla — 2010-05-30T11:03:31Z