Thread overview
[Issue 21021] std.variant confused by alias this when struct larger than maxDataSize
[Issue 21021] sending a struct containing array using send/receive have problems.
Jul 06, 2020
George Toutoungis
Jul 06, 2020
George Toutoungis
Jul 07, 2020
Simen Kjaeraas
Jul 07, 2020
Dlang Bot
Jul 07, 2020
Simen Kjaeraas
Jul 07, 2020
Dlang Bot
July 06, 2020
https://issues.dlang.org/show_bug.cgi?id=21021

--- Comment #1 from George Toutoungis <georges.toutoungis@gmail.com> ---
Hi the following code have problem.
in the Message struct, if you comment on the field "hello", it works fine.
also if you comment the line "alias h this", also it works fine.
if the size of the array is smaller than 3, also it works fine.
absolutely there is a problem. the same problem exist on dmd and ldc2
hope you find it and and improve the compilers.

here is the code. if you have any question, please do not hesitate. thanks a lot for the D programming language, it is great "my favorite".

the code:

/////////////////////////////
module test;

import std.concurrency;
import std.stdio: write, writeln, writef, writefln;


struct LimitEntry
{
    int price;
    int quantity;
}

struct Header
{
        int a;
}

struct Message(ITEM_TYPE)
{
        Header       h;

        int          hello;
        int          element;
        ITEM_TYPE[3] array;

        alias h this;
}


alias MessageType = Message!(LimitEntry);

void spawnedFunc()
{
    receive(
        (MessageType m)
        {
                writeln(m);
        }
    );
}

void main()
{
        auto id = spawn(&spawnedFunc);

        auto message = MessageType();

        message.h.a     = 2;
        message.hello   = 3;
        message.element = 5;
        message.array[0] = LimitEntry(0,0);
        message.array[1] = LimitEntry(1,1);
        message.array[2] = LimitEntry(2,2);

        writeln(message);

        send(id, message);
}
////////////////////////////////////////////

--
July 06, 2020
https://issues.dlang.org/show_bug.cgi?id=21021

--- Comment #2 from George Toutoungis <georges.toutoungis@gmail.com> ---
additional comments.
I see the problem on FreeBSD also.
didn't try other OS only (MacOS and FreeBSD).

Thanks a lot,
George

--
July 07, 2020
https://issues.dlang.org/show_bug.cgi?id=21021

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |simen.kjaras@gmail.com
           Hardware|x86                         |All
            Summary|sending a struct containing |std.variant confused by
                   |array using send/receive    |alias this when struct
                   |have problems.              |larger than maxDataSize
                 OS|Mac OS X                    |All
           Severity|enhancement                 |major

--- Comment #3 from Simen Kjaeraas <simen.kjaras@gmail.com> ---
Reduced:

import std.variant;

struct Message {
        int h;
        int[5] array;
        alias h this;
}

unittest {
    Message msg;
    msg.array[] = 3;
    Variant a = msg;
    auto other = a.get!Message;
    assert(msg.array[0] == 3);
    assert(other.array[0] == 3);
}

Change Variant to VariantN!40, or reduce the size of Message to maxDataSize or below, and it works.

--
July 07, 2020
https://issues.dlang.org/show_bug.cgi?id=21021

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

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

--- Comment #4 from Dlang Bot <dlang-bot@dlang.rocks> ---
@Biotronic created dlang/phobos pull request #7552 "Fix issue 21021 - std.variant confused by alias this when struct larg…" fixing this issue:

- Fix issue 21021 - std.variant confused by alias this when struct larger than maxDataSize

https://github.com/dlang/phobos/pull/7552

--
July 07, 2020
https://issues.dlang.org/show_bug.cgi?id=21021

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

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

--- Comment #5 from Simen Kjaeraas <simen.kjaras@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

--
July 07, 2020
https://issues.dlang.org/show_bug.cgi?id=21021

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

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

--- Comment #6 from Dlang Bot <dlang-bot@dlang.rocks> ---
dlang/phobos pull request #7552 "Fix issue 21021 - std.variant confused by alias this when struct larg…" was merged into master:

- f4fc3e5a34c7ac6580b228180fde582fb16f6629 by Simen Kjærås:
  Fix issue 21021 - std.variant confused by alias this when struct larger than
maxDataSize

https://github.com/dlang/phobos/pull/7552

--