Bug 13848 – overlapping initialization for r

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2014-12-10T09:38:42Z
Last change time
2019-06-14T01:13:53Z
Keywords
pull
Assigned to
No Owner
Creator
deadalnix

Comments

Comment #0 by deadalnix — 2014-12-10T09:38:42Z
Sample code below: module a; template MixinTpl(P) { union { A a; } P p; auto fun() { return SS(a, p); } struct SS { union { A a; ulong r; } P p; } } struct A {} union P {} struct S { mixin MixinTpl!P; }
Comment #1 by k.hara.pg — 2014-12-10T15:41:12Z
(In reply to deadalnix from comment #0) > auto fun() { > return SS(a, p); > } Your're trying to initialize SS.a by the value a. and SS.r by using p. Of course SS.a and SS.r are overlapped each other, so initializing the two fields at the same time is invalid. In this case, you cannot use literal style syntax to construct SS. An alternative way is: auto fun() { SS ss = {a:a, p:p}; // use StructInitializer syntax return ss; }
Comment #2 by deadalnix — 2014-12-10T21:48:50Z
(In reply to Kenji Hara from comment #1) > (In reply to deadalnix from comment #0) > > auto fun() { > > return SS(a, p); > > } > > Your're trying to initialize SS.a by the value a. and SS.r by using p. What ? That do not make any sense. Also, I was able to trigger that error with an explicit constructor in a much more convoluted sample code. Basically as follow: module a; import b; struct S { mixin MixinTpl!P; this(...) { ... } } module b; template MixinTpl(P) { union { A a; } P p; auto fun() { return SS(a, p); } struct SS { union { A a; ulong r; } P p; } } struct A {} union P {} > Of course SS.a and SS.r are overlapped each other, so initializing the two > fields at the same time is invalid. > > In this case, you cannot use literal style syntax to construct SS. An > alternative way is: > > auto fun() { > SS ss = {a:a, p:p}; // use StructInitializer syntax > return ss; > } Ok at least I have a workaround, thank. There is still an issue in there, so I'm reopening.
Comment #3 by deadalnix — 2014-12-10T21:54:24Z
Also, it may have more to do with constructor not being found because of bug, and that this error message is simply a side effect of using default constructor.
Comment #4 by deadalnix — 2014-12-11T05:46:35Z
S type; template TplMixin() { union { D d1; ulong raw; } D d2; auto fun() { return S(d1, d2); } } struct D { } alias SS = typeof(S.init.fun()); struct S { mixin TplMixin; this(D d1, D d2) { } this(SS* ) { } }
Comment #5 by k.hara.pg — 2014-12-11T08:02:18Z
(In reply to deadalnix from comment #2) > What ? That do not make any sense. Also, I was able to trigger that error > with an explicit constructor in a much more convoluted sample code. > Basically as follow: Currently a struct literal expression SS(a, p) always tries to match the arguments to the struct fields by using the order of struct field declarations. In the matching process, the overlapping between fields is not merely considered. I think it's similar to function call. The first argument will match to the first parameter (first field), and the second argument will match to the second parameter (second field), and the third argument will ... Therefore, currently struct literal expression cannot correctly initialize overlapped fields. It's a known limitation. But the error diagnostic would be a bit unkind. It would be better to report the type mismatch between the second argument and corresponding struct field.
Comment #6 by razvan.nitu1305 — 2019-06-04T11:38:59Z
Spec: "If there are anonymous unions in the struct, only the first member of the anonymous union can be initialized with a struct literal, and all subsequent non-overlapping fields are default initialized." [1] [1] https://dlang.org/spec/struct.html#struct-literal The error is therefore correctly issued, however the error message can be improved. Currently, "overlapping initialization for r" does not hint at all how the issue can be fixed. A better workaround would be to have the union as the last field of the struct and then initialize it as S(p, a);
Comment #7 by dlang-bot — 2019-06-04T12:12:44Z
@RazvanN7 created dlang/dmd pull request #9965 "Fix Issue 13848 - overlapping initialization for r" fixing this issue: - Fix Issue 13848 - overlapping initialization for r https://github.com/dlang/dmd/pull/9965
Comment #8 by dlang-bot — 2019-06-14T01:13:53Z
dlang/dmd pull request #9965 "Fix Issue 13848 - overlapping initialization for r" was merged into master: - 69f93c5e86df4663d26bd0dc2bfe4d08be808ac6 by RazvanN7: Fix Issue 13848 - overlapping initialization for r https://github.com/dlang/dmd/pull/9965