www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 13890] New: Comparing arrays within structs results in an

https://issues.dlang.org/show_bug.cgi?id=13890

          Issue ID: 13890
           Summary: Comparing arrays within structs results in an
                    assignment.
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Windows
            Status: NEW
          Severity: normal
          Priority: P1
         Component: druntime
          Assignee: nobody puremagic.com
          Reporter: b.j.dejong hotmail.com

There seems to a bug when comparing two dynamic arrays contained within
structs. The resulting operation is a comparison but also an assignment so the
right answer is returned but the left hand side of the comparison now contains
the elements of the right hand side of the comparison.

The problem does not occur when comparing static arrays nor when comparing
arrays normally or arrays contained within classes.

The following code is the smallest version that always reproduces the bug on my
system:

struct Bug {
    float[] elements = new float[4];

    public this(float[] elements...) {
        this.elements[] = elements[];
    }

    public bool opEquals(const Bug other) {
        return other.elements[] == this.elements[];
    }
}

unittest {
    import std.stdio;
    auto a = Bug(1,2,3,4);

    writeln(a.elements); // Bug([1, 2, 3, 4])
    writeln(a == Bug(0,0,0,0)); // true?? <-- This is where the bug happens.
    writeln(a.elements); // Bug([0, 0, 0, 0])
}

When looking at it through the debugger it seems the array in a had all if it's
elements reassigned to those in the temporary.

--
Dec 24 2014