digitalmars.D.learn - Struct literals bug ?
- andrea9940 (4/4) Sep 29 2013 I think that I found a bug in the initialization of a struct.
- John Colvin (48/52) Sep 29 2013 I'm not sure what the rules are for initialisation of unions, or
- andrea9940 (3/3) Sep 29 2013 Thanks for the answer, I will use the aliases; however I just
- monarch_dodra (31/34) Sep 29 2013 I think there is a bug in there somewhere though:
- John Colvin (32/66) Sep 29 2013 Definitely something very wrong:
- andrea9940 (2/2) Sep 30 2013 Added to bugzilla
- monarch_dodra (3/5) Sep 30 2013 Good news, it's already fixed :)
I think that I found a bug in the initialization of a struct. This program throws "a.v.y != 0" but all assert should pass... (I'm using the latest dmd version available) Code: http://pastebin.com/VHQP8DaE
Sep 29 2013
On Sunday, 29 September 2013 at 16:36:22 UTC, andrea9940 wrote:I think that I found a bug in the initialization of a struct. This program throws "a.v.y != 0" but all assert should pass... (I'm using the latest dmd version available) Code: http://pastebin.com/VHQP8DaEI'm not sure what the rules are for initialisation of unions, or even if they are defined. What's happening is that the memory is being initialised first by your explicit initialisers for xyza and then again by the defaul initialisers (nan in the case of float) for rgba. There's no need for a union though in that example, as the types are the same. Would this work for you? struct Vector(ubyte size, Type) { static assert(size < 5); static if (size > 0) { Type x = 0; alias r = x; } static if (size > 1) { Type y = 0; alias g = y; } static if (size > 2) { Type z = 0; alias b = z; } static if (size > 3) { Type w = 1; alias a = w; } } struct A { int n; Vector!(3, float) v; } unittest { Vector!(3, float) v; assert(v.x == 0, "v.x != 0"); assert(v.y == 0, "v.y != 0"); assert(v.z == 0, "v.z != 0"); A a; assert(a.v.x == 0, "a.v.x != 0"); assert(a.v.y == 0, "a.v.y != 0"); assert(a.v.z == 0, "a.v.z != 0"); }
Sep 29 2013
Thanks for the answer, I will use the aliases; however I just tried my code on codepad and surprising it worked without errors http://codepad.org/hp0YxIi7
Sep 29 2013
On Sunday, 29 September 2013 at 17:35:33 UTC, andrea9940 wrote:Thanks for the answer, I will use the aliases; however I just tried my code on codepad and surprising it worked without errors http://codepad.org/hp0YxIi7I think there is a bug in there somewhere though: //---- struct V{ union { struct { float x = 0; float y = 0; } struct { float r; float g; } } } void main() { assert(V.init.x == 0 && V.init.y == 0); //Passes V v1 = V.init; assert(v1 is V.init); //Passes V v2 = V(); // OR simply use: V v2; assert(v2 is V.init); //Fails } //---- That just isn't right. "T.init" and "T()" is supposed to be equivalent (bar static opCall). Also, I'm 99% confident that when initializing a union, the first member (in this case, the struct), is the one that gets initialized. *fully* initialized. So my conclusion is that there is something wrong in the construction sequence/defintion, and that this bug is definitely valid.
Sep 29 2013
On Sunday, 29 September 2013 at 18:01:01 UTC, monarch_dodra wrote:On Sunday, 29 September 2013 at 17:35:33 UTC, andrea9940 wrote:Definitely something very wrong: struct V { union { struct { float x = 0; float y = 0; int a = 3; } struct { float r; float g; int d; } } } import std.stdio; void main() { writeln("V(", V.init.x, ", ", V.init.y, ", ", V.init.a, ", ", V.init.r, ", ", V.init.g, ", ", V.init.d, ")"); writeln(V.init); writeln(V(V.init.x, V.init.y, V.init.a)); } V(0, 0, 3, 0, 0, 3) V(0, nan, 0, 0, nan, 0) V(0, 0, 3, 0, 0, 3) WAT???Thanks for the answer, I will use the aliases; however I just tried my code on codepad and surprising it worked without errors http://codepad.org/hp0YxIi7I think there is a bug in there somewhere though: //---- struct V{ union { struct { float x = 0; float y = 0; } struct { float r; float g; } } } void main() { assert(V.init.x == 0 && V.init.y == 0); //Passes V v1 = V.init; assert(v1 is V.init); //Passes V v2 = V(); // OR simply use: V v2; assert(v2 is V.init); //Fails } //---- That just isn't right. "T.init" and "T()" is supposed to be equivalent (bar static opCall). Also, I'm 99% confident that when initializing a union, the first member (in this case, the struct), is the one that gets initialized. *fully* initialized. So my conclusion is that there is something wrong in the construction sequence/defintion, and that this bug is definitely valid.
Sep 29 2013
Added to bugzilla http://d.puremagic.com/issues/show_bug.cgi?id=11147
Sep 30 2013
On Monday, 30 September 2013 at 08:48:08 UTC, andrea9940 wrote:Added to bugzilla http://d.puremagic.com/issues/show_bug.cgi?id=11147Good news, it's already fixed :) Thanks Kenji
Sep 30 2013