Bug 2943 – Struct copying in presence of alias member this only copies alias this member
Status
RESOLVED
Resolution
FIXED
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2009-05-05T18:41:00Z
Last change time
2015-06-09T01:26:25Z
Keywords
patch, wrong-code
Assigned to
nobody
Creator
dsimcha
Comments
Comment #0 by dsimcha — 2009-05-05T18:41:06Z
When a struct has a member that is alias this'd and that struct is assigned the value of another struct of the same type, only the member of the struct that is alias this'd is copied. Apparently, D tries conversion via alias this *before* assigning as the full struct.
Marking this as critical because it can lead to extremely subtle, hard to find bugs in user code with absolutely no warning.
import std.stdio;
struct Foo {
int a;
int b;
alias b this;
}
void main() {
Foo foo, foo2;
foo.a = 1;
foo.b = 2;
foo2.a = 3;
foo2.b = 4;
writeln(foo2.a, "\t", foo2.b); // 3 4
foo2 = foo;
writeln(foo2.a, "\t", foo2.b); // 3 2
}
Comment #1 by dsimcha — 2009-07-05T07:32:52Z
*** Issue 3135 has been marked as a duplicate of this issue. ***
Comment #2 by dsimcha — 2010-08-30T15:16:14Z
*** Issue 4770 has been marked as a duplicate of this issue. ***
Comment #3 by schveiguy — 2010-08-31T05:17:50Z
upvoted, I can't believe this is been a bug for over a year, doesn't anyone use alias this?
Comment #4 by kovrov+puremagic — 2010-09-05T09:16:32Z
I do. As stated in comment for Bug 3135#c1 - an empty postblit function seem to workaround the issue..
Comment #5 by dsimcha — 2010-09-05T09:52:19Z
(In reply to comment #3)
> doesn't anyone use alias this?
I mostly don't, but only b/c it's currently so buggy it's not even funny. I just looked, I myself have filed at least 5 bug reports on it, 4 of which I filed within a few days after the first version of DMD with alias this came out.
IMHO the next big todo after 64 support is to tackle the general extreme bugginess of alias this and inout, as well as the ref issue w/ opApply (Bug 2443). These are key features for library writers. Their bugginess severely limits their usability, and right now a major criticism of D is lack of libraries, so supporting library writers is kind of important.
Comment #6 by clugdbug — 2010-10-06T14:39:59Z
Very simple. This is a special case where alias this should be ignored.
PATCH: opover.c, BinExp::op_overload(), line 684 and 698. Also fixes bug 4641.
#if DMDV2
// Try alias this on first operand
- if (ad1 && ad1->aliasthis)
+ if (ad1 && ad1->aliasthis && !(op == TOKassign && ad2 && ad1 == ad2))
{
/* Rewrite (e1 op e2) as:
* (e1.aliasthis op e2)
*/
// Try alias this on second operand
- if (ad2 && ad2->aliasthis)
+ if (ad2 && ad2->aliasthis && !(op == TOKassign && ad1 && ad1 == ad2))
{
/* Rewrite (e1 op e2) as:
* (e1 op e2.aliasthis)
*/
============
TEST FOR TEST SUITE
struct Foo2943 {
int a;
int b;
alias b this;
}
void main() {
Foo2943 foo, foo2;
foo.a = 1;
foo.b = 2;
foo2.a = 3;
foo2.b = 4;
foo2 = foo;
assert(foo2.a == foo.a);
}