Comment #0 by snarwin+bugzilla — 2020-09-06T23:34:38Z
Example program:
---
struct NeedsInit
{
int n;
@disable this();
}
union U
{
NeedsInit a;
}
struct S
{
U u;
this(NeedsInit arg){ u.a = arg; }
}
---
Compiler output, from run.dlang.io:
Up to 2.060 : Failure with output:
-----
onlineapp.d(15): Error: can only initialize const member a inside constructor
onlineapp.d(15): Error: can only initialize const member a inside constructor
onlineapp.d(15): Error: constructor onlineapp.S.this field u must be initialized in constructor
-----
2.061 to 2.080.1: Success and no output
Since 2.081.2: Failure with output: onlineapp.d(15): Error: field `u` must be initialized in constructor
This appears to be a known bug, since there is a comment about unions in `dmd.semantic3.Semantic3Visitor.visit(FuncDeclaration)`:
/* Current bugs in the flow analysis:
* 1. union members should not produce error messages even if
* not assigned to
* 2. structs should recognize delegating opAssign calls as well
* as delegating calls to other constructors
*/
Comment #1 by dlang-bot — 2021-04-08T22:54:17Z
@MoonlightSentinel updated dlang/dmd pull request #12402 "Fix 21229 - Accept construction of union member as initialization" fixing this issue:
- Fix 21229 - Accept construction of a unions member as initialization
Because the `union` is initialised when one of its members has been
constructed.
https://github.com/dlang/dmd/pull/12402
Comment #2 by dlang-bot — 2021-04-09T07:39:29Z
dlang/dmd pull request #12402 "Fix 21229 - Accept construction of union member as initialization" was merged into stable:
- d123fabb47d7c64a25997b8d2dcc2f1a7e981469 by MoonlightSentinel:
Fix 21229 - Accept construction of a unions member as initialization
Because the `union` is initialised when one of its members has been
constructed.
https://github.com/dlang/dmd/pull/12402
Comment #3 by dlang-bot — 2021-04-09T10:48:55Z
dlang/dmd pull request #12408 "Merge stable into master" was merged into master:
- 655278312e2851b74b1f88dd71c7ffdb47224c20 by MoonlightSentinel:
Fix 21229 - Accept construction of a unions member as initialization
Because the `union` is initialised when one of its members has been
constructed.
https://github.com/dlang/dmd/pull/12408