diff --git a/src/expression.c b/src/expression.c index 11801191c3de..ed591205a87b 100644 --- a/src/expression.c +++ b/src/expression.c @@ -4088,7 +4088,8 @@ Expression *ArrayLiteralExp::semantic(Scope *sc) semanticTypeInfo(sc, t0); - if (sc->func && !sc->intypeof && elements && type->toBasetype()->ty == Tarray) + if (sc->func && !sc->intypeof && !(sc->flags & SCOPEctfe) && + elements && type->toBasetype()->ty == Tarray) { for (size_t i = 0; i < elements->dim; i++) { @@ -4239,7 +4240,7 @@ Expression *AssocArrayLiteralExp::semantic(Scope *sc) if (tkey == Type::terror || tvalue == Type::terror) return new ErrorExp; - if (sc->func && !sc->intypeof && keys->dim && sc->func->setGC()) + if (sc->func && !sc->intypeof && !(sc->flags & SCOPEctfe) && keys->dim && sc->func->setGC()) { error("associative array literal in @nogc function %s may cause GC allocation", sc->func->toChars()); return new ErrorExp; @@ -5175,7 +5176,7 @@ Expression *NewExp::semantic(Scope *sc) //printf("NewExp:type '%s'\n", type->toChars()); semanticTypeInfo(sc, type); - if (sc->func && !sc->intypeof) + if (sc->func && !sc->intypeof && !(sc->flags & SCOPEctfe)) { if (member && !member->isNogc() && sc->func->setGC()) { @@ -9417,7 +9418,7 @@ Expression *DeleteExp::semantic(Scope *sc) } } - if (sc->func && !sc->intypeof && sc->func->setGC()) + if (sc->func && !sc->intypeof && !(sc->flags & SCOPEctfe) && sc->func->setGC()) { error("cannot use 'delete' in @nogc function %s", sc->func->toChars()); goto Lerr; @@ -10423,7 +10424,7 @@ Expression *IndexExp::semantic(Scope *sc) e2 = e2->implicitCastTo(sc, taa->index); // type checking } type = taa->next; - if (sc->func && !sc->intypeof && sc->func->setGC()) + if (sc->func && !sc->intypeof && !(sc->flags & SCOPEctfe) && sc->func->setGC()) { error("indexing an associative array in @nogc function %s may cause gc allocation", sc->func->toChars()); @@ -11326,7 +11327,7 @@ Expression *AssignExp::semantic(Scope *sc) checkDefCtor(ale->loc, tn); semanticTypeInfo(sc, tn); - if (sc->func && !sc->intypeof && sc->func->setGC()) + if (sc->func && !sc->intypeof && !(sc->flags & SCOPEctfe) && sc->func->setGC()) { error("Setting 'length' in @nogc function %s may cause GC allocation", sc->func->toChars()); @@ -11621,7 +11622,7 @@ Expression *CatAssignExp::semantic(Scope *sc) if (e) return e; - if (sc->func && !sc->intypeof && sc->func->setGC()) + if (sc->func && !sc->intypeof && !(sc->flags & SCOPEctfe) && sc->func->setGC()) { error("cannot use operator ~= in @nogc function %s", sc->func->toChars()); return new ErrorExp(); @@ -12075,7 +12076,7 @@ Expression *CatExp::semantic(Scope *sc) if (e) return e; - if (sc->func && !sc->intypeof && sc->func->setGC()) + if (sc->func && !sc->intypeof && !(sc->flags & SCOPEctfe) && sc->func->setGC()) { error("cannot use operator ~ in @nogc function %s", sc->func->toChars()); return new ErrorExp(); diff --git a/test/fail_compilation/nogc.d b/test/fail_compilation/nogc.d index 4adc379a62b7..71b2ed241109 100644 --- a/test/fail_compilation/nogc.d +++ b/test/fail_compilation/nogc.d @@ -154,3 +154,37 @@ class D17 : C17 int bar() { return x; } return &bar; } + +/******************************************/ +// 12630 + +void test12630() @nogc +{ + // All of these declarations should cause no errors. + + static const ex1 = new Exception("invalid"); + //enum ex2 = new Exception("invalid"); + + static const arr1 = [[1,2], [3, 4]]; + enum arr2 = [[1,2], [3, 4]]; + + static const aa1 = [1:1, 2:2]; + enum aa2 = [1:1, 2:2]; + + static const v1 = aa1[1]; + enum v2 = aa2[1]; + + Object o; + static const del1 = (delete o).sizeof; + enum del2 = (delete o).sizeof; + + int[] a; + static const len1 = (a.length = 1).sizeof; + enum len2 = (a.length = 1).sizeof; + + static const cata1 = (a ~= 1).sizeof; + enum cata2 = (a ~= 1).sizeof; + + static const cat1 = (a ~ a).sizeof; + enum cat2 = (a ~ a).sizeof; +}