Bug 6962 – Wrong Code With Scope Exit and Array Parameter, only with -O

Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-11-16T18:12:00Z
Last change time
2015-06-09T05:11:52Z
Keywords
wrong-code
Assigned to
nobody
Creator
dsimcha
Blocks
6955

Comments

Comment #0 by dsimcha — 2011-11-16T18:12:10Z
The following code was reduced from std.conv. It prints "BAD!!!" only with -O enabled. Since parse() gets rid of the entire contents of v, v.length should be zero upon exiting toImpl and nothing should be printed. import core.stdc.stdio; T toImpl(T)(immutable string value) { string v = value; scope(exit) { if (v.length) { printf("BAD!!!"); } } return parse!T(v); } T parse(T)(ref string value) { value = value[0..0]; return 666; } void main() { immutable s = "42"; toImpl!float(s); }
Comment #1 by clugdbug — 2012-11-15T05:29:59Z
Marginally reduced test case: int bug6962(string value) { string v = value; scope(exit) assert(!v.length); ref6962(v); return 1; } void ref6962(ref string value) { value = value[0..0]; } void main() { string s = "42"; bug6962(s); }
Comment #2 by clugdbug — 2012-11-15T23:39:03Z
Further reduced. The 'ref' is not necessary. The return statement is required, otherwise the try-finally gets removed in the semantic pass. void bug6962(string value) { string v = value; try { v = v[0LU..0LU]; return; } finally { assert(!v.length); } } void main() { bug6962("42"); } --------- When compiled with -O, the assert is compiled incorrectly. Instead of taking v.length, it uses value.length. Bonus: on Linux 64, running obj2asm on the object file (with or without -O) causes obj2asm to segfault.
Comment #3 by bugzilla — 2013-01-30T23:23:10Z
Comment #4 by github-bugzilla — 2013-01-31T00:31:50Z
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/e67581883885baeb2882b72dac8b577444d03dfb fix Issue 6962 - Wrong Code With Scope Exit and Array Parameter, only with -O
Comment #5 by github-bugzilla — 2013-01-31T01:03:24Z
Commits pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/11b76a4aff5401b2928185a69789661ee4712907 fix Issue 6962 - Wrong Code With Scope Exit and Array Parameter, only with -O https://github.com/D-Programming-Language/dmd/commit/d617999ef0b36e8ceea5d789b86bb5c20462dd29 Merge pull request #1586 from WalterBright/b46 fix Issue 6962 - Wrong Code With Scope Exit and Array Parameter, only wi...