[Issue 19721] Cannot take address of scope local variable even with dip1000 if a member variable is a delegate
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sun Feb 9 21:38:10 UTC 2020
https://issues.dlang.org/show_bug.cgi?id=19721
ag0aep6g <ag0aep6g at gmail.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |ag0aep6g at gmail.com
--- Comment #1 from ag0aep6g <ag0aep6g at gmail.com> ---
As far as I understand, it's correct that the code is rejected. `scope` only
provides one level of protection. That means, you can't return a `scope`
pointer, but you can dereference it, make a copy of the pointee, and return
that.
In the example, `func` is allowed to return `*clientData`, regardless of what
happens anywhere else in the code. And since `s` is `scope`, `s.dg` might rely
on references to the stack. So to avoid a leak, you can't be allowed to call
`func` on `s`.
In code:
----
Struct g;
void main() @safe {
main2();
() { int[10] stompy = 13; } ();
g.dg(0); /* Prints "13". We've got memory corruption. */
}
void main2() @safe {
int stack;
scope s = Struct();
s.dg = (int x) @safe { import std.stdio; writeln(stack); };
() @trusted { func(&s); } (); /* Let's pretend this works in @safe. */
}
private struct Struct {
void delegate(int) @safe dg;
}
void func(scope Struct* clientData) @safe nothrow {
g = *clientData;
}
----
(In reply to Atila Neves from comment #0)
> It compiles if the `dg` member variable is removed or replaced with, say, an
> int.
An int doesn't have any indirections, unlike a delegate.
--
More information about the Digitalmars-d-bugs
mailing list