www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11132] New: Odd diagnostic with C-style struct initializer when union field is present

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

           Summary: Odd diagnostic with C-style struct initializer when
                    union field is present
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: diagnostic
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: andrej.mitrovich gmail.com



15:35:22 PDT ---
-----
struct S
{
    int x;
    union
    {
        int a;
        int b;
    }

    int z;
}

void main()
{
    S s = { 1, 2, 3 };
}
-----

$ dmd test.d
test.d(15): Error: variable test.main.s is not a static and cannot have static
initializer

A more appropriate diagnostic is emitted when using the regular D-style syntax:

S s = S(1, 2, 3);
test.d(17): Error: overlapping initialization for b

*However*, C (or is it just C++?) supports a special syntax for initializing
union fields:

S s = { 1, { 2 }, 3 };

But the above is an error in D:
test.d(17): Error: a struct is not a valid initializer for a int

I don't know if we want to support this. But we at least have to fix the
diagnostic in the first example.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 27 2013
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11132




15:39:07 PDT ---
In case you're wondering, I've ran into it while porting this code:

-----
struct STGMEDIUM
{
    DWORD tymed;
    union
    {
        HBITMAP hBitmap;
        PVOID hMetaFilePict;
        HENHMETAFILE hEnhMetaFile;
        HGLOBAL hGlobal;
        LPWSTR lpszFileName;
        LPSTREAM pstm;
        LPSTORAGE pstg;
    }

    LPUNKNOWN pUnkForRelease;
}

STGMEDIUM stgmed = { TYMED.TYMED_HGLOBAL, { 0 }, 0 };
-----

Speaking of which, what is a union field initialized to in D? I mean, what does
.init equal to for multiple types? Is it just the bit-pattern zero?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 27 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11132


Kenji Hara <k.hara.pg gmail.com> changed:

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



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

After merging the PR, compier will report more better message for the OP code:
test.d(22): Error: overlapping initialization for field a and b

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




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

https://github.com/D-Programming-Language/dmd/commit/3f671f1bee0e769efc45c7684b891210707806cb
fix Issue 11132 - Odd diagnostic with C-style struct initializer when union
field is present

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


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla digitalmars.com



00:36:13 PDT ---

 Speaking of which, what is a union field initialized to in D?
It's supposed to be set to the .init of its first field, and any remaining bytes set to 0. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 05 2013
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=11132


Walter Bright <bugzilla digitalmars.com> changed:

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


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