Bug 19919 – Incorrect initialization of union when first member isn't marked = void
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2019-05-29T10:41:41Z
Last change time
2021-07-30T12:25:46Z
Keywords
accepts-invalid, pull
Assigned to
No Owner
Creator
Simen Kjaeraas
Comments
Comment #0 by simen.kjaras — 2019-05-29T10:41:41Z
unittest {
union U {
int n;
float f = 3.14f;
}
U u;
assert(u.f == 3.14f);
}
The above assert should pass, as U has exactly one explicit initializer. Instead, the value of u.f is 0.0, because int.init is used instead. Another option is that the above should fail to compile with an 'overlapping default initialization' error.
If n is void-initialized, f is correctly set to 3.14f. If n is a type with an initalizer other than 0, the initial value changes. (if n's initializer is 1078523331, the assert passes)
Comment #1 by razvan.nitu1305 — 2019-06-04T12:26:31Z
The spec:
"Unions are by default initialized to whatever the Initializer for the first field is, and if none is supplied, to the default initializer for the first field's type" [1]
It seems that the resolution to this would be to issue an error (that would state that the initialization is not used) if we were to stick to the spec.
Or we could modify the spec and allow a single explicit initializer per union.
[1] https://dlang.org/spec/struct.html#default_union_init
Comment #2 by andrei — 2019-06-04T16:20:16Z
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.
Comment #3 by dlang-bot — 2019-06-26T09:46:46Z
@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
Comment #4 by dlang-bot — 2019-07-02T12:26:04Z
@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
Comment #5 by dlang-bot — 2019-07-10T16:45:23Z
dlang/dmd pull request #10122 "Fix Issues 19919 and 19917 - Incorrect initialization of union when first member isn't marked = void" was merged into master:
- a6c0a59757d137829187801fc059e4198c18e67d by RazvanN7:
Fix Issues 19919 and 19917 - Incorrect initialization of union when first member isn't marked = void
- 523dfc1b1be6759ebca5139ec805946bcd29f7d0 by RazvanN7:
Fix Issues 19919 and 19917 - Incorrect initialization of union when first member isn't marked = void
https://github.com/dlang/dmd/pull/10122
Comment #6 by dlang-bot — 2021-07-30T12:25:46Z
dlang/dmd pull request #12899 "End deprecation period for incorrect initialization of union when first member isn't marked (Issues 19919 and 19917)" was merged into master:
- 5600f50c129c45d7867238a0488fc49f8aa167cd by Iain Buclaw:
End deprecation period for incorrect initialization of union when first member isn't marked (Issues 19919 and 19917)
https://github.com/dlang/dmd/pull/12899