Skip to content

Commit

Permalink
Merge pull request #2947 from 9rnsr/fix11720
Browse files Browse the repository at this point in the history
Issue 11720 - Function-local static variables should cause "already defined in another scope" error
  • Loading branch information
WalterBright committed Dec 25, 2013
2 parents 6acfacb + 0b575f8 commit 5901e16
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
28 changes: 17 additions & 11 deletions src/expression.c
Expand Up @@ -6155,15 +6155,18 @@ Expression *DeclarationExp::semantic(Scope *sc)
if (ad)
{
if (ad->decl && ad->decl->dim == 1)
{ s = (*ad->decl)[0];
{
s = (*ad->decl)[0];
continue;
}
}
break;
}

if (s->isVarDeclaration())
{ // Do semantic() on initializer first, so:
VarDeclaration *v = s->isVarDeclaration();
if (v)
{
// Do semantic() on initializer first, so:
// int a = a;
// will be illegal.
declaration->semantic(sc);
Expand All @@ -6176,26 +6179,29 @@ Expression *DeclarationExp::semantic(Scope *sc)
if (s->ident)
{
if (!sc->insert(s))
{ error("declaration %s is already defined", s->toPrettyChars());
{
error("declaration %s is already defined", s->toPrettyChars());
return new ErrorExp();
}
else if (sc->func)
{
if ( (s->isFuncDeclaration() || s->isTypedefDeclaration() ||
s->isAggregateDeclaration() || s->isEnumDeclaration() ||
s->isInterfaceDeclaration()) &&
if ((s->isFuncDeclaration() ||
s->isTypedefDeclaration() ||
s->isAggregateDeclaration() ||
s->isEnumDeclaration() ||
v && v->isDataseg()) && // Bugzilla 11720
!sc->func->localsymtab->insert(s))
{
error("declaration %s is already defined in another scope in %s",
s->toPrettyChars(), sc->func->toChars());
return new ErrorExp();
}
else
{ // Disallow shadowing

{
// Disallow shadowing
for (Scope *scx = sc->enclosing; scx && scx->func == sc->func; scx = scx->enclosing)
{ Dsymbol *s2;

{
Dsymbol *s2;
if (scx->scopesym && scx->scopesym->symtab &&
(s2 = scx->scopesym->symtab->lookup(s->ident)) != NULL &&
s != s2)
Expand Down
33 changes: 33 additions & 0 deletions test/fail_compilation/fail11720.d
@@ -0,0 +1,33 @@
// REQUIRED_ARGS: -o-
/*
TEST_OUTPUT:
---
fail_compilation/fail11720.d(23): Error: declaration fail11720.foo!().foo.temp is already defined in another scope in foo
fail_compilation/fail11720.d(13): Error: template instance fail11720.foo!() error instantiating
fail_compilation/fail11720.d(31): Error: declaration fail11720.bar.temp is already defined in another scope in bar
---
*/

void main()
{
foo();
bar();
}

alias TypeTuple(T...) = T;

void foo()()
{
foreach (T; TypeTuple!(int, double))
{
static temp = T.stringof;
}
}

void bar()
{
foreach (T; TypeTuple!(int, double))
{
static temp = T.stringof;
}
}

0 comments on commit 5901e16

Please sign in to comment.