Skip to content

Commit 4803978

Browse files
Diagnose comparisons between two arrays (P2865). [GH #370]
A comparison between two arrays with an equality or relational operator is now a discretionary error in C++26, a deprecation warning in C++20 and C++23, and a remark before C++20 (a warning when emulating Clang 20 or later). When emulating GCC before 15 or Clang before 20, C++26 gets only the warning. Class operands converted to arrays by a conversion function (including through an implicit qualification conversion) are covered too. Co-authored-by: Daveed Vandevoorde <dvandevoorde@nvidia.com>
1 parent 3c88dad commit 4803978

46 files changed

Lines changed: 1223 additions & 2 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/error_msg.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3680,5 +3680,8 @@ ec_interpolated_id_is_not_identifier;;"%sq does not spell an identifier"
36803680
ec_interpolated_operand_is_not_token_sequence;;"the operand of a \"\\{...}\" interpolator is not a token sequence"
36813681
ec_tag_redefined_differently;;"this declaration of %n does not declare the same type as the declaration %p"
36823682
ec_bad_gcc_unroll_pragma_value;;"the argument of \"#pragma GCC unroll\" must be a non-negative integral constant less than 65535"
3683+
ec_array_comparison_deprecated;;"comparison between two arrays is deprecated"
3684+
ec_array_comparison_not_allowed;;"comparison between two arrays is not allowed"
3685+
ec_array_comparison_compares_addresses;;"comparison between two arrays compares their addresses"
36833686
###############################################################################
36843687

‎src/expr.c‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32487,6 +32487,63 @@ different enum types (or, in C, have different affiliated enum types).
3248732487
} /* diagnose_comparison_if_different_enum_types */
3248832488

3248932489

