D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 21021 - std.variant confused by alias this when struct larger than maxDataSize
Summary: std.variant confused by alias this when struct larger than maxDataSize
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P1 major
Assignee: No Owner
URL:
Keywords: pull
Depends on:
Blocks:
 
Reported: 2020-07-06 23:47 UTC by George Toutoungis
Modified: 2020-07-07 10:32 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description George Toutoungis 2020-07-06 23:47:40 UTC

    
Comment 1 George Toutoungis 2020-07-06 23:51:55 UTC
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);
}
////////////////////////////////////////////
Comment 2 George Toutoungis 2020-07-06 23:55:48 UTC
additional comments.
I see the problem on FreeBSD also.
didn't try other OS only (MacOS and FreeBSD).

Thanks a lot,
George
Comment 3 Simen Kjaeraas 2020-07-07 07:33:52 UTC
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.
Comment 4 Dlang Bot 2020-07-07 08:34:47 UTC
@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
Comment 5 Simen Kjaeraas 2020-07-07 08:35:52 UTC
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
Comment 6 Dlang Bot 2020-07-07 10:32:20 UTC
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