diff --git a/src/expression.c b/src/expression.c index c4f2ba390ba3..78cfa1d4b30f 100644 --- a/src/expression.c +++ b/src/expression.c @@ -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; @@ -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"); } diff --git a/src/interpret.c b/src/interpret.c index d8b69181d6c3..f4c7676cc0d2 100644 --- a/src/interpret.c +++ b/src/interpret.c @@ -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; } diff --git a/src/mtype.c b/src/mtype.c index 026686613ca2..de48960f1d3e 100644 --- a/src/mtype.c +++ b/src/mtype.c @@ -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)) @@ -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) diff --git a/test/fail_compilation/fail13601.d b/test/fail_compilation/fail13601.d new file mode 100644 index 000000000000..4831dd311993 --- /dev/null +++ b/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; +}