Thread overview
[Issue 13848] overlapping initialization for r
Dec 10, 2014
Kenji Hara
Dec 10, 2014
deadalnix
Dec 10, 2014
deadalnix
Dec 11, 2014
deadalnix
Dec 11, 2014
Kenji Hara
Jun 04, 2019
RazvanN
Jun 04, 2019
Dlang Bot
Jun 14, 2019
Dlang Bot
December 10, 2014
https://issues.dlang.org/show_bug.cgi?id=13848

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |INVALID

--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> ---
(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;
}

--
December 10, 2014
https://issues.dlang.org/show_bug.cgi?id=13848

deadalnix <deadalnix@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |---

--- Comment #2 from deadalnix <deadalnix@gmail.com> ---
(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.

--
December 10, 2014
https://issues.dlang.org/show_bug.cgi?id=13848

--- Comment #3 from deadalnix <deadalnix@gmail.com> ---
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.

--
December 11, 2014
https://issues.dlang.org/show_bug.cgi?id=13848

--- Comment #4 from deadalnix <deadalnix@gmail.com> ---
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* ) { }
}

--
December 11, 2014
https://issues.dlang.org/show_bug.cgi?id=13848

--- Comment #5 from Kenji Hara <k.hara.pg@gmail.com> ---
(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.

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

RazvanN <razvan.nitu1305@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |razvan.nitu1305@gmail.com

--- Comment #6 from RazvanN <razvan.nitu1305@gmail.com> ---
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);

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

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

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

--- Comment #7 from Dlang Bot <dlang-bot@dlang.rocks> ---
@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

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #8 from Dlang Bot <dlang-bot@dlang.rocks> ---
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

--