Bug 22040 – dip1000: foreach ref can escape scope array

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-06-18T18:13:08Z
Last change time
2024-07-19T13:17:19Z
Keywords
safe, Vision
Assigned to
No Owner
Creator
Dennis
See also
https://issues.dlang.org/show_bug.cgi?id=21868

Comments

Comment #0 by dkorpel — 2021-06-18T18:13:08Z
This is correctly rejected: ``` ref int f(ref scope int[] arr) { foreach(ref e; arr) return e; // Error: scope variable `__r2` may not be returned assert(0); } ``` This isn't: (`scope` => `return scope`) ``` ref int f(ref return scope int[] arr) { foreach(ref e; arr) return e; assert(0); } ``` The `return` applies to the `ref arr`, not the `int[]`. This is another case of the compiler conflating `return ref` and `return scope` like in issue 21868.
Comment #1 by dkorpel — 2024-07-19T13:17:19Z
With the new rules, `return scope` now applies to the `int[]` instead of the `ref arr`.