/* Warn about C++20 [depr.array.comp] array comparisons: "Equality
and relational comparisons between two operands of array type are
- deprecated." We also warn in C and earlier C++ standards. CODE is
- the code for this comparison, OP0 and OP1 are the operands. */
+ deprecated." In C++26 this is a permerror. We also warn in C and earlier
+ C++ standards. CODE is the code for this comparison, OP0 and OP1 are
+ the operands. */
void
do_warn_array_compare (location_t location, tree_code code, tree op0, tree op1)
op1 = TREE_OPERAND (op1, 0);
auto_diagnostic_group d;
- if (warning_at (location, OPT_Warray_compare,
- (c_dialect_cxx () && cxx_dialect >= cxx20)
- ? G_("comparison between two arrays is deprecated in C++20")
- : G_("comparison between two arrays")))
+ diagnostic_t kind = DK_WARNING;
+ const char *msg;
+ if (c_dialect_cxx () && cxx_dialect >= cxx20)
+ {
+ /* P2865R5 made this comparison ill-formed in C++26. */
+ if (cxx_dialect >= cxx26)
+ {
+ msg = G_("comparison between two arrays is not allowed in C++26");
+ kind = DK_PERMERROR;
+ }
+ else
+ msg = G_("comparison between two arrays is deprecated in C++20");
+ }
+ else
+ msg = G_("comparison between two arrays");
+ if (emit_diagnostic (kind, location, OPT_Warray_compare, msg))
{
/* C doesn't allow +arr. */
if (c_dialect_cxx ())
warning_at (location, OPT_Wfloat_equal,
"comparing floating-point with %<==%> "
"or %<!=%> is unsafe");
- if (complain & tf_warning)
- {
- tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
- tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
- if ((TREE_CODE (stripped_orig_op0) == STRING_CST
- && !integer_zerop (cp_fully_fold (op1)))
- || (TREE_CODE (stripped_orig_op1) == STRING_CST
- && !integer_zerop (cp_fully_fold (op0))))
- warning_at (location, OPT_Waddress,
- "comparison with string literal results in "
- "unspecified behavior");
- else if (warn_array_compare
- && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
- && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
- do_warn_array_compare (location, code, stripped_orig_op0,
- stripped_orig_op1);
- }
+ {
+ tree stripped_orig_op0 = tree_strip_any_location_wrapper (orig_op0);
+ tree stripped_orig_op1 = tree_strip_any_location_wrapper (orig_op1);
+ if ((complain & tf_warning_or_error)
+ && ((TREE_CODE (stripped_orig_op0) == STRING_CST
+ && !integer_zerop (cp_fully_fold (op1)))
+ || (TREE_CODE (stripped_orig_op1) == STRING_CST
+ && !integer_zerop (cp_fully_fold (op0)))))
+ warning_at (location, OPT_Waddress,
+ "comparison with string literal results in "
+ "unspecified behavior");
+ else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
+ && TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE)
+ {
+ /* P2865R5 made array comparisons ill-formed in C++26. */
+ if (complain & tf_warning_or_error)
+ do_warn_array_compare (location, code, stripped_orig_op0,
+ stripped_orig_op1);
+ else if (cxx_dialect >= cxx26)
+ return error_mark_node;
+ }
+ }
build_type = boolean_type_node;
if ((code0 == INTEGER_TYPE || code0 == REAL_TYPE
"comparison with string literal results "
"in unspecified behavior");
}
- else if (warn_array_compare
- && TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
+ else if (TREE_CODE (TREE_TYPE (orig_op0)) == ARRAY_TYPE
&& TREE_CODE (TREE_TYPE (orig_op1)) == ARRAY_TYPE
- && code != SPACESHIP_EXPR
- && (complain & tf_warning))
- do_warn_array_compare (location, code,
- tree_strip_any_location_wrapper (orig_op0),
- tree_strip_any_location_wrapper (orig_op1));
+ && code != SPACESHIP_EXPR)
+ {
+ if (complain & tf_warning_or_error)
+ do_warn_array_compare (location, code,
+ tree_strip_any_location_wrapper (orig_op0),
+ tree_strip_any_location_wrapper (orig_op1));
+ else if (cxx_dialect >= cxx26)
+ return error_mark_node;
+ }
if (gnu_vector_type_p (type0) && gnu_vector_type_p (type1))
{
bool
g ()
{
- bool b = arr1 == arr2; /* { dg-warning "comparison between two arrays" } */
- b &= arr1 != arr2; /* { dg-warning "comparison between two arrays" } */
- b &= arr1 > arr2; /* { dg-warning "comparison between two arrays" } */
- b &= arr1 >= arr2; /* { dg-warning "comparison between two arrays" } */
- b &= arr1 < arr2; /* { dg-warning "comparison between two arrays" } */
- b &= arr1 <= arr2; /* { dg-warning "comparison between two arrays" } */
+ bool b = arr1 == arr2; /* { dg-warning "comparison between two arrays" "" { target { c || c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
+ b &= arr1 != arr2; /* { dg-warning "comparison between two arrays" "" { target { c || c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
+ b &= arr1 > arr2; /* { dg-warning "comparison between two arrays" "" { target { c || c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
+ b &= arr1 >= arr2; /* { dg-warning "comparison between two arrays" "" { target { c || c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
+ b &= arr1 < arr2; /* { dg-warning "comparison between two arrays" "" { target { c || c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
+ b &= arr1 <= arr2; /* { dg-warning "comparison between two arrays" "" { target { c || c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
#ifdef __cplusplus
b &= +arr1 == +arr2;
b &= +arr1 != +arr2;
b &= &arr1[0] < &arr2[0];
b &= &arr1[0] <= &arr2[0];
- b &= arr3 == arr4; /* { dg-warning "comparison between two arrays" } */
+ b &= arr3 == arr4; /* { dg-warning "comparison between two arrays" "" { target { c || c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
#if defined(__cplusplus) && __cplusplus > 201703L
auto cmp = arr1 <=> arr2; /* { dg-error "invalid operands" "" { target c++20 } } */
int
foo (int x, int y)
{
- return (x ? a : b) == (y ? a : b); /* { dg-warning "comparison between two arrays" } */
-/* { dg-message "use '&\\\(\[^\n\r]*\\\)\\\[0\\\] == &\\\(\[^\n\r]*\\\)\\\[0\\\]' to compare the addresses" "" { target c } .-1 } */
-/* { dg-message "use unary '\\\+' which decays operands to pointers or '&\\\(\[^\n\r]*\\\)\\\[0\\\] == &\\\(\[^\n\r]*\\\)\\\[0\\\]' to compare the addresses" "" { target c++ } .-2 } */
+ return (x ? a : b) == (y ? a : b); /* { dg-warning "comparison between two arrays" "" { target { c || c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
+/* { dg-message "use '&\\\(\[^\n\r]*\\\)\\\[0\\\] == &\\\(\[^\n\r]*\\\)\\\[0\\\]' to compare the addresses" "" { target c } .-2 } */
+/* { dg-message "use unary '\\\+' which decays operands to pointers or '&\\\(\[^\n\r]*\\\)\\\[0\\\] == &\\\(\[^\n\r]*\\\)\\\[0\\\]' to compare the addresses" "" { target c++ } .-3 } */
}
--- /dev/null
+/* PR c++/97573 */
+/* { dg-do compile } */
+
+#ifndef __cplusplus
+# define bool _Bool
+#endif
+
+int arr1[5];
+int arr2[5];
+int arr3[2][2];
+int arr4[2][2];
+
+bool
+g ()
+{
+ bool b = arr1 == arr2; /* { dg-warning "comparison between two arrays" "" { target { c++20 && c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
+ b &= arr1 != arr2; /* { dg-warning "comparison between two arrays" "" { target { c++20 && c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
+ b &= arr1 > arr2; /* { dg-warning "comparison between two arrays" "" { target { c++20 && c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
+ b &= arr1 >= arr2; /* { dg-warning "comparison between two arrays" "" { target { c++20 && c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
+ b &= arr1 < arr2; /* { dg-warning "comparison between two arrays" "" { target { c++20 && c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
+ b &= arr1 <= arr2; /* { dg-warning "comparison between two arrays" "" { target { c++20 && c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
+#ifdef __cplusplus
+ b &= +arr1 == +arr2;
+ b &= +arr1 != +arr2;
+ b &= +arr1 > +arr2;
+ b &= +arr1 >= +arr2;
+ b &= +arr1 < +arr2;
+ b &= +arr1 <= +arr2;
+#endif
+ b &= &arr1[0] == &arr2[0];
+ b &= &arr1[0] != &arr2[0];
+ b &= &arr1[0] > &arr2[0];
+ b &= &arr1[0] >= &arr2[0];
+ b &= &arr1[0] < &arr2[0];
+ b &= &arr1[0] <= &arr2[0];
+
+ b &= arr3 == arr4; /* { dg-warning "comparison between two arrays" "" { target { c++20 && c++23_down } } } */
+/* { dg-error "comparison between two arrays" "" { target c++26 } .-1 } */
+
+#if defined(__cplusplus) && __cplusplus > 201703L
+ auto cmp = arr1 <=> arr2; /* { dg-error "invalid operands" "" { target c++20 } } */
+#endif
+ return b;
+}
--- /dev/null
+/* PR c++/97573 */
+/* { dg-do compile } */
+/* { dg-options "-Wall -Wno-error=array-compare" } */
+
+#ifndef __cplusplus
+# define bool _Bool
+#endif
+
+int arr1[5];
+int arr2[5];
+int arr3[2][2];
+int arr4[2][2];
+
+bool
+g ()
+{
+ bool b = arr1 == arr2; /* { dg-warning "comparison between two arrays" } */
+ b &= arr1 != arr2; /* { dg-warning "comparison between two arrays" } */
+ b &= arr1 > arr2; /* { dg-warning "comparison between two arrays" } */
+ b &= arr1 >= arr2; /* { dg-warning "comparison between two arrays" } */
+ b &= arr1 < arr2; /* { dg-warning "comparison between two arrays" } */
+ b &= arr1 <= arr2; /* { dg-warning "comparison between two arrays" } */
+#ifdef __cplusplus
+ b &= +arr1 == +arr2;
+ b &= +arr1 != +arr2;
+ b &= +arr1 > +arr2;
+ b &= +arr1 >= +arr2;
+ b &= +arr1 < +arr2;
+ b &= +arr1 <= +arr2;
+#endif
+ b &= &arr1[0] == &arr2[0];
+ b &= &arr1[0] != &arr2[0];
+ b &= &arr1[0] > &arr2[0];
+ b &= &arr1[0] >= &arr2[0];
+ b &= &arr1[0] < &arr2[0];
+ b &= &arr1[0] <= &arr2[0];
+
+ b &= arr3 == arr4; /* { dg-warning "comparison between two arrays" } */
+
+#if defined(__cplusplus) && __cplusplus > 201703L
+ auto cmp = arr1 <=> arr2; /* { dg-error "invalid operands" "" { target c++20 } } */
+#endif
+ return b;
+}
--- /dev/null
+// PR c++/117788
+// { dg-do compile { target c++11 } }
+
+constexpr int arr1[5]{};
+constexpr int arr2[5]{};
+
+template<int I>
+void f1 (int(*)[arr1 == arr2 ? I : I]) = delete; // { dg-warning "comparison between two arrays" "" { target { c++20 && c++23_down } } }
+// { dg-error "comparison between two arrays" "" { target c++26 } .-1 }
+
+template<int>
+void f1 (...) { }
+
+template<int I>
+void f2 (int(*)[arr1 > arr2 ? I : 1]) = delete; // { dg-warning "comparison between two arrays" "" { target { c++20 && c++23_down } } }
+// { dg-error "comparison between two arrays" "" { target c++26 } .-1 }
+
+template<int>
+void f2 (...) { }
+
+void
+g ()
+{
+ f1<0>(nullptr);
+ f2<0>(nullptr);
+}
test02()
{
std::pair<int, std::string_view> a[2]{ {1,"two"}, {3,"four"}};
- auto pos = ranges::find(a | views::values, "four");
+ // FIXME: We should be able to get rid of the decay via the + here.
+ // But we'd end up comparing two array types in equality_comparable_with
+ // -> __weakly_eq_cmp_with which is ill-formed in C++26 due to P2865.
+ auto pos = ranges::find(a | views::values, +"four");
VERIFY( *pos == "four" );
static_assert( ranges::borrowed_range<decltype(a | views::keys)> );