Skip to content

Commit

Permalink
Fix Issue 4733 - Possible bugs caused by dynamic arrays in boolean ev…
Browse files Browse the repository at this point in the history
…aluation context

Turn it into a warning
  • Loading branch information
yebblies committed Feb 2, 2015
1 parent 37e6395 commit 727233c
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/expression.c
Expand Up @@ -2662,6 +2662,11 @@ Expression *Expression::checkToBoolean(Scope *sc)
error("expression %s of type %s does not have a boolean value", toChars(), t->toChars());
return new ErrorExp();
}

if (tb->ty == Tarray)
warning("implicit conversion of dynamic arrays to bool can be ambiguous and will be deprecated. Use one of: %s !is null, %s.length, or %s.ptr instead",
toChars(), toChars(), toChars());

return e;
}

Expand Down
28 changes: 28 additions & 0 deletions test/fail_compilation/warn4733.d
@@ -0,0 +1,28 @@
/*
REQUIRED_ARGS: -w
PERMUTE_ARGS:
TEST_OUTPUT:
---
fail_compilation/warn4733.d(20): Warning: implicit conversion of dynamic arrays to bool can be ambiguous and will be deprecated. Use one of: arr !is null, arr.length, or arr.ptr instead
fail_compilation/warn4733.d(21): Warning: implicit conversion of dynamic arrays to bool can be ambiguous and will be deprecated. Use one of: arr !is null, arr.length, or arr.ptr instead
fail_compilation/warn4733.d(22): Warning: implicit conversion of dynamic arrays to bool can be ambiguous and will be deprecated. Use one of: arr !is null, arr.length, or arr.ptr instead
fail_compilation/warn4733.d(23): Warning: implicit conversion of dynamic arrays to bool can be ambiguous and will be deprecated. Use one of: arr !is null, arr.length, or arr.ptr instead
fail_compilation/warn4733.d(24): Warning: implicit conversion of dynamic arrays to bool can be ambiguous and will be deprecated. Use one of: arr !is null, arr.length, or arr.ptr instead
fail_compilation/warn4733.d(25): Warning: implicit conversion of dynamic arrays to bool can be ambiguous and will be deprecated. Use one of: arr !is null, arr.length, or arr.ptr instead
fail_compilation/warn4733.d(26): Warning: implicit conversion of dynamic arrays to bool can be ambiguous and will be deprecated. Use one of: arr !is null, arr.length, or arr.ptr instead
fail_compilation/warn4733.d(27): Warning: implicit conversion of dynamic arrays to bool can be ambiguous and will be deprecated. Use one of: arr !is null, arr.length, or arr.ptr instead
---
*/

void main()
{
int[] arr;
assert(arr);
assert(!arr);
assert(arr || 0);
assert(arr && 1);
assert(arr ? true : false);
do {} while(arr);
for (; arr;) {}
if (arr) {}
}

0 comments on commit 727233c

Please sign in to comment.