Skip to content

Commit

Permalink
fix Issue 12630 - @nogc should recognize compile-time evaluation context
Browse files Browse the repository at this point in the history
  • Loading branch information
9rnsr committed Apr 24, 2014
1 parent 9871b57 commit 1cc4287
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/expression.c
Expand Up @@ -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++)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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())
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
34 changes: 34 additions & 0 deletions test/fail_compilation/nogc.d
Expand Up @@ -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;
}

0 comments on commit 1cc4287

Please sign in to comment.