Thread overview
[Issue 2943] New: Struct copying in presence of alias member this only copies alias this member
May 05, 2009
d-bugmail
Jul 05, 2009
David Simcha
Aug 30, 2010
David Simcha
Sep 05, 2010
Serg Kovrov
Sep 05, 2010
David Simcha
Oct 06, 2010
Don
Oct 08, 2010
Walter Bright
May 05, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2943

           Summary: Struct copying in presence of alias member this only
                    copies alias this member
           Product: D
           Version: 2.029
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: critical
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: dsimcha@yahoo.com


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
}


-- 

July 05, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=2943


David Simcha <dsimcha@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kovrov+puremagic@gmail.com




--- Comment #1 from David Simcha <dsimcha@yahoo.com>  2009-07-05 07:32:52 PDT ---
*** Issue 3135 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 30, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=2943


David Simcha <dsimcha@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kieron_brown@hotmail.com


--- Comment #2 from David Simcha <dsimcha@yahoo.com> 2010-08-30 15:16:14 PDT ---
*** Issue 4770 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 31, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=2943


Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy@yahoo.com


--- Comment #3 from Steven Schveighoffer <schveiguy@yahoo.com> 2010-08-31 05:17:50 PDT ---
upvoted, I can't believe this is been a bug for over a year, doesn't anyone use alias this?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 05, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=2943



--- Comment #4 from Serg Kovrov <kovrov+puremagic@gmail.com> 2010-09-05 09:16:32 PDT ---
I do. As stated in comment for Bug 3135#c1 - an empty postblit function seem to workaround the issue..

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 05, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=2943



--- Comment #5 from David Simcha <dsimcha@yahoo.com> 2010-09-05 09:52:19 PDT ---
(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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 06, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=2943


Don <clugdbug@yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                 CC|                            |clugdbug@yahoo.com.au


--- Comment #6 from Don <clugdbug@yahoo.com.au> 2010-10-06 14:39:59 PDT ---
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);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 08, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=2943


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #7 from Walter Bright <bugzilla@digitalmars.com> 2010-10-08 13:38:10 PDT ---
http://www.dsource.org/projects/dmd/changeset/709

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------