Skip to content

Commit

Permalink
Fixes Issue 7050 - PrettyPrint calls to unsafe functions for nicer di…
Browse files Browse the repository at this point in the history
…agnostic error messages.

Add line number for implicitly generated function.
  • Loading branch information
AndrejMitrovic committed Oct 21, 2012
1 parent 81860f8 commit ce7d938
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/expression.c
Expand Up @@ -1705,13 +1705,13 @@ void Expression::checkPurity(Scope *sc, FuncDeclaration *f)
{
if (outerfunc->setImpure())
error("pure function '%s' cannot call impure function '%s'",
outerfunc->toChars(), f->toChars());
outerfunc->toPrettyChars(), f->toPrettyChars());
}
}
#else
if (sc->func && sc->func->isPure() && !sc->intypeof && !f->isPure())
error("pure function '%s' cannot call impure function '%s'",
sc->func->toChars(), f->toChars());
sc->func->toPrettyChars(), f->toPrettyChars());
#endif
}

Expand Down Expand Up @@ -1805,8 +1805,13 @@ void Expression::checkSafety(Scope *sc, FuncDeclaration *f)
!f->isSafe() && !f->isTrusted())
{
if (sc->func->setUnsafe())
{
if (loc.linnum == 0) // e.g. implicitly generated dtor
loc = sc->func->loc;

error("safe function '%s' cannot call system function '%s'",
sc->func->toChars(), f->toChars());
sc->func->toPrettyChars(), f->toPrettyChars());
}
}
}
#endif
Expand Down Expand Up @@ -8321,12 +8326,12 @@ Expression *CallExp::semantic(Scope *sc)
if (sc->func && !tf->purity && !(sc->flags & SCOPEdebug))
{
if (sc->func->setImpure())
error("pure function '%s' cannot call impure %s '%s'", sc->func->toChars(), p, e1->toChars());
error("pure function '%s' cannot call impure %s '%s'", sc->func->toPrettyChars(), p, e1->toChars());
}
if (sc->func && tf->trust <= TRUSTsystem)
{
if (sc->func->setUnsafe())
error("safe function '%s' cannot call system %s '%s'", sc->func->toChars(), p, e1->toChars());
error("safe function '%s' cannot call system %s '%s'", sc->func->toPrettyChars(), p, e1->toChars());
}

if (!tf->callMatch(NULL, arguments))
Expand Down
14 changes: 14 additions & 0 deletions test/fail_compilation/diag7050a.d
@@ -0,0 +1,14 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag7050a.d(5): Error: safe function 'diag7050a.foo' cannot call system function 'diag7050a.Foo.this'
---
*/

#line 1
struct Foo {
this (int a) {}
}
@safe void foo() {
auto f = Foo(3);
}
15 changes: 15 additions & 0 deletions test/fail_compilation/diag7050b.d
@@ -0,0 +1,15 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag7050b.d(5): Error: pure function 'diag7050b.f.g' cannot call impure function 'diag7050b.f'
---
*/

#line 1
void f()
{
pure void g()
{
f();
}
}
22 changes: 22 additions & 0 deletions test/fail_compilation/diag7050c.d
@@ -0,0 +1,22 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag7050c.d(7): Error: safe function 'diag7050c.B.~this' cannot call system function 'diag7050c.A.~this'
---
*/

#line 1
struct A
{
~this(){}
}

@safe struct B
{
A a;
}

@safe void f()
{
auto x = B.init;
}

0 comments on commit ce7d938

Please sign in to comment.