Bug 23751 – Returning by ref from opApply fools DIP1000

Status
NEW
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2023-02-27T23:55:03Z
Last change time
2024-12-13T19:27:29Z
Keywords
accepts-invalid, safe
Assigned to
No Owner
Creator
timon.gehr
See also
https://issues.dlang.org/show_bug.cgi?id=23739
Moved to GitHub: dmd#18153 →

Comments

Comment #0 by timon.gehr — 2023-02-27T23:55:03Z
DMD 2.102.1: Test case 1: --- struct S{ int x; int opApply(scope int delegate(ref int x)@safe @nogc dg)@safe @nogc{ return dg(x); } } ref int foo(ref S s)@safe @nogc{ foreach(ref x;s) return x; assert(0); } int *escapeStack()@safe{ S s; return &foo(s); } void main()@safe{ import std.stdio; auto p=escapeStack(); writeln(*p); // 0 writeln("overwrite stack"); writeln(*p); // garbage } --- Test case 2: --- struct S{ int x; int opApply(scope int delegate(ref int x)@safe @nogc dg)@safe @nogc{ return dg(x); } } ref int foo(ref S s)@safe @nogc{ foreach(x;s) return x; assert(0); } int *escapeStack()@safe{ S s; return &foo(s); } void overwriteStack(int depth)@safe{ auto s=S(2); import std.stdio; if(depth==0) writeln("overwrite stack: ",s); else overwriteStack(depth-1); } void main()@safe{ import std.stdio; auto p=escapeStack(); writeln(*p); // 0 overwriteStack(10); writeln(*p); // garbage } --- (Partial reboot of Issue 23739 with proper test cases.)
Comment #1 by razvan.nitu1305 — 2023-02-28T10:18:07Z
Nice try, Timon. ;)
Comment #2 by robert.schadek — 2024-12-13T19:27:29Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18153 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB