Skip to content

Commit

Permalink
Merge pull request #6279 from WalterBright/fix16747
Browse files Browse the repository at this point in the history
fix Issue 16747 - Cannot have stack allocated classes in @safe code
  • Loading branch information
MartinNowak committed Dec 19, 2016
1 parent 9e2d8f7 commit 0117027
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/declaration.d
Original file line number Diff line number Diff line change
Expand Up @@ -2231,7 +2231,7 @@ extern (C++) class VarDeclaration : Declaration
// delete this;
Expression ec;
ec = new VarExp(loc, this);
e = new DeleteExp(loc, ec);
e = new DeleteExp(loc, ec, true);
e.type = Type.tvoid;
break;
}
Expand Down
10 changes: 7 additions & 3 deletions src/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -11067,9 +11067,12 @@ extern (C++) final class NotExp : UnaExp
*/
extern (C++) final class DeleteExp : UnaExp
{
extern (D) this(Loc loc, Expression e)
bool isRAII; // true if called automatically as a result of scoped destruction

extern (D) this(Loc loc, Expression e, bool isRAII)
{
super(loc, TOKdelete, __traits(classInstanceSize, DeleteExp), e);
this.isRAII = isRAII;
}

override Expression semantic(Scope* sc)
Expand Down Expand Up @@ -11184,8 +11187,9 @@ extern (C++) final class DeleteExp : UnaExp
return new ErrorExp();
}

// unsafe
if (!sc.intypeof && sc.func && sc.func.setUnsafe())
if (!sc.intypeof && sc.func &&
!isRAII &&
sc.func.setUnsafe())
{
error("%s is not @safe but is used in @safe function %s", toChars(), sc.func.toChars());
err = true;
Expand Down
1 change: 1 addition & 0 deletions src/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,7 @@ class NotExp : public UnaExp

class DeleteExp : public UnaExp
{
bool isRAII;
public:
Expression *semantic(Scope *sc);
Expression *toBoolean(Scope *sc);
Expand Down
2 changes: 1 addition & 1 deletion src/parse.d
Original file line number Diff line number Diff line change
Expand Up @@ -7616,7 +7616,7 @@ final class Parser : Lexer
case TOKdelete:
nextToken();
e = parseUnaryExp();
e = new DeleteExp(loc, e);
e = new DeleteExp(loc, e, false);
break;

case TOKcast: // cast(type) expression
Expand Down
13 changes: 13 additions & 0 deletions test/compilable/test16747.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
PERMUTE_ARGS:
*/

class C { @safe ~this() { } }
class D : C { }

void fwd() @safe
{
scope o = new Object();
scope c = new C();
scope d = new D();
}
10 changes: 10 additions & 0 deletions test/runnable/testscope.d
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,15 @@ void test7049() @safe

/********************************************/

// https://issues.dlang.org/show_bug.cgi?id=16747

void test16747() @safe
{
scope o = new Object();
}

/********************************************/

void main()
{
test1();
Expand All @@ -274,6 +283,7 @@ void main()
test10();
test7435();
test7049();
test16747();

printf("Success\n");
}

0 comments on commit 0117027

Please sign in to comment.