32490+
static a_boolean is_array_comparison_operand(an_operand *opnd)
32491+
/*
32492+
Return TRUE if opnd, an operand of a built-in equality or relational operator,
32493+
is an array: either it has array type, or it is a class operand that overload
32494+
resolution converted to an array and then to a pointer (e.g., through a
32495+
conversion function returning a reference to an array), possibly followed by
32496+
an implicit qualification conversion.
32497+
*/
32498+
{
32499+
a_boolean is_array = is_array_type(opnd->type);
32500+
32501+
if (!is_array && is_expression_operand(opnd)) {
32502+
an_expr_node_ptr expr = opnd->variant.expression;
32503+
/* Skip implicit pointer conversions (e.g., "int *" to "const int *");
32504+
a cast written in the source ends the search. */
32505+
while (is_cast_operation_node(expr) && expr->compiler_generated &&
32506+
is_pointer_type(expr->type)) {
32507+
expr = expr->variant.operation.operands;
32508+
} /* while */
32509+
is_array = is_operation_node(expr) &&
32510+
node_operator_is(expr, eok_array_to_pointer);
32511+
} /* if */
32512+
return is_array;
32513+
} /* is_array_comparison_operand */
32514+
32515+
32516+
static void check_for_array_comparison(an_operand *opnd1,
32517+
an_operand *opnd2,
32518+
a_source_position *operator_pos)
32519+
/*
32520+
opnd1 and opnd2 are the operands of a built-in equality or relational
32521+
operator at operator_pos, before the array-to-pointer conversion. If both
32522+
are arrays, issue a diagnostic: such comparisons are deprecated in C++20 and
32523+
not allowed in C++26 (P2865). Before C++20, issue a remark (a warning when
32524+
emulating Clang 20 or later, which warns by default).
32525+
*/
32526+
{
32527+
if (!C_mode() && is_array_comparison_operand(opnd1) &&
32528+
is_array_comparison_operand(opnd2)) {
32529+
if (!cpp20_mode) {
32530+
expr_pos_diagnostic(clangcpp_version_is(>=200000) ? es_warning
32531+
: es_remark,
32532+
ec_array_comparison_compares_addresses,
32533+
operator_pos);
32534+
} else if (cpp26_mode && !gpp_version_is(<150000) &&
32535+
!clangcpp_version_is(<200000)) {
32536+
/* GCC 15 and Clang 20 implement P2865. Earlier versions only issue a
32537+
deprecation warning. */
32538+
expr_pos_diagnostic(es_discretionary_error,
32539+
ec_array_comparison_not_allowed, operator_pos);
32540+
} else {
32541+
expr_pos_warning(ec_array_comparison_deprecated, operator_pos);
32542+
} /* if */
32543+
} /* if */
32544+
} /* check_for_array_comparison */
32545+
32546+
3249032547
static void process_rel_operator(an_operand *opnd1,
3249132548
an_operand *opnd2,
3249232549
a_token_kind operator_token,
@@ -32532,6 +32589,7 @@ describe the location of the operator.
3253232589
} /* if */
3253332590
if (!processed) {
3253432591
/* Non-operator-function cases. */
32592+
check_for_array_comparison(opnd1, opnd2, operator_pos);
3253532593
/* The first operand must be an arithmetic or enum type or a pointer. */
3253632594
do_operand_transformations(opnd1, TOPT_NO_OPTIONS);
3253732595
opnd1_is_pointer = FALSE;
@@ -33036,6 +33094,7 @@ operator_tsn describe the location of the operator.
3303633094
} /* if */
3303733095
if (!processed) {
3303833096
/* Non-operator-function cases. */
33097+
check_for_array_comparison(opnd1, opnd2, operator_pos);
3303933098
/* The first operand must be an arithmetic or enum type or a pointer. */
3304033099
do_operand_transformations(opnd1, TOPT_NO_OPTIONS);
3304133100
if (is_arithmetic_or_enum_type(opnd1->type)) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[GH #370]
2+
C++26: comparison between two arrays is not allowed
3+
4+
A comparison between two arrays using an equality or relational operator is
5+
deprecated in C++20 and not allowed in C++26 (P2865). The front end did not
6+
diagnose it. It is now a warning in C++20 and C++23, and an error in C++26
7+
(only a warning when emulating GCC versions before 15 or Clang versions before
8+
20, which do the same). Before C++20 it is a remark (a warning when
9+
emulating Clang 20 or later, which warns by default). This includes class
10+
operands that are converted to arrays by a conversion function. For
11+
example, with --c++26:
12+
13+
int a[2], b[2];
14+
bool x = a == b; // Now an error.
15+
bool y = a == &b[0]; // Okay: one operand is a pointer.
16+
struct C { using A = int[2]; operator A &() const; } c;
17+
bool z = c == c; // Now an error.

‎tests/expectations/edg_x86_64/imported.log‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,7 +1440,6 @@ clang/cpp/SemaCXX/vtordisp-mode.sft.cpp:fp:-DTEST_NUMBER=3 --c++11 -DVTORDISP_MO
14401440
clang/cpp/SemaCXX/warn-array-comparion.sft.cpp:fn:-DTEST_NUMBER=1 --c++17:FAIL:
14411441
clang/cpp/SemaCXX/warn-array-comparion.sft.cpp:fn:-DTEST_NUMBER=2 --c++20:FAIL:
14421442
clang/cpp/SemaCXX/warn-array-comparion.sft.cpp:fn:-DTEST_NUMBER=3 --c++20:FAIL:
1443-
clang/cpp/SemaCXX/warn-array-comparion.sft.cpp:fn:-DTEST_NUMBER=4 --c++26:FAIL:
14441443
clang/cpp/SemaCXX/warn-bool-conversion.sft.cpp:fn:-DTEST_NUMBER=1 --c++03:FAIL:
14451444
clang/cpp/SemaCXX/warn-cast-function-type-strict.sft.cpp:fp:-DTEST_NUMBER=1:FAIL:
14461445
clang/cpp/SemaCXX/warn-cast-function-type.sft.cpp:fp:-DTEST_NUMBER=1:FAIL:
@@ -1452,7 +1451,6 @@ clang/cpp/SemaCXX/warn-memsize-comparison.sft.cpp:fn:-DTEST_NUMBER=1:FAIL:
14521451
clang/cpp/SemaCXX/warn-nrvo.sft.cpp:fp:-DTEST_NUMBER=1:FAIL:
14531452
clang/cpp/SemaCXX/warn-self-comparisons.sft.cpp:fn:-DTEST_NUMBER=1 --c++17:FAIL:
14541453
clang/cpp/SemaCXX/warn-self-comparisons.sft.cpp:fn:-DTEST_NUMBER=2 --c++20:FAIL:
1455-
clang/cpp/SemaCXX/warn-self-comparisons.sft.cpp:fn:-DTEST_NUMBER=3 --c++26:FAIL:
14561454
clang/cpp/SemaCXX/warn-static-const-float.sft.cpp:fn:-DTEST_NUMBER=1 --c++03 -DEXT --c++03 -DNONE --c++03 -DNONE --c++03 -DNONE:FAIL:
14571455
clang/cpp/SemaCXX/warn-static-const-float.sft.cpp:fn:-DTEST_NUMBER=2 --c++11 -DERR:FAIL:
14581456
clang/cpp/SemaCXX/warn-static-const-float.sft.cpp:fn:-DTEST_NUMBER=3 --c++11 -DERR:FAIL:
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
fe_only -DTEST_NUMBER=1 --c++26 Test_name.c
2+
"Test_name.c", line 10: error: comparison between two arrays is not allowed
3+
bool x1 = a == b; // Two arrays: diagnosed.
4+
^
5+
6+
"Test_name.c", line 11: error: comparison between two arrays is not allowed
7+
bool x2 = a != b;
8+
^
9+
10+
"Test_name.c", line 12: error: comparison between two arrays is not allowed
11+
bool x3 = a < b;
12+
^
13+
14+
"Test_name.c", line 13: error: comparison between two arrays is not allowed
15+
bool x4 = a >= b;
16+
^
17+
18+
"Test_name.c", line 22: error: comparison between two arrays is not allowed
19+
bool z1 = c == c; // Two arrays: diagnosed.
20+
^
21+
22+
"Test_name.c", line 23: error: comparison between two arrays is not allowed
23+
bool z2 = c < c;
24+
^
25+
26+
"Test_name.c", line 24: error: comparison between two arrays is not allowed
27+
bool z3 = c == a;
28+
^
29+
30+
"Test_name.c", line 33: error: comparison between two arrays is not allowed
31+
bool h1(C c) { return c == ca; } // Two arrays: diagnosed.
32+
^
33+
34+
"Test_name.c", line 37: error: comparison between two arrays is not allowed
35+
bool s1 = "abc" == "abc";
36+
^
37+
38+
"Test_name.c", line 41: error: comparison between two arrays is not allowed
39+
template<class T> bool t1(T) { return a == b; }
40+
^
41+
42+
"Test_name.c", line 44: error: comparison between two arrays is not allowed
43+
template<class T> bool t2(T &x, T &y) { return x == y; }
44+
^
45+
detected during instantiation of "bool t2(T &, T &) [with T=int [2]]" at line 45
46+
47+
"Test_name.c", line 47: error: comparison between two arrays is not allowed
48+
template<int N> bool t3(int (&x)[N], int (&y)[N]) { return x < y; }
49+
^
50+
detected during instantiation of "bool t3(int (&)[N], int (&)[N]) [with N=2]" at line 48
51+
52+
12 errors detected in the compilation of "Test_name.c".
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
fe_only -DTEST_NUMBER=2 --c++26 --gnu_version 150000 Test_name.c
2+
"Test_name.c", line 10: error: comparison between two arrays is not allowed
3+
bool x1 = a == b; // Two arrays: diagnosed.
4+
^
5+
6+
"Test_name.c", line 11: error: comparison between two arrays is not allowed
7+
bool x2 = a != b;
8+
^
9+
10+
"Test_name.c", line 12: error: comparison between two arrays is not allowed
11+
bool x3 = a < b;
12+
^
13+
14+
"Test_name.c", line 13: error: comparison between two arrays is not allowed
15+
bool x4 = a >= b;
16+
^
17+
18+
"Test_name.c", line 22: error: comparison between two arrays is not allowed
19+
bool z1 = c == c; // Two arrays: diagnosed.
20+
^
21+
22+
"Test_name.c", line 23: error: comparison between two arrays is not allowed
23+
bool z2 = c < c;
24+
^
25+
26+
"Test_name.c", line 24: error: comparison between two arrays is not allowed
27+
bool z3 = c == a;
28+
^
29+
30+
"Test_name.c", line 33: error: comparison between two arrays is not allowed
31+
bool h1(C c) { return c == ca; } // Two arrays: diagnosed.
32+
^
33+
34+
"Test_name.c", line 37: error: comparison between two arrays is not allowed
35+
bool s1 = "abc" == "abc";
36+
^
37+
38+
"Test_name.c", line 41: error: comparison between two arrays is not allowed
39+
template<class T> bool t1(T) { return a == b; }
40+
^
41+
42+
"Test_name.c", line 44: error: comparison between two arrays is not allowed
43+
template<class T> bool t2(T &x, T &y) { return x == y; }
44+
^
45+
detected during instantiation of "bool t2(T &, T &) [with T=int [2]]" at line 45
46+
47+
"Test_name.c", line 47: error: comparison between two arrays is not allowed
48+
template<int N> bool t3(int (&x)[N], int (&y)[N]) { return x < y; }
49+
^
50+
detected during instantiation of "bool t3(int (&)[N], int (&)[N]) [with N=2]" at line 48
51+
52+
12 errors detected in the compilation of "Test_name.c".
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
fe_only -DTEST_NUMBER=3 --c++26 --clang_version 200000 Test_name.c
2+
"Test_name.c", line 10: error: comparison between two arrays is not allowed
3+
bool x1 = a == b; // Two arrays: diagnosed.
4+
^
5+
6+
"Test_name.c", line 11: error: comparison between two arrays is not allowed
7+
bool x2 = a != b;
8+
^
9+
10+
"Test_name.c", line 12: error: comparison between two arrays is not allowed
11+
bool x3 = a < b;
12+
^
13+
14+
"Test_name.c", line 13: error: comparison between two arrays is not allowed
15+
bool x4 = a >= b;
16+
^
17+
18+
"Test_name.c", line 22: error: comparison between two arrays is not allowed
19+
bool z1 = c == c; // Two arrays: diagnosed.
20+
^
21+
22+
"Test_name.c", line 23: error: comparison between two arrays is not allowed
23+
bool z2 = c < c;
24+
^
25+
26+
"Test_name.c", line 24: error: comparison between two arrays is not allowed
27+
bool z3 = c == a;
28+
^
29+
30+
"Test_name.c", line 33: error: comparison between two arrays is not allowed
31+
bool h1(C c) { return c == ca; } // Two arrays: diagnosed.
32+
^
33+
34+
"Test_name.c", line 37: error: comparison between two arrays is not allowed
35+
bool s1 = "abc" == "abc";
36+
^
37+
38+
"Test_name.c", line 41: error: comparison between two arrays is not allowed
39+
template<class T> bool t1(T) { return a == b; }
40+
^
41+
42+
"Test_name.c", line 44: error: comparison between two arrays is not allowed
43+
template<class T> bool t2(T &x, T &y) { return x == y; }
44+
^
45+
detected during instantiation of "bool t2(T &, T &) [with T=int [2]]" at line 45
46+
47+
"Test_name.c", line 47: error: comparison between two arrays is not allowed
48+
template<int N> bool t3(int (&x)[N], int (&y)[N]) { return x < y; }
49+
^
50+
detected during instantiation of "bool t3(int (&)[N], int (&)[N]) [with N=2]" at line 48
51+
52+
12 errors detected in the compilation of "Test_name.c".
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
fe_only -DTEST_NUMBER=1 --c++20 Test_name.c
2+
"Test_name.c", line 13: warning: comparison between two arrays is deprecated
3+
bool x1 = a == b; // Two arrays: diagnosed.
4+
^
5+
6+
"Test_name.c", line 14: warning: comparison between two arrays is deprecated
7+
bool x2 = a != b;
8+
^
9+
10+
"Test_name.c", line 15: warning: comparison between two arrays is deprecated
11+
bool x3 = a < b;
12+
^
13+
14+
"Test_name.c", line 16: warning: comparison between two arrays is deprecated
15+
bool x4 = a >= b;
16+
^
17+
18+
"Test_name.c", line 25: warning: comparison between two arrays is deprecated
19+
bool z1 = c == c; // Two arrays: diagnosed.
20+
^
21+
22+
"Test_name.c", line 26: warning: comparison between two arrays is deprecated
23+
bool z2 = c < c;
24+
^
25+
26+
"Test_name.c", line 27: warning: comparison between two arrays is deprecated
27+
bool z3 = c == a;
28+
^
29+
30+
"Test_name.c", line 36: warning: comparison between two arrays is deprecated
31+
bool h1(C c) { return c == ca; } // Two arrays: diagnosed.
32+
^
33+
34+
"Test_name.c", line 40: warning: comparison between two arrays is deprecated
35+
bool s1 = "abc" == "abc";
36+
^
37+
38+
"Test_name.c", line 44: warning: comparison between two arrays is deprecated
39+
template<class T> bool t1(T) { return a == b; }
40+
^
41+
42+
"Test_name.c", line 47: warning: comparison between two arrays is deprecated
43+
template<class T> bool t2(T &x, T &y) { return x == y; }
44+
^
45+
detected during instantiation of "bool t2(T &, T &) [with T=int [2]]" at line 48
46+
47+
"Test_name.c", line 50: warning: comparison between two arrays is deprecated
48+
template<int N> bool t3(int (&x)[N], int (&y)[N]) { return x < y; }
49+
^
50+
detected during instantiation of "bool t3(int (&)[N], int (&)[N]) [with N=2]" at line 51
51+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
fe_only -DTEST_NUMBER=2 --c++26 --gnu_version 140000 Test_name.c
2+
"Test_name.c", line 13: warning: comparison between two arrays is deprecated
3+
bool x1 = a == b; // Two arrays: diagnosed.
4+
^
5+
6+
"Test_name.c", line 14: warning: comparison between two arrays is deprecated
7+
bool x2 = a != b;
8+
^
9+
10+
"Test_name.c", line 15: warning: comparison between two arrays is deprecated
11+
bool x3 = a < b;
12+
^
13+
14+
"Test_name.c", line 16: warning: comparison between two arrays is deprecated
15+
bool x4 = a >= b;
16+
^
17+
18+
"Test_name.c", line 25: warning: comparison between two arrays is deprecated
19+
bool z1 = c == c; // Two arrays: diagnosed.
20+
^
21+
22+
"Test_name.c", line 26: warning: comparison between two arrays is deprecated
23+
bool z2 = c < c;
24+
^
25+
26+
"Test_name.c", line 27: warning: comparison between two arrays is deprecated
27+
bool z3 = c == a;
28+
^
29+
30+
"Test_name.c", line 36: warning: comparison between two arrays is deprecated
31+
bool h1(C c) { return c == ca; } // Two arrays: diagnosed.
32+
^
33+
34+
"Test_name.c", line 40: warning: comparison between two arrays is deprecated
35+
bool s1 = "abc" == "abc";
36+
^
37+
38+
"Test_name.c", line 44: warning: comparison between two arrays is deprecated
39+
template<class T> bool t1(T) { return a == b; }
40+
^
41+
42+
"Test_name.c", line 47: warning: comparison between two arrays is deprecated
43+
template<class T> bool t2(T &x, T &y) { return x == y; }
44+
^
45+
detected during instantiation of "bool t2(T &, T &) [with T=int [2]]" at line 48
46+
47+
"Test_name.c", line 50: warning: comparison between two arrays is deprecated
48+
template<int N> bool t3(int (&x)[N], int (&y)[N]) { return x < y; }
49+
^
50+
detected during instantiation of "bool t3(int (&)[N], int (&)[N]) [with N=2]" at line 51
51+

0 commit comments

Comments
 (0)