D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2943 - Struct copying in presence of alias member this only copies alias this member
Summary: Struct copying in presence of alias member this only copies alias this member
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 critical
Assignee: No Owner
URL:
Keywords: patch, wrong-code
: 3135 4770 (view as issue list)
Depends on:
Blocks:
 
Reported: 2009-05-05 18:41 UTC by David Simcha
Modified: 2015-06-09 01:26 UTC (History)
5 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description David Simcha 2009-05-05 18:41:06 UTC
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 David Simcha 2009-07-05 07:32:52 UTC
*** Issue 3135 has been marked as a duplicate of this issue. ***
Comment 2 David Simcha 2010-08-30 15:16:14 UTC
*** Issue 4770 has been marked as a duplicate of this issue. ***
Comment 3 Steven Schveighoffer 2010-08-31 05:17:50 UTC
upvoted, I can't believe this is been a bug for over a year, doesn't anyone use alias this?
Comment 4 Serg Kovrov 2010-09-05 09:16:32 UTC
I do. As stated in comment for Bug 3135#c1 - an empty postblit function seem to workaround the issue..
Comment 5 David Simcha 2010-09-05 09:52:19 UTC
(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 Don 2010-10-06 14:39:59 UTC
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);
}
Comment 7 Walter Bright 2010-10-08 13:38:10 UTC
http://www.dsource.org/projects/dmd/changeset/709