www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - Is this a bug in struct literals ?

reply HATA <hata mail1.big.or.jp> writes:
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.
Jun 08 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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.
Jun 08 2007
parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
Frits van Bommel wrote:
 AFAIK this is a previously unknown bug. It seems to have been introduced 
 in DMD 1.014 along with struct literals themselves.
Entered into Bugzilla: http://d.puremagic.com/issues/show_bug.cgi?id=1262
Jun 08 2007
parent HATA <hata mail1.big.or.jp> writes:
 Entered into Bugzilla: http://d.puremagic.com/issues/show_bug.cgi?id=1262
Thank you for your reply and help.
Jun 08 2007