Skip to content

Commit

Permalink
Merge pull request #431 from 9rnsr/fix6764
Browse files Browse the repository at this point in the history
Issue 2296 & 6764 - IFTI fails on typesafe variadic function
  • Loading branch information
Don Clugston committed Aug 28, 2012
2 parents c4dfdff + 34e1b76 commit a5c56ac
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 10 deletions.
69 changes: 59 additions & 10 deletions src/template.c
Expand Up @@ -40,6 +40,8 @@ long __cdecl __ehfilter(LPEXCEPTION_POINTERS ep);

#define LOG 0

int templateParameterLookup(Type *tparam, TemplateParameters *parameters);

/********************************************
* These functions substitute for dynamic_cast. dynamic_cast does not work
* on earlier versions of gcc.
Expand Down Expand Up @@ -1590,12 +1592,57 @@ MATCH TemplateDeclaration::deduceFunctionTemplateMatch(Scope *sc, Loc loc, Objec
Type *tb = prmtype->toBasetype();
switch (tb->ty)
{
// Perhaps we can do better with this, see TypeFunction::callMatch()
// 6764 fix - TypeAArray may be TypeSArray have not yet run semantic().
case Tsarray:
{ TypeSArray *tsa = (TypeSArray *)tb;
dinteger_t sz = tsa->dim->toInteger();
if (sz != nfargs - i)
goto Lnomatch;
case Taarray:
{ // Perhaps we can do better with this, see TypeFunction::callMatch()
if (tb->ty == Tsarray)
{ TypeSArray *tsa = (TypeSArray *)tb;
dinteger_t sz = tsa->dim->toInteger();
if (sz != nfargs - i)
goto Lnomatch;
}
else if (tb->ty == Taarray)
{ TypeAArray *taa = (TypeAArray *)tb;
Expression *dim = new IntegerExp(loc, nfargs - i, Type::tsize_t);

int i = templateParameterLookup(taa->index, parameters);
if (i == -1)
{ Expression *e;
Type *t;
Dsymbol *s;
taa->index->resolve(loc, sc, &e, &t, &s);
if (!e)
goto Lnomatch;
e = e->optimize(WANTvalue | WANTinterpret);
e = e->implicitCastTo(sc, Type::tsize_t);
e = e->optimize(WANTvalue);
if (!dim->equals(e))
goto Lnomatch;
}
else
{ // This code matches code in TypeInstance::deduceType()
TemplateParameter *tprm = parameters->tdata()[i];
TemplateValueParameter *tvp = tprm->isTemplateValueParameter();
if (!tvp)
goto Lnomatch;
Expression *e = (Expression *)dedtypes[i];
if (e)
{
if (!dim->equals(e))
goto Lnomatch;
}
else
{
Type *vt = tvp->valType->semantic(0, sc);
MATCH m = (MATCH)dim->implicitConvTo(vt);
if (!m)
goto Lnomatch;
dedtypes[i] = dim;
}
}
}
/* fall through */
}
case Tarray:
{ TypeArray *ta = (TypeArray *)tb;
Expand Down Expand Up @@ -2313,12 +2360,14 @@ int templateIdentifierLookup(Identifier *id, TemplateParameters *parameters)

int templateParameterLookup(Type *tparam, TemplateParameters *parameters)
{
assert(tparam->ty == Tident);
TypeIdentifier *tident = (TypeIdentifier *)tparam;
//printf("\ttident = '%s'\n", tident->toChars());
if (tident->idents.dim == 0)
if (tparam->ty == Tident)
{
return templateIdentifierLookup(tident->ident, parameters);
TypeIdentifier *tident = (TypeIdentifier *)tparam;
//printf("\ttident = '%s'\n", tident->toChars());
if (tident->idents.dim == 0)
{
return templateIdentifierLookup(tident->ident, parameters);
}
}
return -1;
}
Expand Down
31 changes: 31 additions & 0 deletions test/runnable/template9.d
Expand Up @@ -306,6 +306,15 @@ void test2246(){
f2246!(C2246!(int,2))();
}

/**********************************/
// 2296

void foo2296(uint D)(int[D] i...){}
void test2296()
{
foo2296(1, 2, 3);
}

/**********************************/
// 1684

Expand Down Expand Up @@ -743,6 +752,26 @@ void test6994()
foo.func2!int(); // NG
}

/**********************************/
// 6764

enum N6764 = 1; //use const for D1

alias size_t[N6764] T6764; //workaround
void f6764()(T6764 arr...) { }

void g6764()(size_t[1] arr...) { }

void h6764()(size_t[N6764] arr...) { }

void test6764()
{
f6764(0); //good
g6764(0); //good
h6764!()(0); //good
h6764(0); //Error: template main.f() does not match any function template declaration
}

/**********************************/
// 3467 & 6806

Expand Down Expand Up @@ -1482,6 +1511,7 @@ int main()
test3608();
test6404();
test2246();
test2296();
bug4984();
test2579();
test5886();
Expand All @@ -1499,6 +1529,7 @@ int main()
test6780();
test6891();
test6994();
test6764();
test3467();
test4413();
test5525();
Expand Down

0 comments on commit a5c56ac

Please sign in to comment.