From 727233c9c5c1d81d13527f74b0a640f546f28a6c Mon Sep 17 00:00:00 2001 From: Daniel Murphy Date: Tue, 27 Jan 2015 21:35:32 +1100 Subject: [PATCH] Fix Issue 4733 - Possible bugs caused by dynamic arrays in boolean evaluation context Turn it into a warning --- src/expression.c | 5 +++++ test/fail_compilation/warn4733.d | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 test/fail_compilation/warn4733.d diff --git a/src/expression.c b/src/expression.c index b654d4a2dc0f..c15c4f6c8b91 100644 --- a/src/expression.c +++ b/src/expression.c @@ -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; } diff --git a/test/fail_compilation/warn4733.d b/test/fail_compilation/warn4733.d new file mode 100644 index 000000000000..368a25923f24 --- /dev/null +++ b/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) {} +}