Bug 13890 – Comparing arrays within structs results in an assignment.
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2014-12-24T14:53:00Z
Last change time
2017-07-05T15:11:44Z
Assigned to
nobody
Creator
b.j.dejong
Comments
Comment #0 by b.j.dejong — 2014-12-24T14:53:43Z
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.
Comment #1 by ketmar — 2014-12-24T15:02:48Z
can't reproduce on DMD git head, x86. so the bug may be x86_64 specific.
Figured out what the actual problem seems to be.
The bug doesn't happen during array comparison but during struct initialisation. All structs apparently get the same array instead of new ones.
When changing the array initialisation to the constructor the bug goes away.
Comment #4 by yebblies — 2015-01-25T14:22:52Z
I can't confirm on linux64 either. It looks superficially similar to issue 2947.
Comment #5 by dlang-bugzilla — 2017-07-05T15:11:44Z
(In reply to b.j.dejong from comment #0)
> struct Bug {
> float[] elements = new float[4];
This code is invalid - Every instance of the "Bug" type will have its "elements" field will point to the same copy of the array. It should not have compiled. See issue 17604.