Bug 18088 – wrong lifetime evaluation of pointers or this-pointers in a struct with @safe
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2017-12-16T18:16:20Z
Last change time
2017-12-16T20:10:55Z
Assigned to
No Owner
Creator
Johannes Nordhoff
Comments
Comment #0 by mephisto — 2017-12-16T18:16:20Z
file: app.d
~~~~~~~~~~~~~~~~~~~~~
void main() {
A a = A( true);
}
@safe
struct A {
bool *lala;
this( bool i) {
lala = &aVar;
}
bool aVar = true;
}
~~~~~~~~~~~~~~~~~~~~~
$ dmd --version
DMD64 D Compiler v2.077.1
...
$
$ dmd app.d
app.d(13): Error: address of variable this assigned to this with longer lifetime
$
I would say the lifetime of "this" is equal to the lifetime of "this", but not longer
Comment #1 by ketmar — 2017-12-16T19:04:37Z
i'd say that dmd is absolutely right here. remember that struct can be *moved* by the compiler at any time, and stored `&aVar` will point to random memory after that.
so while compiler is correctly forbidding that construct in @safe code, the error message is really misleading, and doesn't help at all. i think that it's better to fix bug description to something like "misleading error message", or alike.
Comment #2 by mephisto — 2017-12-16T19:17:10Z
I gave a bad example.
file: app2.d
~~~~~~~~~~~~~~~~~~~~~
void main() {
A a = A( true);
}
@safe
struct A {
bool delegate() lala;
this( bool i) {
lala = &aFunc;
}
bool aFunc() {
return true;
}
}
~~~~~~~~~~~~~~~~~~~~~
$
$ dmd app2.d
app2.d(12): Error: address of variable this assigned to this with longer lifetime
$
so, I thought the pointer to a function shouldnt change when copying the struct.
Comment #3 by ketmar — 2017-12-16T19:25:51Z
still the same thing: delegate holds the hidden pointer to `this` (aka pointer to struct), and struct can be moved.
Comment #4 by mephisto — 2017-12-16T19:44:41Z
okay, I was misleaded. My actual ambition was to point to functions of structs (since I believe they are static). So, it is another issue
Comment #5 by ketmar — 2017-12-16T19:51:19Z
still, error message should be better, 'cause it explains nothing now. ;-) would you, please, fix bug title? or, maybe, close this bug and open a new one, i don't know what will be better.