Is this a bug in struct literals ?

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Fri Jun 8 04:56:03 PDT 2007


HATA wrote:
> Hello.
> 
> I found that a question of following program in D-Language sled in huge BBS named 2ch in Japan.
> We think this is bug. In dmd 1.015ver. this bug has not yet fixed.
> If you has known these bugs, sorry.
> 
> import std.stdio;
> struct A{int v;} 
> void main(){ 
>     A a = A(10); 
>     if (a == a.init) writefln(a.v,"(a==a.init)"); 
>     else writefln(a.v,"(a!=a.init)"); 
>     a.v = 100; 
>     if (a == a.init) writefln(a.v,"(a==a.init)"); 
>     else writefln(a.v,"(a!=a.init)"); 
>     a = A(1000); 
>     if (a == a.init) writefln(a.v,"(a==a.init)"); 
>     else writefln(a.v,"(a!=a.init)"); 
> } 
> 
> Result of execution:
> all comparisons in "if" are true.
> and all values of a.v are 10.

Definitely a bug. If you look at the (unoptimized) assembly code 
generated you can see that each comparison of a to a.init _writes 10 to 
a.v_ before the comparison (as though the code was "if ((a = a.init) && 
(a == a.init))".
(The optimized version also does this, but optimizes out the code for 
"a.v = 100" and "a = A(1000)" since they have no effect)

AFAIK this is a previously unknown bug. It seems to have been introduced 
in DMD 1.014 along with struct literals themselves.


More information about the Digitalmars-d-bugs mailing list