Skip to content

Commit

Permalink
fix Issue 6359 - Pure/@safe-inference should not be affected by __tra…
Browse files Browse the repository at this point in the history
…its(compiles)
  • Loading branch information
9rnsr committed Jun 6, 2014
1 parent 4b9eeef commit 94c11fd
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/expression.c
Expand Up @@ -2304,7 +2304,7 @@ void Expression::checkPurity(Scope *sc, FuncDeclaration *f)
// OR, they must have the same pure parent.
if (!f->isPure() && calledparent != outerfunc)
{
if (outerfunc->setImpure())
if (sc->flags & SCOPEcompile ? outerfunc->isPureBypassingInferenceX() : outerfunc->setImpure())
{
error("pure function '%s' cannot call impure function '%s'",
outerfunc->toPrettyChars(), f->toPrettyChars());
Expand Down Expand Up @@ -2353,7 +2353,7 @@ void Expression::checkPurity(Scope *sc, VarDeclaration *v)
FuncDeclaration *ff = s->isFuncDeclaration();
if (!ff)
break;
if (ff->setImpure())
if (sc->flags & SCOPEcompile ? ff->isPureBypassingInferenceX() : ff->setImpure())
{
error("pure function '%s' cannot access mutable static data '%s'",
sc->func->toPrettyChars(), v->toChars());
Expand Down Expand Up @@ -2405,7 +2405,7 @@ void Expression::checkPurity(Scope *sc, VarDeclaration *v)
FuncDeclaration *ff = s->isFuncDeclaration();
if (!ff)
break;
if (ff->setImpure())
if (sc->flags & SCOPEcompile ? ff->isPureBypassingInferenceX() : ff->setImpure())
{
error("pure nested function '%s' cannot access mutable data '%s'",
ff->toChars(), v->toChars());
Expand Down Expand Up @@ -2437,7 +2437,7 @@ void Expression::checkSafety(Scope *sc, FuncDeclaration *f)

if (!f->isSafe() && !f->isTrusted())
{
if (sc->func->setUnsafe())
if (sc->flags & SCOPEcompile ? sc->func->isSafeBypassingInference() : sc->func->setUnsafe())
{
if (loc.linnum == 0) // e.g. implicitly generated dtor
loc = sc->func->loc;
Expand All @@ -2461,7 +2461,7 @@ void Expression::checkNogc(Scope *sc, FuncDeclaration *f)

if (!f->isNogc())
{
if (sc->func->setGC())
if (sc->flags & SCOPEcompile ? sc->func->isNogcBypassingInference() : sc->func->setGC())
{
if (loc.linnum == 0) // e.g. implicitly generated dtor
loc = sc->func->loc;
Expand Down
1 change: 1 addition & 0 deletions src/func.c
Expand Up @@ -1291,6 +1291,7 @@ void FuncDeclaration::semantic3(Scope *sc)
sc2->structalign = STRUCTALIGN_DEFAULT;
if (this->ident != Id::require && this->ident != Id::ensure)
sc2->flags = sc->flags & ~SCOPEcontract;
sc2->flags &= ~SCOPEcompile;
sc2->tf = NULL;
sc2->os = NULL;
sc2->noctor = 0;
Expand Down
5 changes: 5 additions & 0 deletions src/traits.c
Expand Up @@ -902,6 +902,11 @@ Expression *semanticTraits(TraitsExp *e, Scope *sc)
ex = ex->semantic(sc2);
ex = resolvePropertiesOnly(sc2, ex);
ex = ex->optimize(WANTvalue);
if (sc2->func && sc2->func->type->ty == Tfunction)
{
TypeFunction *tf = (TypeFunction *)sc2->func->type;
canThrow(ex, sc2->func, tf->isnothrow);
}
ex = checkGC(sc2, ex);
if (ex->op == TOKerror)
err = true;
Expand Down
63 changes: 63 additions & 0 deletions test/compilable/testInference.d
Expand Up @@ -84,6 +84,69 @@ void test6351()
alias bug6351!(deleg6351) baz6531;
}

/***************************************************/
// 6359

void impure6359() nothrow @safe @nogc {}
void throwable6359() pure @safe @nogc {}
void system6359() pure nothrow @nogc {}
void gcable6359() pure nothrow @safe {}

int global6359;

void f6359() pure nothrow @safe @nogc
{
static assert(!__traits(compiles, impure6359()));
static assert(!__traits(compiles, throwable6359()));
static assert(!__traits(compiles, system6359()));
static assert(!__traits(compiles, gcable6359()));
static assert(!__traits(compiles, global6359++));

//static assert(!__traits(compiles, { impure6359(); }())); // BUG: blocked by issue 9148.
static assert(!__traits(compiles, { throwable6359(); }()));
static assert(!__traits(compiles, { system6359(); }()));
static assert(!__traits(compiles, { gcable6359(); }()));
static assert(!__traits(compiles, { global6359++; }()));
}

void g6359()() pure nothrow @safe @nogc
{
static assert(!__traits(compiles, impure6359()));
static assert(!__traits(compiles, throwable6359()));
static assert(!__traits(compiles, system6359()));
static assert(!__traits(compiles, gcable6359()));
static assert(!__traits(compiles, global6359++));

//static assert(!__traits(compiles, { impure6359(); }())); // BUG: blocked by issue 9148.
static assert(!__traits(compiles, { throwable6359(); }()));
static assert(!__traits(compiles, { system6359(); }()));
static assert(!__traits(compiles, { gcable6359(); }()));
static assert(!__traits(compiles, { global6359++; }()));
}

// attribute inference is not affected by the expressions inside __traits(compiles)
void h6359()()
{
static assert( __traits(compiles, impure6359()));
static assert( __traits(compiles, throwable6359()));
static assert( __traits(compiles, system6359()));
static assert( __traits(compiles, gcable6359()));
static assert( __traits(compiles, global6359++));

static assert( __traits(compiles, { impure6359(); }()));
static assert( __traits(compiles, { throwable6359(); }()));
static assert( __traits(compiles, { system6359(); }()));
static assert( __traits(compiles, { gcable6359(); }()));
//static assert( __traits(compiles, { global6359++; }())); // BUG: blocked by issue 9148.
}

void test6359() pure nothrow @safe @nogc
{
f6359();
g6359();
h6359();
}

/***************************************************/
// 7017

Expand Down
6 changes: 2 additions & 4 deletions test/runnable/statictor.d
Expand Up @@ -38,10 +38,8 @@ static this() nothrow pure @safe
int* p;
static assert(!__traits(compiles, ++p));
static assert(!__traits(compiles, ++global6677));
// BUG (not related to this):
//auto throwit = { throw new Exception("sup"); };
//static assert(!__traits(compiles, throwit() )); // Should pass
//throwit() // This fails, but __traits(compiles, ...) claims it compiles
auto throwit = { throw new Exception("sup"); };
static assert(!__traits(compiles, throwit() ));
}

shared static this() nothrow pure @safe
Expand Down

0 comments on commit 94c11fd

Please sign in to comment.