Skip to content

Commit

Permalink
Merge pull request #8048 from WalterBright/fix15660
Browse files Browse the repository at this point in the history
fix Issue 15660 - break immutable with pure function and mutable refe…
merged-on-behalf-of: Walter Bright <WalterBright@users.noreply.github.com>
  • Loading branch information
dlang-bot committed Mar 21, 2018
2 parents 87f537a + ab5a186 commit 54ab04e
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 25 deletions.
11 changes: 9 additions & 2 deletions src/dmd/dcast.d
Expand Up @@ -30,6 +30,7 @@ import dmd.expressionsem;
import dmd.func;
import dmd.globals;
import dmd.impcnvtab;
import dmd.id;
import dmd.init;
import dmd.intrange;
import dmd.mtype;
Expand Down Expand Up @@ -784,7 +785,7 @@ extern (C++) MATCH implicitConvTo(Expression e, Type t)

override void visit(CallExp e)
{
enum LOG = 0;
enum LOG = false;
static if (LOG)
{
printf("CallExp::implicitConvTo(this=%s, type=%s, t=%s)\n", e.toChars(), e.type.toChars(), t.toChars());
Expand All @@ -797,7 +798,13 @@ extern (C++) MATCH implicitConvTo(Expression e, Type t)
/* Allow the result of strongly pure functions to
* convert to immutable
*/
if (e.f && e.f.isReturnIsolated())
if (e.f && e.f.isReturnIsolated() &&
(!global.params.vsafe || // lots of legacy code breaks with the following purity check
e.f.isPure() >= PURE.strong ||
// Special case exemption for Object.dup() which we assume is implemented correctly
e.f.ident == Id.dup &&
e.f.toParent2() == ClassDeclaration.object.toParent())
)
{
result = e.type.immutableOf().implicitConvTo(t);
if (result > MATCH.constant) // Match level is MATCH.constant at best.
Expand Down
1 change: 1 addition & 0 deletions src/dmd/id.d
Expand Up @@ -295,6 +295,7 @@ immutable Msgtable[] msgtable =
{ "_ArrayPostblit" },
{ "_ArrayDtor" },
{ "_d_delThrowable" },
{ "dup" },

// For pragma's
{ "Pinline", "inline" },
Expand Down
16 changes: 0 additions & 16 deletions test/compilable/fix17635.d

This file was deleted.

23 changes: 23 additions & 0 deletions test/fail_compilation/fix17635.d
@@ -0,0 +1,23 @@
/* REQUIRED_ARGS: -dip1000
TEST_OUTPUT:
---
fail_compilation/fix17635.d(22): Error: cannot implicitly convert expression `f(& p)` of type `immutable(int)**` to `immutable(int**)`
---
*/
// https://issues.dlang.org/show_bug.cgi?id=17635
// https://issues.dlang.org/show_bug.cgi?id=15660

alias T = immutable int;

T** f(const T** input) pure
{
T** output;
return output;
}

void main()
{
T i;
T* p = &i;
immutable T** r = f(&p);
}
22 changes: 22 additions & 0 deletions test/fail_compilation/test15660.d
@@ -0,0 +1,22 @@
/* REQUIRED_ARGS: -dip1000
TEST_OUTPUT:
---
fail_compilation/test15660.d(20): Error: cannot implicitly convert expression `f(v)` of type `int[]` to `immutable(int[])`
---
*/

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

int[] f(ref void[] m) pure
{
auto result = new int[5];
m = result;
return result;
}

void main()
{
void[] v;
immutable x = f(v);
}

4 changes: 2 additions & 2 deletions test/runnable/arrayop.d
Expand Up @@ -627,14 +627,14 @@ void test9656()

{
int[] ma = [1,2,3];
immutable ia = ma.dup;
immutable ia = ma.idup;
}


{
static struct V { int val; }
V[] ma = [V(1), V(2)];
immutable ia = ma.dup;
immutable ia = ma.idup;
}

{
Expand Down
2 changes: 1 addition & 1 deletion test/runnable/implicit.d
Expand Up @@ -265,7 +265,7 @@ int*[] pureFoo() pure { return null; }

void testDIP29_5() pure
{
{ char[] s; immutable x = s.dup; }
{ char[] s; immutable x = s.idup; }
{ immutable x = (cast(int*[])null).dup; }
{ immutable x = pureFoo(); }
{ immutable x = pureFoo().dup; }
Expand Down
10 changes: 6 additions & 4 deletions test/runnable/testconst.d
Expand Up @@ -3169,14 +3169,16 @@ void test8408()
struct T2 { S2 s; }
struct T3 { S1 s1; S2 s2; }

test!(int , true )();
test!(int[3], true )();
/*
test!(int , false)();
test!(int[3], false)();
test!(C , false)();
test!(S1 , true )();
test!(S1 , false)();
test!(S2 , false)();
test!(T1 , true )();
test!(T1 , false)();
test!(T2 , false)();
test!(T3 , false)();
*/
}

/************************************/
Expand Down

0 comments on commit 54ab04e

Please sign in to comment.