[Issue 21021] std.variant confused by alias this when struct larger than maxDataSize

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jul 7 08:35:52 UTC 2020


https://issues.dlang.org/show_bug.cgi?id=21021

Simen Kjaeraas <simen.kjaras at gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|dmd                         |phobos

--- Comment #5 from Simen Kjaeraas <simen.kjaras at gmail.com> ---
The root cause here is this, which essentially is what happens inside
std.variant.VariantN.opAssign[0]:

struct S {
    int i;
    alias i this;
    string s;
}

unittest {
    S s = S(3, "Foo");
    S s2 = S(s); // calls S(s.i), the auto-generated constructor
    assert(s.s == "");
}

If we rearrange S such that the string comes first, it simply does not compile,
but when the alias this is (the same type as) the first element of the struct,
S(s) compiles and silently does the wrong thing.


[0]:
https://github.com/dlang/phobos/blob/22ebc705c14bcc2e18e0323c6d53e3bb7274a4fa/std/variant.d#L689

--


More information about the Digitalmars-d-bugs mailing list