Skip to content

Commit

Permalink
Merge pull request #4706 from 9rnsr/fix14556
Browse files Browse the repository at this point in the history
[REG2.067] Issue 14556 - can't instantiate struct that has constructor and static array of enum
  • Loading branch information
WalterBright committed Jun 4, 2015
2 parents a74d774 + 3cde9f0 commit 920d18e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -8446,15 +8446,15 @@ Expression *CallExp::semantic(Scope *sc)
StructLiteralExp *sle = new StructLiteralExp(loc, sd, NULL, e1->type);
if (!sd->fill(loc, sle->elements, true))
return new ErrorExp();
// Bugzilla 14556: Set concrete type to avoid further redundant semantic().
sle->type = e1->type;

/* Copy from the initializer symbol for larger symbols,
* otherwise the literals expressed as code get excessively large.
*/
if (sd->size(loc) > Target::ptrsize * 4 && !t1->needsNested())
sle->sinit = toInitializer(sd);

sle->type = type;

Expression *e = sle;
if (CtorDeclaration *cf = sd->ctor->isCtorDeclaration())
{
Expand Down
9 changes: 8 additions & 1 deletion src/struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,14 @@ bool StructDeclaration::fill(Loc loc, Expressions *elements, bool ctorinit)
Type *telem = vx->type;
if (telem->ty == Tsarray)
{
telem = telem->baseElemOf();
/* We cannot use Type::baseElemOf() here.
* If the bottom of the Tsarray is an enum type, baseElemOf()
* will return the base of the enum, and its default initializer
* would be different from the enum's.
*/
while (telem->toBasetype()->ty == Tsarray)
telem = ((TypeSArray *)telem->toBasetype())->next;

if (telem->ty == Tvoid)
telem = Type::tuns8->addMod(telem->mod);
}
Expand Down
27 changes: 27 additions & 0 deletions test/runnable/structlit.d
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,32 @@ void test13021()
auto fail4 = U4(1.0).b; // OK <- Internal error: backend/el.c 2904
}

/********************************************/
// 14556

enum E14556 { a = 1 }

struct S14556a
{
this(int) {}
E14556[1] data;
}

struct S14556b
{
this(int) {}
void[1] data;
}

void test14556()
{
auto sa = S14556a(0);
assert(sa.data == [E14556.a]);

auto sb = S14556b(0);
assert(sb.data[] == cast(ubyte[1])[0]);
}

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

int main()
Expand Down Expand Up @@ -1448,6 +1474,7 @@ int main()
test11147();
test11256();
test13021();
test14556();

printf("Success\n");
return 0;
Expand Down

0 comments on commit 920d18e

Please sign in to comment.