Bug 4214 – Rebinding of scoped class references

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-05-20T13:25:00Z
Last change time
2015-06-09T05:13:50Z
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2010-05-20T13:25:35Z
This is a D2 program: import std.stdio: printf; class Foo { int x; this(int xx) { this.x = xx; } ~this() { printf("Foo(%d) destructor\n", this.x); } } void main() { scope Foo f1 = new Foo(1); Foo f2 = new Foo(2); f1 = f2; } With DMD v2.046 it compiles and at runtime it prints just: Foo(2) destructor If the Foo(1) object was meant to be deallocated at the end of the scope of main(), this program can cause problems. I can see two solutions: 1) Disallow the overwriting (rebinding) of references of scoped objects. This can be acceptable. 2) The right semantics of the code I have written there seems that both F(1) and F(2) objects have to be allocated with main() ends. So the compiler can look if inside main() the f1 is written over. If it's never written over in main(), then it can act normally. Otherwise at runtime at the end of the scope of main() the runtime can look at the f1 reference, if it refers to the object actually allocated on the stack, then it can act normally. If it points elsewhere then the runtime can call the destructor of both the object on the stack and the object referenced. I am not sure if this can be done.
Comment #1 by bearophile_hugs — 2010-05-20T16:59:15Z
Sorry, I meant: 2) The right semantics of the code I have written there seems to be: both F(1) and F(2) objects need to be deallocated when main() ends.
Comment #2 by verylonglogin.reg — 2012-01-22T06:22:40Z
http://d-p-l.org/attribute.html#scope "Assignment to a scope, other than initialization, is not allowed." *** This issue has been marked as a duplicate of issue 2483 ***