Bug 16454 – Return in the body of a foreach in a constructor backed by opApply corrupts the object

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-08-31T17:45:38Z
Last change time
2024-12-13T18:49:55Z
Keywords
wrong-code
Assigned to
No Owner
Creator
André Puel
Moved to GitHub: dmd#19184 →

Comments

Comment #0 by andrepuel — 2016-08-31T17:45:38Z
import std.stdio; struct OpApply { int opApply(int delegate(int) cb) { return cb(42); } } struct Bolinha { int a; this(ref OpApply moviadao) { foreach(int b; moviadao) { this.a = b; return; } } }; void main() { import std.stdio; OpApply range; writeln(Bolinha(range).a); } The expected print result was 42. But I get a random memory garbage instead. The following minor modification in main will 'fix' the issue: void main() { import std.stdio; OpApply range; Bolinha littleBall = Bolinha(range); writeln(littleBall.a); }
Comment #1 by ag0aep6g — 2016-08-31T18:14:51Z
Slightly reduced: ---- struct OpApply { int opApply(int delegate(int) cb) { return 0; } } struct Bolinha { int a = 0; this(int dummy) { OpApply moviadao; foreach(int b; moviadao) return; } } void main() { import std.stdio; writeln(Bolinha(0).a); } ---- Also fails on Linux and Windows (wine).
Comment #2 by moonlightsentinel — 2021-10-26T13:19:53Z
Works reliably since 2.094.1 according to run.dlang.io / local tests
Comment #3 by robert.schadek — 2024-12-13T18:49:55Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19184 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB