Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
September 29, 2013 Struct literals bug ? | ||||
---|---|---|---|---|
| ||||
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 |
September 29, 2013 Re: Struct literals bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to andrea9940 | 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/VHQP8DaE
I'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");
}
|
September 29, 2013 Re: Struct literals bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | 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 |
September 29, 2013 Re: Struct literals bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to andrea9940 | 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/hp0YxIi7
I 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.
|
September 29, 2013 Re: Struct literals bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to monarch_dodra | 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:
>> 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
>
> I 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.
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???
|
September 30, 2013 Re: Struct literals bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to John Colvin | Added to bugzilla http://d.puremagic.com/issues/show_bug.cgi?id=11147 |
September 30, 2013 Re: Struct literals bug ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to andrea9940 | On Monday, 30 September 2013 at 08:48:08 UTC, andrea9940 wrote:
> Added to bugzilla http://d.puremagic.com/issues/show_bug.cgi?id=11147
Good news, it's already fixed :)
Thanks Kenji
|
Copyright © 1999-2021 by the D Language Foundation