Bug 2483 – DMD allows assignment to a scope variable
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2008-12-01T12:58:00Z
Last change time
2015-06-09T05:11:45Z
Keywords
accepts-invalid, patch
Assigned to
yebblies
Creator
snake.scaly
Comments
Comment #0 by snake.scaly — 2008-12-01T12:58:01Z
The specs at http://www.digitalmars.com/d/1.0/attribute.html#scope say: "Assignment to a scope, other than initialization, is not
allowed." Nevertheless, the following code compiles:
void main()
{
scope Object foo;
foo = new Object;
}
Tested this in DMD 1.037, DMD 2.021, 2.019 and 1.033.
Comment #1 by verylonglogin.reg — 2012-01-22T06:10:28Z
This bug leads to not calling destructor for the stack object and deleting the last heap object instead:
---
import std.stdio;
scope class C {
int n;
this(int n) { writefln(" this(%s) at %s", this.n = n, cast(void*)this); }
~this() { writefln("~this(%s) at %s", n, cast(void*)this); }
}
void main() {
int i;
writefln("Stack is at %s", &i);
writefln("Heap is at %s", (new void[1]).ptr);
{
scope C c = new C(1); // at stack, never destroyed
c = new C(2); // at heap, destroyed on collect
c = new C(3); // ditto
c = new C(4); // at heap, destroyed on scope exit
}
writeln("after scope");
}
---
Comment #2 by verylonglogin.reg — 2012-01-22T06:22:40Z
*** Issue 4214 has been marked as a duplicate of this issue. ***