Bug 11355 – Copy through alias this with @disabled this(this)
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-10-25T12:19:00Z
Last change time
2015-02-18T03:38:12Z
Keywords
accepts-invalid, pull
Assigned to
nobody
Creator
m-ou.se
Comments
Comment #0 by m-ou.se — 2013-10-25T12:19:35Z
I'm trying to use a struct for RAII (like C++'s unique_ptr), so i
@disabled this(this). However, it's still getting copied somehow:
struct A {
int some_resource = 0;
~this() { if (some_resource) writeln("Freeing resource."); }
@disable this(this); // Prevent copying
}
struct B {
A a;
alias a this;
}
void main() {
B b;
b.some_resource = 100;
A a = move(b);
}
The above code prints 'Freeing resource.' twice!
See it in action on http://dpaste.dzfl.pl/6461df03
I guess the first one is from the temporary B that move(b) gives.
However, that temporary should be moved into a and then left in
B.init state, but that doesn't happen.
Comment #1 by nick — 2014-07-08T14:40:11Z
I think the move line should be disallowed:
A a = move(b); // implicit copy, currently allowed
A a = move(b).a; // currently disallowed by @disable this(this)
(The second line is equivalent, just expanding via alias this).
I think this test case may be the same bug:
struct A {
@disable this(this); // Prevent copying
}
struct B {
A a;
alias a this;
}
void main() {
A a = B(); // implicit copy of temporaryB.a should be disallowed
}