Testcase:
```
struct S {
this(ref int i) {
DBG(&i);
}
}
void DBG(lazy scope int* args) {}
```
Compilation with dlang2.101 gives the error:
<source>(3): Deprecation: escaping reference to outer local variable `i`
Removing `lazy` makes the code compile.
Note that this is very similar but distinct from https://issues.dlang.org/show_bug.cgi?id=21290 , because that regression already appeared from 2.073.
Comment #1 by razvan.nitu1305 — 2023-02-03T10:53:20Z
The compiler essentially rewrites the code to:
```
struct S
{
this(ref int i)
{
DBG(() => &i);
}
}
void DBG(scope int* delegate() args) {}
```
So it essentially attaches scope to args no to the return type of args (as far as I understand it doesn't make sense to attach scope on a return type), therefore the scoping information is lost and the compiler is conservative and doesn't let you do that.
I guess the compiler should first perform escape analysis before lowering i to a delegate.
Comment #2 by razvan.nitu1305 — 2023-02-03T12:18:47Z
Actually, it turns out you can escape i:
struct S {
this(ref int i) {
DBG(&i);
}
}
int* delegate() outer;
void DBG(lazy scope int* args)
{
outer = &args;
}
void main()
{
int b;
S s = S(b);
import std.stdio;
writeln(*outer());
}
It seems that scope is not propagated on the extracted delegate.
Comment #3 by robert.schadek — 2024-12-13T19:27:06Z