www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 4967] New: { } struct literals not documented, and not working

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4967

           Summary: { } struct literals not documented, and not working
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Windows
            Status: NEW
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: ah08010-d yahoo.com



PDT ---
I unthinkingly created a struct literal like S s = { 9 }; and it worked (which,
now that I'm complaining about it, I can't find documented in the chm). 

But this format doesn't appear to initialize any other fields:
$ cat test.d
import std.stdio;

struct S {
    int key;

    int x = 1;

    void dump() {
        writefln("%s %s", key, x);
    }
}

void main() {
    S s1;
    s1.dump;

    S s2 = { 9 };
    s2.dump;
}
This code prints:
$ dmd -run test.d
0 1
9 1310436

Note the trash in the second variable. I am not sure what the correct solution
is - should struct literals require ctor style, or use static-initializer
format and an implicit copy? - but I would certainly have appreciated a
warning, if nothing else, when I started compiling code like this.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 01 2010
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4967


nfxjfg gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |nfxjfg gmail.com
            Version|D2                          |D1



They're deprecated in D2.
I think D1 has them documented.
Maybe should mark this as INVALID.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 01 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4967




PDT ---
If they're deprecated in D2 but present in D1, why is this now a D1 ticket? The
problem (for me) is with D2. If they're deprecated, etc., then all the more
reason for dmd2 to issue a warning.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 01 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4967


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |clugdbug yahoo.com.au
            Version|D1                          |D1 & D2




 They're deprecated in D2.
I hope that will happen, but so far it hasn't. They are still in the spec.
 I think D1 has them documented.
 Maybe should mark this as INVALID.
They are documented in D2, in struct.html. It says: "The static initializer syntax can also be used to initialize non-static variables, provided that the member names are not given. The initializer need not be evaluatable at compile time." But in DMD, ever since CTFE was introduced, it must be evaluatable at compile time. The implementation is also full of bugs. For example, see bug 3271. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 01 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4967


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|{ } struct literals not     |member default initializers
                   |documented, and not working |not working in static
                   |                            |struct initializers



They are documented in struct.html, "Static Initialization of Structs"

The bug is that default initializers are ignored for members which aren't in
the struct static initializer. When this bug was originally filed, they got
garbage.
Now they use the default initializer for the type, but they should use the
member initializer, if one is present.

Barely tested patch, in init.c (350), StructInitializer::toExpression():

            if (!(*elements)[i])
               // Default initialize
+                if (vd->init)
+                    (*elements)[i] = vd->init->toExpression();
+                else                
                    (*elements)[i] = vd->type->defaultInit();

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 29 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4967


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dsimcha yahoo.com



*** Issue 6396 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 29 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4967


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mrmocool gmx.de



*** Issue 7058 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 29 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4967


Don <clugdbug yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull



https://github.com/D-Programming-Language/dmd/pull/968

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 29 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4967




Commit pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/a13f8d15b1f91ddece6e49c627925a386674ed7c
Fix issue 4967 member default initializers not working in static struct
initializers

If a member has an initializer, use it in prefence to the initializer for the
type.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 30 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4967


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 31 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=4967


Andrej Mitrovic <andrej.mitrovich gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich gmail.com



20:29:02 PDT ---
I think Issue 2485 is a dup of this.

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