www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 2943] New: Struct copying in presence of alias member this only copies alias this member

reply d-bugmail puremagic.com writes:
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
}


-- 
May 05 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2943


David Simcha <dsimcha yahoo.com> changed:

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





---
*** 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: -------
Jul 05 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2943


David Simcha <dsimcha yahoo.com> changed:

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



*** 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: -------
Aug 30 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2943


Steven Schveighoffer <schveiguy yahoo.com> changed:

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



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: -------
Aug 31 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2943




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: -------
Sep 05 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=2943





 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: -------
Sep 05 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Oct 06 2010
prev sibling parent d-bugmail puremagic.com writes:
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



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: -------
Oct 08 2010