Skip to content

Commit

Permalink
fix Issue 12822 - Delegate .ptr assignment considered @safe
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Jun 12, 2016
1 parent 26c2213 commit 51143be
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/expression.d
Expand Up @@ -11914,6 +11914,16 @@ extern (C++) final class DelegatePtrExp : UnaExp
return this;
}

override Expression modifiableLvalue(Scope* sc, Expression e)
{
if (sc.func.setUnsafe())
{
error("cannot modify delegate pointer in @safe code %s", toChars());
return new ErrorExp();
}
return Expression.modifiableLvalue(sc, e);
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down Expand Up @@ -11957,6 +11967,16 @@ extern (C++) final class DelegateFuncptrExp : UnaExp
return this;
}

override Expression modifiableLvalue(Scope* sc, Expression e)
{
if (sc.func.setUnsafe())
{
error("cannot modify delegate function pointer in @safe code %s", toChars());
return new ErrorExp();
}
return Expression.modifiableLvalue(sc, e);
}

override void accept(Visitor v)
{
v.visit(this);
Expand Down
1 change: 1 addition & 0 deletions src/tokens.d
Expand Up @@ -674,6 +674,7 @@ extern (C++) struct Token
TOKon_scope_exit: "scope(exit)",
TOKon_scope_success: "scope(success)",
TOKon_scope_failure: "scope(failure)",
TOKdelegateptr: "delegateptr",
];

static this()
Expand Down
16 changes: 16 additions & 0 deletions test/fail_compilation/test12822.d
@@ -0,0 +1,16 @@
/*
---
fail_compilation/test12822.d(11): Error: cannot modify delegate pointer in @safe code dg.ptr
fail_compilation/test12822.d(12): Error: cannot modify delegate function pointer in @safe code dg.ptr
---
*/

// https://issues.dlang.org/show_bug.cgi?id=12822
void test2(int delegate() dg) @safe
{
static int i;
dg.ptr = &i;
dg.funcptr = &func;
}

int func();

0 comments on commit 51143be

Please sign in to comment.