Skip to content

Commit

Permalink
Merge pull request #4539 from 9rnsr/fix14388
Browse files Browse the repository at this point in the history
Issue 14388 - ICE with idup-ed struct literal in template argument
  • Loading branch information
WalterBright committed Apr 18, 2015
2 parents 596aef6 + 1ad3183 commit d5b33f2
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/expression.c
Expand Up @@ -4350,7 +4350,7 @@ bool StructLiteralExp::equals(RootObject *o)

Expression *StructLiteralExp::syntaxCopy()
{
StructLiteralExp *exp = new StructLiteralExp(loc, sd, arraySyntaxCopy(elements), stype);
StructLiteralExp *exp = new StructLiteralExp(loc, sd, arraySyntaxCopy(elements), type ? type : stype);
exp->origin = this;
return exp;
}
Expand Down
2 changes: 1 addition & 1 deletion src/interpret.c
Expand Up @@ -1956,7 +1956,7 @@ class Interpreter : public Visitor
void visit(Expression *e)
{
#if LOG
printf("%s Expression::interpret() %s\n", e->loc.toChars(), e->toChars());
printf("%s Expression::interpret() '%s' %s\n", e->loc.toChars(), Token::toChars(e->op), e->toChars());
printf("type = %s\n", e->type->toChars());
e->print();
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/template.c
Expand Up @@ -5806,7 +5806,7 @@ void TemplateInstance::trySemantic3(Scope *sc2)

void TemplateInstance::semantic(Scope *sc, Expressions *fargs)
{
//printf("TemplateInstance::semantic('%s', this=%p, gag = %d, sc = %p)\n", toChars(), this, global.gag, sc);
//printf("[%s] TemplateInstance::semantic('%s', this=%p, gag = %d, sc = %p)\n", loc.toChars(), toChars(), this, global.gag, sc);
#if 0
for (Dsymbol *s = this; s; s = s->parent)
{
Expand Down
45 changes: 45 additions & 0 deletions test/compilable/compile1.d
Expand Up @@ -848,3 +848,48 @@ pragma(msg, typeof(s14166.x ^^ 2)); // ok <- error
pragma(msg, typeof(s14166.y ^^= 2.5)); // ok <- error
pragma(msg, typeof(makeAA14166()[0] = 1)); // ok <- error
pragma(msg, typeof(tup14166.field = makeTup14166())); // ok <- error

/***************************************************/
// 14388

@property immutable(T)[] idup14388(T)(T[] a)
{
alias U = immutable(T);
U[] res;
foreach (ref e; a)
res ~= e;
return res;
}

struct Data14388(A14388 a)
{
auto foo()
{
return Data14388!a.init; // [B]
}
}

struct A14388
{
struct Item {}

immutable(Item)[] items;

this(int dummy)
{
items = [Item()].idup14388;
}
}

void test14388()
{
auto test = Data14388!(A14388(42)).init.foo(); // [A]
/*
* A(42) is interpreter to a struct literal A([immutable(Item)()]).
* The internal VarDeclaration with STCmanifest for the Data's template parameteter 'a'
* calls syntaxCopy() on its ((ExpInitializer *)init)->exp in VarDeclaration::semantic(),
* and 'immutable(Item)()'->syntaxCopy() had incorrectly removed the qualifier.
* Then, the arguments of two Data template instances at [A] and [B] had become unmatch,
* and the second instantiation had created the AST duplication.
*/
}

0 comments on commit d5b33f2

Please sign in to comment.