Skip to content

Commit

Permalink
Fix Issue 13947 - Padding in empty structs is compared
Browse files Browse the repository at this point in the history
  • Loading branch information
yebblies committed Jan 7, 2015
1 parent 98a4c74 commit bb98439
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/e2ir.c
Expand Up @@ -2130,7 +2130,12 @@ elem *toElem(Expression *e, IRState *irs)

//printf("EqualExp::toElem()\n");
elem *e;
if (t1->ty == Tstruct)
if (t1->ty == Tstruct && ((TypeStruct *)t1)->sym->fields.dim == 0)
{
// we can skip the compare if the structs are empty
e = el_long(TYbool, ee->op == TOKequal);
}
else if (t1->ty == Tstruct)
{
// Do bit compare of struct's
elem *es1 = toElem(ee->e1, irs);
Expand Down Expand Up @@ -2265,7 +2270,12 @@ elem *toElem(Expression *e, IRState *irs)
//printf("IdentityExp::toElem() %s\n", toChars());

elem *e;
if (t1->ty == Tstruct || t1->isfloating())
if (t1->ty == Tstruct && ((TypeStruct *)t1)->sym->fields.dim == 0)
{
// we can skip the compare if the structs are empty
e = el_long(TYbool, ie->op == TOKidentity);
}
else if (t1->ty == Tstruct || t1->isfloating())
{
// Do bit compare of struct's
elem *es1 = toElem(ie->e1, irs);
Expand Down
22 changes: 22 additions & 0 deletions test/runnable/xtest46.d
Expand Up @@ -5372,6 +5372,27 @@ void test2856()
B[1]; // Okay
}

/***************************************************/

void test13947()
{
struct S {}
static assert(S.sizeof == 1);

S a;
S b;
*cast(ubyte*)&a = 1;
*cast(ubyte*)&b = 2;
assert(a == b);
assert(a is b);
assert(!(a != b));
assert(!(a !is b));
static assert(S() == S());
static assert(S() is S());
static assert(!(S() != S()));
static assert(!(S() !is S()));
}

/***************************************************/
// 3091

Expand Down Expand Up @@ -7507,6 +7528,7 @@ int main()
test9538();
test9700();
test9834();
test13947();
test9883();
test10091();
test9130();
Expand Down

0 comments on commit bb98439

Please sign in to comment.