Skip to content

Commit

Permalink
Merge pull request #4084 from 9rnsr/fix13601
Browse files Browse the repository at this point in the history
[REG2.064] Issue 13601 - static if (__ctfe) should emit error
  • Loading branch information
WalterBright committed Oct 24, 2014
2 parents 6ae83a8 + 7565650 commit ff8313c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/expression.c
Expand Up @@ -3019,6 +3019,12 @@ Expression *IdentifierExp::semantic(Scope *sc)

if (ident == Id::ctfe)
{
if (sc->flags & SCOPEctfe)
{
error("variable __ctfe cannot be read at compile time");
return new ErrorExp();
}

// Create the magic __ctfe bool variable
VarDeclaration *vd = new VarDeclaration(loc, Type::tbool, Id::ctfe, NULL);
vd->storage_class |= STCtemp;
Expand Down Expand Up @@ -11569,7 +11575,7 @@ Expression *AssignExp::semantic(Scope *sc)
{
error("cannot rebind scope variables");
}
if (e1->op == TOKvar && ((VarExp*)e1)->var->ident == Id::ctfe)
if (e1->op == TOKvar && ((VarExp *)e1)->var->ident == Id::ctfe)
{
error("cannot modify compiler-generated variable __ctfe");
}
Expand Down
2 changes: 1 addition & 1 deletion src/interpret.c
Expand Up @@ -6125,7 +6125,7 @@ class Interpreter : public Visitor
}
else
{
e->error("%s is not a compile-time boolean expression", e1->toChars());
e->error("%s is not a compile time boolean expression", e1->toChars());
result = EXP_CANT_INTERPRET;
return;
}
Expand Down
11 changes: 9 additions & 2 deletions src/mtype.c
Expand Up @@ -6533,8 +6533,6 @@ Type *TypeIdentifier::syntaxCopy()

void TypeIdentifier::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsymbol **ps, bool intypeid)
{
Dsymbol *scopesym;

//printf("TypeIdentifier::resolve(sc = %p, idents = '%s')\n", sc, toChars());

if ((ident->equals(Id::super) || ident->equals(Id::This)) && !hasThis(sc))
Expand All @@ -6558,7 +6556,16 @@ void TypeIdentifier::resolve(Loc loc, Scope *sc, Expression **pe, Type **pt, Dsy
}
}
}
if (ident == Id::ctfe)
{
error(loc, "variable __ctfe cannot be read at compile time");
*pe = NULL;
*ps = NULL;
*pt = Type::terror;
return;
}

Dsymbol *scopesym;
Dsymbol *s = sc->search(loc, ident, &scopesym);
resolveHelper(loc, sc, s, scopesym, pe, pt, ps, intypeid);
if (*pt)
Expand Down
17 changes: 17 additions & 0 deletions test/fail_compilation/fail13601.d
@@ -0,0 +1,17 @@
/*
TEST_OUTPUT:
---
fail_compilation/fail13601.d(13): Error: variable __ctfe cannot be read at compile time
fail_compilation/fail13601.d(14): Error: variable __ctfe cannot be read at compile time
fail_compilation/fail13601.d(15): Error: variable __ctfe cannot be read at compile time
fail_compilation/fail13601.d(16): Error: variable __ctfe cannot be read at compile time
---
*/

void test()
{
static if (__ctfe) {}
enum a = __ctfe ? "a" : "b";
static int b = __ctfe * 2;
int[__ctfe] sarr;
}

0 comments on commit ff8313c

Please sign in to comment.