Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 11553 - dmd segfault with recursive template #2826

Merged
merged 1 commit into from
Jan 5, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions src/template.c
Original file line number Diff line number Diff line change
Expand Up @@ -2309,9 +2309,50 @@ void functionResolve(Match *m, Dsymbol *dstart, Loc loc, Scope *sc,
return 0;

Dsymbol *s = ti->inst->toAlias();
if (!s->isFuncDeclaration() && !s->isTemplateDeclaration())
FuncDeclaration *fd;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some comments explaining what this section does.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated.

if (TemplateDeclaration *tdx = s->isTemplateDeclaration())
{
Objects dedtypesX; // empty tiargs

// Bugzilla 11553: Check for recursive instantiation of tdx.
for (TemplateDeclaration::Previous *p = tdx->previous; p; p = p->prev)
{
if (arrayObjectMatch(p->dedargs, &dedtypesX))
{
//printf("recursive, no match p->sc=%p %p %s\n", p->sc, this, this->toChars());
/* It must be a subscope of p->sc, other scope chains are not recursive
* instantiations.
*/
for (Scope *scx = sc; scx; scx = scx->enclosing)
{
if (scx == p->sc)
{
error(loc, "recursive template expansion while looking for %s.%s", ti->toChars(), tdx->toChars());
goto Lerror;
}
}
}
/* BUG: should also check for ref param differences
*/
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move declarations of pr and dedtypesX to here. Add comment "Add instantiation scope to threaded list so we can detect recursive instantiations".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved pr. dedtypesX is necessary for arrayObjectMatch(p->dedargs, &dedtypesX).

TemplateDeclaration::Previous pr;
pr.prev = tdx->previous;
pr.sc = sc;
pr.dedargs = &dedtypesX;
tdx->previous = ≺ // add this to threaded list

fd = resolveFuncCall(loc, sc, s, NULL, tthis, fargs, 1);

tdx->previous = pr.prev; // unlink from threaded list
}
else if (s->isFuncDeclaration())
{
fd = resolveFuncCall(loc, sc, s, NULL, tthis, fargs, 1);
}
else
goto Lerror;
FuncDeclaration *fd = resolveFuncCall(loc, sc, s, NULL, tthis, fargs, 1);

if (!fd)
return 0;

Expand Down
22 changes: 22 additions & 0 deletions test/fail_compilation/ice11553.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
TEST_OUTPUT:
---
fail_compilation/ice11553.d(22): Error: recursive template expansion while looking for A!().A()
fail_compilation/ice11553.d(22): Error: expression template A() of type void does not have a boolean value
---
*/

template A(alias T)
{
template A()
{
alias A = T!();
}
}

template B()
{
alias B = A!(.B);
}

static if (A!B) {}
55 changes: 55 additions & 0 deletions test/runnable/template9.d
Original file line number Diff line number Diff line change
Expand Up @@ -2972,6 +2972,61 @@ void test11533c()
assert(foo.call() == var);
}

/******************************************/
// 11553

struct Pack11553(T ...)
{
alias Unpack = T;
enum length = T.length;
}

template isPack11553(TList ...)
{
static if (TList.length == 1 && is(Pack11553!(TList[0].Unpack) == TList[0]))
{
enum isPack11553 = true;
}
else
{
enum isPack11553 = false;
}
}

template PartialApply11553(alias T, uint argLoc, Arg ...)
if (Arg.length == 1)
{
template PartialApply11553(L ...)
{
alias PartialApply11553 = T!(L[0 .. argLoc], Arg, L[argLoc .. $]);
}
}

template _hasLength11553(size_t len, T)
{
static if (T.length == len)
{
enum _hasLength11553 = true;
}
else
{
enum _hasLength11553 = false;
}
}

alias _hasLength11553(size_t len) = PartialApply11553!(._hasLength11553, 0, len);


alias hl11553 = _hasLength11553!1;

// this segfaults
static if (!isPack11553!hl11553) { pragma(msg, "All good 1"); }

// these are fine
static if ( hl11553!(Pack11553!(5))) { pragma(msg, "All good 2"); }

static if (!hl11553!(Pack11553!( ))) { pragma(msg, "All good 3"); }

/******************************************/
// 11818

Expand Down