Thread overview
[Issue 11132] New: Odd diagnostic with C-style struct initializer when union field is present
Sep 27, 2013
Andrej Mitrovic
Sep 27, 2013
Andrej Mitrovic
Oct 02, 2013
Kenji Hara
Oct 05, 2013
Walter Bright
Oct 05, 2013
Walter Bright
September 27, 2013
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


--- Comment #0 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-09-27 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: -------
September 27, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11132



--- Comment #1 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2013-09-27 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: -------
October 02, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11132


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

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


--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2013-10-02 08:46:14 PDT ---
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: -------
October 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11132



--- Comment #3 from github-bugzilla@puremagic.com 2013-10-05 00:32:58 PDT ---
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: -------
October 05, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11132


Walter Bright <bugzilla@digitalmars.com> changed:

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


--- Comment #4 from Walter Bright <bugzilla@digitalmars.com> 2013-10-05 00:36:13 PDT ---
(In reply to comment #1)
> 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: -------
October 05, 2013
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: -------