Navigation Menu

Skip to content

Commit

Permalink
fix Issue 9393 - Partial template specialization and template lambda …
Browse files Browse the repository at this point in the history
…does not work
  • Loading branch information
9rnsr committed Jan 26, 2013
1 parent 2d83684 commit 5af207f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 18 deletions.
28 changes: 17 additions & 11 deletions src/cast.c
Expand Up @@ -1745,15 +1745,18 @@ Expression *CommaExp::castTo(Scope *sc, Type *t)

/****************************************
* Set type inference target
* t Target type
* flag 1: don't put an error when inference fails
* sc it is used for the semantic of t, when != NULL
* tparams template parameters should be inferred
*/

Expression *Expression::inferType(Type *t, int flag, TemplateParameters *tparams)
Expression *Expression::inferType(Type *t, int flag, Scope *sc, TemplateParameters *tparams)
{
return this;
}

Expression *ArrayLiteralExp::inferType(Type *t, int flag, TemplateParameters *tparams)
Expression *ArrayLiteralExp::inferType(Type *t, int flag, Scope *sc, TemplateParameters *tparams)
{
if (t)
{
Expand All @@ -1764,7 +1767,7 @@ Expression *ArrayLiteralExp::inferType(Type *t, int flag, TemplateParameters *tp
for (size_t i = 0; i < elements->dim; i++)
{ Expression *e = (*elements)[i];
if (e)
{ e = e->inferType(tn, flag, tparams);
{ e = e->inferType(tn, flag, sc, tparams);
(*elements)[i] = e;
}
}
Expand All @@ -1773,7 +1776,7 @@ Expression *ArrayLiteralExp::inferType(Type *t, int flag, TemplateParameters *tp
return this;
}

Expression *AssocArrayLiteralExp::inferType(Type *t, int flag, TemplateParameters *tparams)
Expression *AssocArrayLiteralExp::inferType(Type *t, int flag, Scope *sc, TemplateParameters *tparams)
{
if (t)
{
Expand All @@ -1785,14 +1788,14 @@ Expression *AssocArrayLiteralExp::inferType(Type *t, int flag, TemplateParameter
for (size_t i = 0; i < keys->dim; i++)
{ Expression *e = (*keys)[i];
if (e)
{ e = e->inferType(ti, flag, tparams);
{ e = e->inferType(ti, flag, sc, tparams);
(*keys)[i] = e;
}
}
for (size_t i = 0; i < values->dim; i++)
{ Expression *e = (*values)[i];
if (e)
{ e = e->inferType(tv, flag, tparams);
{ e = e->inferType(tv, flag, sc, tparams);
(*values)[i] = e;
}
}
Expand All @@ -1801,7 +1804,7 @@ Expression *AssocArrayLiteralExp::inferType(Type *t, int flag, TemplateParameter
return this;
}

Expression *FuncExp::inferType(Type *to, int flag, TemplateParameters *tparams)
Expression *FuncExp::inferType(Type *to, int flag, Scope *sc, TemplateParameters *tparams)
{
if (!to)
return this;
Expand Down Expand Up @@ -1859,7 +1862,10 @@ Expression *FuncExp::inferType(Type *to, int flag, TemplateParameters *tparams)
Type *tprm = p->type;
if (tprm->reliesOnTident(tparams))
goto L1;
tprm = tprm->semantic(loc, td->scope);
if (sc)
tprm = tprm->semantic(loc, sc);
if (tprm->ty == Terror)
goto L1;
tiargs->push(tprm);
u = dim; // break inner loop
}
Expand Down Expand Up @@ -1915,13 +1921,13 @@ Expression *FuncExp::inferType(Type *to, int flag, TemplateParameters *tparams)
return e;
}

Expression *CondExp::inferType(Type *t, int flag, TemplateParameters *tparams)
Expression *CondExp::inferType(Type *t, int flag, Scope *sc, TemplateParameters *tparams)
{
if (t)
{
t = t->toBasetype();
e1 = e1->inferType(t, flag, tparams);
e2 = e2->inferType(t, flag, tparams);
e1 = e1->inferType(t, flag, sc, tparams);
e2 = e2->inferType(t, flag, sc, tparams);
}
return this;
}
Expand Down
10 changes: 5 additions & 5 deletions src/expression.h
Expand Up @@ -142,7 +142,7 @@ struct Expression : Object
virtual MATCH implicitConvTo(Type *t);
virtual IntRange getIntRange();
virtual Expression *castTo(Scope *sc, Type *t);
virtual Expression *inferType(Type *t, int flag = 0, TemplateParameters *tparams = NULL);
virtual Expression *inferType(Type *t, int flag = 0, Scope *sc = NULL, TemplateParameters *tparams = NULL);
virtual void checkEscape();
virtual void checkEscapeRef();
virtual Expression *resolveLoc(Loc loc, Scope *sc);
Expand Down Expand Up @@ -453,7 +453,7 @@ struct ArrayLiteralExp : Expression
Expression *interpret(InterState *istate, CtfeGoal goal = ctfeNeedRvalue);
MATCH implicitConvTo(Type *t);
Expression *castTo(Scope *sc, Type *t);
Expression *inferType(Type *t, int flag = 0, TemplateParameters *tparams = NULL);
Expression *inferType(Type *t, int flag = 0, Scope *sc = NULL, TemplateParameters *tparams = NULL);
dt_t **toDt(dt_t **pdt);

Expression *doInline(InlineDoState *ids);
Expand All @@ -479,7 +479,7 @@ struct AssocArrayLiteralExp : Expression
Expression *interpret(InterState *istate, CtfeGoal goal = ctfeNeedRvalue);
MATCH implicitConvTo(Type *t);
Expression *castTo(Scope *sc, Type *t);
Expression *inferType(Type *t, int flag = 0, TemplateParameters *tparams = NULL);
Expression *inferType(Type *t, int flag = 0, Scope *sc = NULL, TemplateParameters *tparams = NULL);

Expression *doInline(InlineDoState *ids);
Expression *inlineScan(InlineScanState *iss);
Expand Down Expand Up @@ -684,7 +684,7 @@ struct FuncExp : Expression
Expression *implicitCastTo(Scope *sc, Type *t);
MATCH implicitConvTo(Type *t);
Expression *castTo(Scope *sc, Type *t);
Expression *inferType(Type *t, int flag = 0, TemplateParameters *tparams = NULL);
Expression *inferType(Type *t, int flag = 0, Scope *sc = NULL, TemplateParameters *tparams = NULL);
char *toChars();
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
elem *toElem(IRState *irs);
Expand Down Expand Up @@ -1654,7 +1654,7 @@ struct CondExp : BinExp
void toCBuffer(OutBuffer *buf, HdrGenState *hgs);
MATCH implicitConvTo(Type *t);
Expression *castTo(Scope *sc, Type *t);
Expression *inferType(Type *t, int flag = 0, TemplateParameters *tparams = NULL);
Expression *inferType(Type *t, int flag = 0, Scope *sc = NULL, TemplateParameters *tparams = NULL);

Expression *doInline(InlineDoState *ids);
Expression *inlineScan(InlineScanState *iss);
Expand Down
4 changes: 2 additions & 2 deletions src/template.c
Expand Up @@ -1507,7 +1507,7 @@ MATCH TemplateDeclaration::deduceFunctionTemplateMatch(Scope *sc, Loc loc, Objec
if (farg->op == TOKfunction)
{ FuncExp *fe = (FuncExp *)farg;
Type *tp = prmtype;
Expression *e = fe->inferType(tp, 1, parameters);
Expression *e = fe->inferType(tp, 1, paramscope, inferparams);
if (!e)
goto Lvarargs;
farg = e;
Expand Down Expand Up @@ -1695,7 +1695,7 @@ MATCH TemplateDeclaration::deduceFunctionTemplateMatch(Scope *sc, Loc loc, Objec
{ FuncExp *fe = (FuncExp *)arg;
Type *tp = tb->nextOf();

Expression *e = fe->inferType(tp, 1, parameters);
Expression *e = fe->inferType(tp, 1, paramscope, inferparams);
if (!e)
goto Lnomatch;
arg = e;
Expand Down
25 changes: 25 additions & 0 deletions test/runnable/funclit.d
Expand Up @@ -716,6 +716,30 @@ void test9153()
];
}

/***************************************************/
// 9393

template ifThrown9393a(E)
{
void ifThrown9393a(T)(scope T delegate(E) errHandler)
{
}
}
void ifThrown9393b(E, T)(scope T delegate(E) errHandler)
{
}

void foo9393(T)(void delegate(T) dg){ dg(T.init); }
void foo9393()(void delegate(int) dg){ foo9393!int(dg); }

void test9393()
{
ifThrown9393a!Exception(e => 10);
ifThrown9393b!Exception(e => 10);

foo9393((x){ assert(x == int.init); });
}

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

int main()
Expand Down Expand Up @@ -758,6 +782,7 @@ int main()
test8496();
test8575();
test9153();
test9393();

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

0 comments on commit 5af207f

Please sign in to comment.