Jump to page: 1 2
Thread overview
[Issue 19917] unions should require that all members are `= void` initialised
May 29, 2019
Nicholas Wilson
May 29, 2019
Manu
May 29, 2019
kinke@gmx.net
May 29, 2019
Manu
May 29, 2019
kinke@gmx.net
May 29, 2019
Manu
Jun 04, 2019
Manu
Jun 26, 2019
Dlang Bot
Jul 02, 2019
Dlang Bot
Jul 10, 2019
Dlang Bot
Jul 30, 2021
Dlang Bot
May 29, 2019
https://issues.dlang.org/show_bug.cgi?id=19917

Nicholas Wilson <iamthewilsonator@hotmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |iamthewilsonator@hotmail.co
                   |                            |m

--- Comment #1 from Nicholas Wilson <iamthewilsonator@hotmail.com> ---
The way it currently works is the first member of a union is the one that dictates the .init of the union

struct S
{
  union
  {
    int x;
    float y; // COMPILE ERROR, 'x' already initialised!
  }

  union
  {
    int z;
    float w = void; // <- this is fine
  }
}

S s;
assert( (cast(int*)&s)[0 .. 2]) == [0,0]); // passes

reverse the order and the assert fails.

If you want void initialisation then if know (or can compute) the size of the
union (say N) then you can make the first member a `void[N] dummy;`.

If the docs need to make that more explicit do say so, go forth and complain on the forums!

--
May 29, 2019
https://issues.dlang.org/show_bug.cgi?id=19917

--- Comment #2 from Manu <turkeyman@gmail.com> ---
Okay... well take this as the enhancement request that it is ;)
That existing rule you describe is unintuitive and terrible. Users should
specify `= void` on every member except one of their choosing. You can't
possibly go wrong then, and there's also zero magic rules in the compiler that
need to be explained under that arrangement.

--
May 29, 2019
https://issues.dlang.org/show_bug.cgi?id=19917

kinke@gmx.net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kinke@gmx.net

--- Comment #3 from kinke@gmx.net ---
I've just seen that LDC handles this differently than DMD. With LDC, an explicit initializer takes precedence, regardless of the lexical order:

union S
{
    int x; // compiler error if also explicitly initialized (except for `=
void`)
    float y = 1.0f;
}

void main()
{
    S s;
    assert(s.y == 1.0f);
}

--
May 29, 2019
https://issues.dlang.org/show_bug.cgi?id=19917

--- Comment #4 from Manu <turkeyman@gmail.com> ---
Wow.
Seriously, the only sensible thing to do is issue a compile error if more than
one item doesn't have `= void`. That would be very sane.

--
May 29, 2019
https://issues.dlang.org/show_bug.cgi?id=19917

--- Comment #5 from kinke@gmx.net ---
Nah, I think LDC's way is fine, but it could be improved by *requiring* (exactly) one field to be explicitly initialized - clear as day and not much more syntax overhead:

union U {
  int i;
  float f = 1.0f;
  real r;
}

vs.

union U {
  int i = void;
  float f = 1.0f;
  real r = void;
}

and

union U {
  int i = 0;
  float f;
  real r;
}

vs.

union U {
  int i;
  float f = void;
  real r = void;
}

--
May 29, 2019
https://issues.dlang.org/show_bug.cgi?id=19917

--- Comment #6 from Manu <turkeyman@gmail.com> ---
Either way. I like the `= void` personally. It's explicit, and there's no magic
rules to implement in the compiler.
It's also not always convenient to express a default construction.

--
June 04, 2019
https://issues.dlang.org/show_bug.cgi?id=19917

Andrei Alexandrescu <andrei@erdani.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrei@erdani.com

--- Comment #7 from Andrei Alexandrescu <andrei@erdani.com> ---
That's excessive. Pasting from https://issues.dlang.org/show_bug.cgi?id=19919:

I think the principle of maximum simplicity goes like this:

* Only the first field can have an initializer (if you want another field...
move it to the top).
* If that initializer is "= void", the union is void-initialized.

All else is in error. That's that. Doesn't prevent any work getting done and is simple to spec and implement.

--
June 04, 2019
https://issues.dlang.org/show_bug.cgi?id=19917

--- Comment #8 from Manu <turkeyman@gmail.com> ---
(In reply to Andrei Alexandrescu from comment #7)
> That's excessive. Pasting from https://issues.dlang.org/show_bug.cgi?id=19919:
> 
> I think the principle of maximum simplicity goes like this:
> 
> * Only the first field can have an initializer (if you want another field...
> move it to the top).
> * If that initializer is "= void", the union is void-initialized.
> 
> All else is in error. That's that. Doesn't prevent any work getting done and is simple to spec and implement.

I don't think it's simple, it's a weird magic rule that you wouldn't/couldn't
expect, and you can't know unless you already know.
Anyway, if it is the case, then the compiler needs to emit an error if an
initialisation is applied to any non-first member explaining that rule.

--
June 26, 2019
https://issues.dlang.org/show_bug.cgi?id=19917

Dlang Bot <dlang-bot@dlang.rocks> changed:

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

--- Comment #9 from Dlang Bot <dlang-bot@dlang.rocks> ---
@RazvanN7 created dlang/dmd pull request #10092 "Fix Issues 19919 and 19917 - Incorrect initialization of union when first member isn't marked = void" fixing this issue:

- Fix Issues 19919 and 19917 - Incorrect initialization of union when first member isn't marked = void

https://github.com/dlang/dmd/pull/10092

--
July 02, 2019
https://issues.dlang.org/show_bug.cgi?id=19917

--- Comment #10 from Dlang Bot <dlang-bot@dlang.rocks> ---
@RazvanN7 created dlang/dmd pull request #10122 "Fix Issues 19919 and 19917 - Incorrect initialization of union when first member isn't marked = void" fixing this issue:

- Fix Issues 19919 and 19917 - Incorrect initialization of union when first member isn't marked = void

https://github.com/dlang/dmd/pull/10122

--
« First   ‹ Prev
1 2