Bug 2421 – Struct assignment is broken (nulls target before evaluating source)

Status
RESOLVED
Resolution
WORKSFORME
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2008-10-17T13:22:00Z
Last change time
2015-06-09T01:31:13Z
Keywords
wrong-code
Assigned to
ibuclaw
Creator
fvbommel

Comments

Comment #0 by fvbommel — 2008-10-17T13:22:46Z
When you assign a struct value to a struct variable, the variable is under some circumstances nulled before evaluating the value. This leads to miscompilation if fields of the value depend on those of the variable. Test case: ----- module test; // Some lib-independence version(Tango) { import tango.io.Stdout; void show(Foo f) { Stdout.formatln("{} : {}", f.p, f.flag); } } else { import std.stdio; void show(Foo f) { writefln("%s : %s", f.p, f.flag); } } struct Foo { void* p; bool flag; } void main() { // Random non-zero values auto f = Foo(&show, true); // Prints correct non-zero values show(f); // Set to fresh copy of itself... f = Foo(f.p, f.flag); // prints {null} : false show(f); } ----- This seems to be as minimal as I can make it. Removing either field makes the problem go away, as does replacing the pointer with an int or float (but replacing the pointer with an object reference or double doesn't). $ gdc --version gdc (GCC) 4.1.3 20070831 (prerelease gdc 0.25, using dmd 1.021) (Ubuntu 0.25-4.1.2-16ubuntu1)
Comment #1 by ibuclaw — 2010-08-05T15:12:05Z
I cannot reproduce this on gdc 0.25 using dmd 1.060 with gcc-4.1.2 and gcc-4.3.5.