Bug 22145 – scope for foreach parameters is ignored
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-07-26T03:30:07Z
Last change time
2022-02-19T09:58:15Z
Keywords
pull, safe
Assigned to
No Owner
Creator
Mathias LANG
Comments
Comment #0 by pro.mathias.lang — 2021-07-26T03:30:07Z
Using HEAD as of July 26th (67132039c4c792d7722daeb9bef0a305e2f74fb1).
```
int* global;
struct Foo {
int opApply (scope int delegate (scope int* ptr) @safe dg) @safe
{
return 0;
}
}
void main () @safe
{
Foo f;
foreach (scope int* x; f) {
global = x;
}
}
```
Compiled with `-dip1000`, it should complain that the assignment violates `scope`, but currently we get:
```
foreach.d(13): Error: function `foreach.Foo.opApply(scope int delegate(scope int* ptr) @safe dg)` is not callable using argument types `(int delegate(int* x) nothrow @nogc @safe)`
foreach.d(13): cannot pass argument `__foreachbody2` of type `int delegate(int* x) nothrow @nogc @safe` to parameter `scope int delegate(scope int* ptr) @safe dg`
```
Comment #1 by bugzilla — 2022-02-17T08:15:40Z
It seems when the compiler creates the foreach delegate, it does not add in `scope` qualifications. This is likely because that part of the implementation is very, very old and has not been touched in a long time.
Comment #2 by dlang-bot — 2022-02-19T08:55:21Z
@WalterBright created dlang/dmd pull request #13692 "fix Issue 22145 - scope for foreach parameters is ignored" fixing this issue:
- fix Issue 22145 - scope for foreach parameters is ignored
https://github.com/dlang/dmd/pull/13692
Comment #3 by dlang-bot — 2022-02-19T09:58:15Z
dlang/dmd pull request #13692 "fix Issue 22145 - scope for foreach parameters is ignored" was merged into master:
- fad668f5cf2a39bfe9355b051226c2fad1cd17ca by Walter Bright:
fix Issue 22145 - scope for foreach parameters is ignored
https://github.com/dlang/dmd/pull/13692