Thread overview
[Issue 1262] New: Local variable of struct type initialized by literal resets when compared to .init
Jun 08, 2007
d-bugmail
Jun 12, 2007
d-bugmail
Jun 12, 2007
d-bugmail
Jun 13, 2007
d-bugmail
Jun 28, 2007
d-bugmail
Jul 24, 2007
d-bugmail
June 08, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1262

           Summary: Local variable of struct type initialized by literal
                    resets when compared to .init
           Product: D
           Version: 1.014
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: fvbommel@wxs.nl


(First seen in a message posted by "HATA" to d.D.bugs, http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.bugs&article_id=11355)
---
import std.stdio;

struct A { int v; }

void main() {
    A a = A(10);

    writefln("Before test 1: ", a.v);
    if (a == a.init) writefln(a.v,"(a==a.init)");
    else writefln(a.v,"(a!=a.init)");

    a.v = 100;
    writefln("Before test 2: ", a.v);
    if (a == a.init) writefln(a.v,"(a==a.init)");
    else writefln(a.v,"(a!=a.init)");

    a = A(1000);
    writefln("Before test 3: ", a.v);
    if (a == a.init) writefln(a.v,"(a==a.init)");
    else writefln(a.v,"(a!=a.init)");
}
---
Even though the value of a.v is not 10 before the last two if statements, it gets set to 10 right before the test.

This doesn't happen if 'a' is a global variable, or if a static opCall(int) with traditional implementation is added. So this is a workaround:
---
struct A {
    int v;
    static A opCall(int x) {
        A result;
        result.v = x;
        return result;
    }
}
---


-- 

June 12, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1262





------- Comment #1 from davidl@126.com  2007-06-12 11:10 -------
umm, struct literals should not be l-value


-- 

June 12, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1262





------- Comment #2 from fvbommel@wxs.nl  2007-06-12 12:13 -------
(In reply to comment #1)
> umm, struct literals should not be l-value

And your point is?

At no point in the code is a struct literal used as an l-value. There's a
struct variable initialized _by_ a struct literal, and a struct literal being
assigned to a variable, but those are the only struct literals used. And those
operations should only copy the value of the struct literal to the variable
being initialized, which should be perfectly fine.
Structs are value types, not reference types.


-- 

June 13, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1262





------- Comment #3 from davidl@126.com  2007-06-12 20:55 -------
heh, you are right. Literal things screw up in my mind


-- 

June 28, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1262


bugzilla@digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED




------- Comment #4 from bugzilla@digitalmars.com  2007-06-27 20:58 -------
Fixed dmd 2.001, 1.017


-- 

July 24, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=1262





------- Comment #5 from thomas-dloop@kuehne.cn  2007-07-24 08:44 -------
Added to DStress as http://dstess.kuehne.cn/run/i/init_06_A.d http://dstess.kuehne.cn/run/i/init_06_B.d http://dstess.kuehne.cn/run/i/init_06_C.d http://dstess.kuehne.cn/run/i/init_06_D.d


-- 

October 15, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=1262



--- Comment #6 from github-bugzilla@puremagic.com 2013-10-15 13:03:43 PDT ---
Commit pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/8f00475ef27cd6382500b8296ae7f23ed3c8fe16 too tight constraint that has no use in private function

Fixes issue 1262

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------