Bug 22309 – Taking the address of a stack variable struct with this is wrongly seen as @safe
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-09-15T11:24:10Z
Last change time
2022-03-28T12:53:11Z
Keywords
pull
Assigned to
No Owner
Creator
João Lourenço
Comments
Comment #0 by jlourenco5691 — 2021-09-15T11:24:10Z
struct Foo
{
Bar bar() @safe { return Bar(&this); }
}
struct Bar
{
this(Foo* foo) @safe {}
}
Bar unsafeUnderSafe() @safe
{
Foo foo;
return foo.bar;
}
Bar unsafe() @safe
{
Foo foo;
return Bar(&foo);
}
void main() @safe
{
auto b1 = unsafeUnderSafe();
auto b2 = unsafe();
}
Comment #1 by dlang-bot — 2021-09-15T12:30:40Z
@iK4tsu created dlang/dmd pull request #13074 "Fix Issue 22309 - Taking the address of a stack variable struct with this is wrongly seen as @safe" fixing this issue:
- dmd.expressionsem: fix taking address of this in safe code
FIX ISSUE #22309
Signed-off-by: João Lourenço <[email protected]>
https://github.com/dlang/dmd/pull/13074
Comment #2 by dkorpel — 2022-03-28T12:53:11Z
As mentioned in the Pull Request discussion, this has to be covered by dip1000. As of https://github.com/dlang/dmd/pull/13672, the code is rejected with -preview=dip1000:
```
test_.d(4): Error: reference to local variable `this` assigned to non-scope parameter `foo` calling test_.Bar.this
test_.d(21): Error: reference to local variable `foo` assigned to non-scope parameter `foo` calling test_.Bar.this
```