Created attachment 1595
Reproduces the undefined behavior
This code compiles and produces Undefined Behavior:
@safe:
import std.stdio;
struct T { int y; }
auto foo() {
int *x;
T t;
t.y = 12345;
x = &t.y;
return x;
}
unittest {
auto x = foo();
writeln("Hello world");
assert(*x == 12345);
}
It seems that the escape analysis checks if the pointed element is itself directly declared on the stack, instead of checking whether it is contained in something that is declared on the stack.
Comment #1 by eyal.lotem — 2016-05-06T07:53:56Z
Please ignore the attachment, it is a less simplified version of the reproduction.
Comment #2 by eyal.lotem — 2016-05-06T07:56:11Z
Created attachment 1596
An even simpler reproduction of the bug
Comment #3 by bugzilla — 2016-06-07T07:32:58Z
The attachment:
---------------------------
safe:
import std.stdio;
struct T { int y; }
auto foo() {
auto t = T(12345);
auto x = &t.y;
return x;
}
unittest {
auto x = foo();
writeln("Hello world");
assert(*x == 12345);
}
Comment #4 by bugzilla — 2016-08-25T07:14:31Z
(In reply to Walter Bright from comment #3)
> The attachment:
> ---------------------------
>
> safe:
>
> import std.stdio;
>
> struct T { int y; }
>
> auto foo() {
> auto t = T(12345);
> auto x = &t.y;
> return x; // line 10
> }
>
> unittest {
> auto x = foo();
> writeln("Hello world");
> assert(*x == 12345);
> }
With:
https://github.com/dlang/dmd/pull/5972
now yields:
test3.d(10): Error: scope variable x may not be returned
so this is resolved once 5972 is pulled.