Skip to content

Commit

Permalink
Merge pull request #6091 from WalterBright/fix16095
Browse files Browse the repository at this point in the history
fix Issue 16095 - a delegate can mutate immutable data and break shar…
  • Loading branch information
AndrejMitrovic authored Aug 27, 2016
2 parents 511618a + 3c53a0f commit d4f45f9
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -9467,6 +9467,19 @@ extern (C++) final class DelegateExp : UnaExp
AggregateDeclaration ad = f.toParent().isAggregateDeclaration();
if (f.needThis())
e1 = getRightThis(loc, sc, ad, e1, f);
if (f.type.ty == Tfunction)
{
TypeFunction tf = cast(TypeFunction)f.type;
if (!MODimplicitConv(e1.type.mod, f.type.mod))
{
OutBuffer thisBuf, funcBuf;
MODMatchToBuffer(&thisBuf, e1.type.mod, tf.mod);
MODMatchToBuffer(&funcBuf, tf.mod, e1.type.mod);
error("%smethod %s is not callable using a %s%s",
funcBuf.peekString(), f.toPrettyChars(), thisBuf.peekString(), e1.toChars());
return new ErrorExp();
}
}
if (ad && ad.isClassDeclaration() && ad.type != e1.type)
{
// A downcast is required for interfaces, see Bugzilla 3706
Expand Down Expand Up @@ -10637,7 +10650,7 @@ extern (C++) final class AddrExp : UnaExp
FuncDeclaration f = dve.var.isFuncDeclaration();
if (f)
{
f = f.toAliasFunc(); // FIXME, should see overlods - Bugzilla 1983
f = f.toAliasFunc(); // FIXME, should see overloads - Bugzilla 1983
if (!dve.hasOverloads)
f.tookAddressOf++;

Expand Down
44 changes: 44 additions & 0 deletions test/fail_compilation/test16095.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
REQUIRED_ARGS:
PERMUTE_ARGS:
TEST_OUTPUT:
---
fail_compilation/test16095.d(20): Error: shared method test16095.C.ping is not callable using a non-shared a
fail_compilation/test16095.d(30): Error: shared method test16095.S.ping is not callable using a non-shared *a
fail_compilation/test16095.d(43): Error: mutable method test16095.Foo.flip is not callable using a immutable foo
---
*/
// https://issues.dlang.org/show_bug.cgi?id=16095

class C
{
void ping() shared;
}

void test1(C a)
{
(&a.ping)(); // error
}

struct S
{
void ping() shared;
}

void test2(S* a)
{
(&a.ping)(); // error
}

struct Foo {
bool flag;
void flip() {
flag = true;
}
}

void test3()
{
immutable Foo foo;
(&foo.flip)(); // error
}

0 comments on commit d4f45f9

Please sign in to comment.