Thread overview
Is this a bug in struct literals ?
Jun 08, 2007
HATA
Jun 08, 2007
Frits van Bommel
Jun 08, 2007
Frits van Bommel
Jun 08, 2007
HATA
June 08, 2007
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.
June 08, 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.
June 08, 2007
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
June 08, 2007
> Entered into Bugzilla: http://d.puremagic.com/issues/show_bug.cgi?id=1262

Thank you for your reply and help.