Skip to content

Commit

Permalink
Merge pull request #3905 from 9rnsr/fix13379
Browse files Browse the repository at this point in the history
[REG2.067a] Issue 13379 - Latest dmd fails with recursive template expansion in std.regex
  • Loading branch information
WalterBright committed Aug 31, 2014
2 parents 2c13b63 + 5efa203 commit 178d02e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/template.c
Expand Up @@ -5946,10 +5946,15 @@ void TemplateInstance::semantic(Scope *sc, Expressions *fargs)
}

// If the first instantiation was in speculative context, but this is not:
if (!inst->tinst && inst->speculative && !(sc->flags & (SCOPEstaticif | SCOPEstaticassert | SCOPEcompile)))
if (!inst->tinst && inst->speculative &&
tinst && !(sc->flags & (SCOPEstaticif | SCOPEstaticassert | SCOPEcompile)))
{
// Reconnect the chain if this instantiation is not in speculative context.
inst->tinst = tinst;
TemplateInstance *tix = tinst;
while (tix && tix != inst)
tix = tix->tinst;
if (tix != inst) // Bugzilla 13379: Prevent circular chain
inst->tinst = tinst;
}

#if LOG
Expand Down Expand Up @@ -6279,8 +6284,6 @@ void TemplateInstance::semantic(Scope *sc, Expressions *fargs)
while (ti && !ti->deferred && ti->tinst)
{
ti = ti->tinst;
if (ti == tinst)
break;
if (++nest > 500)
{
global.gag = 0; // ensure error message gets printed
Expand Down Expand Up @@ -7823,21 +7826,14 @@ bool TemplateInstance::needsCodegen()
}
}

if (speculative)
for (TemplateInstance *ti = this; ti; ti = ti->tinst)
{
//printf("\tti = %s spec = %d\n", toChars(), speculative);
TemplateInstance *ti = this;
while (ti->tinst && ti->tinst != this)
{
ti = ti->tinst;
//printf("\tti = %s spec = %d\n", ti->toChars(), ti->speculative);
if (!ti->speculative)
return true;
}
return false;
//printf("\tti = %s spec = %d\n", ti->toChars(), ti->speculative);
if (!ti->speculative)
return true;
}

return true;
return false;
}

/* ======================== TemplateMixin ================================ */
Expand Down
40 changes: 40 additions & 0 deletions test/runnable/template9.d
Expand Up @@ -4149,6 +4149,45 @@ void test13378()
doSome13378(v);
}

/******************************************/
// 13379

void test13379()
{
match13379("");
}

auto match13379(RegEx )(RegEx re)
if (is(RegEx == Regex13379!char)) // #1 Regex!char (speculative && tinst == NULL)
{}
auto match13379(String)(String re)
{}

struct Regex13379(Char)
{
ShiftOr13379!Char kickstart; // #2 ShiftOr!char (speculative && tinst == Regex!char)
}
struct ShiftOr13379(Char)
{
this(ref Regex13379!Char re) // #3 Regex!Char (speculative && tinst == ShiftOr!char)
{
uint n_length;
uint idx;
n_length = min13379(idx, n_length);
}
}

template MinType13379(T...)
{
alias MinType13379 = T[0];
}
MinType13379!T min13379(T...)(T args) // #4 MinType!uint (speculative && thist == ShiftOr!char)
{
alias a = args[0];
alias b = args[$-1];
return cast(typeof(return)) (a < b ? a : b);
}

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

int main()
Expand Down Expand Up @@ -4253,6 +4292,7 @@ int main()
test13299();
test13374();
test13378();
test13379();

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

0 comments on commit 178d02e

Please sign in to comment.