Skip to content

Commit

Permalink
rfix
Browse files Browse the repository at this point in the history
  • Loading branch information
somzzz committed Nov 2, 2017
1 parent 3c2d007 commit 6fdead0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/ddmd/dcast.d
Expand Up @@ -67,8 +67,8 @@ extern (C++) Expression implicitCastTo(Expression e, Scope* sc, Type t)
override void visit(Expression e)
{
//printf("Expression.implicitCastTo(%s of type %s) => %s\n", e.toChars(), e.type.toChars(), t.toChars());

MATCH match = e.implicitConvTo(t);

if (match)
{
if (match == MATCH.constant && (e.type.constConv(t) || !e.isLvalue() && e.type.equivalent(t)))
Expand Down Expand Up @@ -935,6 +935,15 @@ extern (C++) MATCH implicitConvTo(Expression e, Type t)
return;
}

// Disallow pointer conversions from shared to unshared
// Bug https://issues.dlang.org/show_bug.cgi?id=17769
// Caused by default = MATCH.constant for visit(CallExp) and default = MATCH.nomatch for visit(Expression)
if (e.type.ty == Tpointer && t.ty == Tpointer &&
e.type.nextOf().isShared() && !t.nextOf().isShared())
{
return;
}

/* Success
*/
result = MATCH.constant;
Expand Down
27 changes: 27 additions & 0 deletions test/fail_compilation/fail17769.d
@@ -0,0 +1,27 @@
/* TEST_OUTPUT:
---
fail_compilation/fail17769.d(21): Error: cannot implicitly convert expression `s.method()` of type `shared(int)*` to `int*`
fail_compilation/fail17769.d(25): Error: cannot implicitly convert expression `foo1` of type `shared(int)*` to `int*`
fail_compilation/fail17769.d(26): Error: cannot implicitly convert expression `s.ptr` of type `shared(int)*` to `int*`
---
*/

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

struct S()
{
shared(int)* method() { return ptr; }
shared(int)* ptr;
}

void main()
{
S!() s;
// Correctly rejected - new behaviour
int* foo = s.method();

// Correctly rejected - previous behaviour
auto foo1 = s.method();
int* bar = foo1; // rejected as expected
int* baz = s.ptr; // ditto
}

0 comments on commit 6fdead0

Please sign in to comment.