Bug 10842 – Some integer casts wrongly remove side-effect of the operand.

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-08-17T18:28:00Z
Last change time
2013-10-08T20:55:44Z
Keywords
pull, wrong-code
Assigned to
nobody
Creator
thelastmammoth

Comments

Comment #0 by thelastmammoth — 2013-08-17T18:28:51Z
dmd -version=A -run main.d: prints nothing dmd -version=B -run main.d: prints ok ---- auto fail(){ import std.stdio; writeln("ok"); /+ //would be same with this: assert(0); //or this: import std.exception; throw new Exception("bad"); +/ return null; } string foo(){ version(A){ auto temp=fail(); return temp; } else return fail(); } void main(){ foo(); }
Comment #1 by andrej.mitrovich — 2013-08-17T18:40:12Z
Reduced: ----- auto ret1() { assert(0); // should throw, doesn't return null; } string test1() { return ret1(); } void main() { test1(); } ----- If you change the return to an explicit expression, e.g. `return ""`, then the assert is triggered. Also perhaps noteworthy is that in older releases the reduced code used to fail to compile, e.g. in 2.057: Error: e2ir: cannot cast ret1() of type typeof(null) to type string
Comment #2 by thelastmammoth — 2013-08-17T18:58:29Z
> If you change the return to an explicit expression, e.g. `return ""`, then the > assert is triggered. In my case I want to make it work with arbitrary types, not just strings, do you have a suggestion for this? ( I just posted a suggestion here: http://forum.dlang.org/thread/[email protected] "how to get enclosing function as symbol ? (eg: __function__.stringof ==__FUNCTION__)" which would allow this among other things. )
Comment #3 by andrej.mitrovich — 2013-08-17T19:09:05Z
(In reply to comment #2) > Reduced: Actually the bug is unrelated to auto, but instead related to typeof(null): ----- typeof(null) ret1() { assert(0); // not thrown return null; } string test1() { return ret1(); } void main() { test1(); } -----
Comment #4 by k.hara.pg — 2013-09-28T23:12:47Z
https://github.com/D-Programming-Language/dmd/pull/2597 The problem is in CastExp::toElem, it sometimes remove the side effect of the cast operand.
Comment #5 by github-bugzilla — 2013-10-08T20:55:26Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/7123433d77bde06769d9227df8f5b3814117b1e6 fix Issue 10842 - Some integer casts wrongly remove side-effect of the operand https://github.com/D-Programming-Language/dmd/commit/175c85bc5658aae266c061d2c3c42a0607325676 Merge pull request #2597 from 9rnsr/fix10842 Issue 10842 - Some integer casts wrongly remove side-effect of the operand.