Skip to content

Commit

Permalink
fix Issue 12909 - [AA] Function is incorrectly inferred as strongly p…
Browse files Browse the repository at this point in the history
…ure for associative array with key of non-mutable array or pointer as argument
  • Loading branch information
9rnsr committed Jun 14, 2014
1 parent f4cb15c commit 20feb28
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
33 changes: 16 additions & 17 deletions src/mtype.c
Expand Up @@ -5805,12 +5805,26 @@ void TypeFunction::purityLevel()
break;
}

t = t->toBasetype();
t = t->baseElemOf();
if (!t->hasPointers())
continue;
if (t->mod & MODimmutable)
continue;

/* Accept immutable(T)[] and immutable(T)* as being strongly pure
*/
if (t->ty == Tarray || t->ty == Tpointer)
{
Type *tn = t->nextOf()->toBasetype();
if (tn->mod & MODimmutable)
continue;
if (tn->mod & (MODconst | MODwild))
{
tf->purity = PUREconst;
continue;
}
}

/* The rest of this is too strict; fix later.
* For example, the only pointer members of a struct may be immutable,
* which would maintain strong purity.
Expand All @@ -5820,22 +5834,7 @@ void TypeFunction::purityLevel()
tf->purity = PUREconst;
continue;
}
if (Type *tn = t->nextOf())
{
tn = tn->toBasetype();
if (tn->ty == Tpointer || tn->ty == Tarray)
{
/* Accept immutable(T)* and immutable(T)[] as being strongly pure
*/
if (tn->mod & MODimmutable)
continue;
if (tn->mod & (MODconst | MODwild))
{
tf->purity = PUREconst;
continue;
}
}
}

/* Should catch delegates and function pointers, and fold in their purity
*/

Expand Down
14 changes: 14 additions & 0 deletions test/compilable/warn3882.d
Expand Up @@ -46,3 +46,17 @@ struct K12760
}
}

/******************************************/
// 12909

int f12909(immutable(int[])[int] aa) pure nothrow
{
aa[0] = [];
return 0;
}

void test12909()
{
immutable(int[])[int] aa;
f12909(aa);
}
20 changes: 20 additions & 0 deletions test/fail_compilation/fail3882.d
Expand Up @@ -26,3 +26,23 @@ void main()
auto fp = &strictlyPure!int;
fp(x);
}

/******************************************/
// bugfix in TypeFunction::purityLevel

/*
TEST_OUTPUT:
---
fail_compilation/fail3882.d(46): Warning: calling fail3882.f1 without side effects discards return value of type int, prepend a cast(void) if intentional
fail_compilation/fail3882.d(47): Warning: calling fail3882.f2 without side effects discards return value of type int, prepend a cast(void) if intentional
---
*/

nothrow pure int f1(immutable(int)[] a) { return 0; }
nothrow pure int f2(immutable(int)* p) { return 0; }

void test_bug()
{
f1([]);
f2(null);
}

0 comments on commit 20feb28

Please sign in to comment.