The following testcase ICEs, because grok_array_decl during
processing_template_decl handling of a non-dependent subscript
emits a -Wcomma-subscript pedwarn, we decide to pass to the
single index argument the index expressions as if it was wrapped
with () around it, but then when preparing it for later instantiation
we don't actually take that into account and ICE on a mismatch of
number of index arguments (the overload expects a single index,
testcase has two index expressions in this case).
For non-dependent subscript which are builtin subscripts we also
emit the same pedwarn and don't ICE, but emit the same pedwarn
again whenever we instantiate it, which is also IMHO undesirable,
it is enough to warn once during parsing the template.
The following patch fixes it by turning even the original index expressions
(those which didn't go through make_args_non_dependent) into a single
index using comma expression(s).
2023-03-30 Jakub Jelinek <jakub@redhat.com>
PR c++/109319
* decl2.cc (grok_array_decl): After emitting a pedwarn for
-Wcomma-subscript, if processing_template_decl set orig_index_exp
to compound expr from orig_index_exp_list.
* g++.dg/cpp23/subscript14.C: New test.
(cherry picked from commit
c016887c91a79d67b6a3c7e19b9219f5ab1e2a4d)
&overload, complain);
}
else
- /* If it would be valid albeit deprecated expression in C++20,
- just pedwarn on it and treat it as if wrapped in (). */
- pedwarn (loc, OPT_Wcomma_subscript,
- "top-level comma expression in array subscript "
- "changed meaning in C++23");
+ {
+ /* If it would be valid albeit deprecated expression in
+ C++20, just pedwarn on it and treat it as if wrapped
+ in (). */
+ pedwarn (loc, OPT_Wcomma_subscript,
+ "top-level comma expression in array subscript "
+ "changed meaning in C++23");
+ if (processing_template_decl)
+ {
+ orig_index_exp
+ = build_x_compound_expr_from_vec (orig_index_exp_list,
+ NULL, complain);
+ if (orig_index_exp == error_mark_node)
+ expr = error_mark_node;
+ release_tree_vector (orig_index_exp_list);
+ }
+ }
}
}
}
return error_mark_node;
}
index_exp = idx;
+ if (processing_template_decl)
+ {
+ orig_index_exp
+ = build_x_compound_expr_from_vec (orig_index_exp_list,
+ NULL, complain);
+ release_tree_vector (orig_index_exp_list);
+ if (orig_index_exp == error_mark_node)
+ return error_mark_node;
+ }
}
if (TREE_CODE (TREE_TYPE (index_exp)) == ARRAY_TYPE)
--- /dev/null
+// PR c++/109319
+// { dg-do compile { target c++11 } }
+// { dg-options "" }
+
+struct B { int &operator[] (int x) { static int b[2]; return b[x]; } };
+int c[2];
+
+template <typename U, typename V>
+int
+foo ()
+{
+ B b;
+ ++b[0, 1]; // { dg-warning "top-level comma expression in array subscript changed meaning" "" { target c++23 } }
+ // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_only } .-1 }
+ ++c[0, 1]; // { dg-warning "top-level comma expression in array subscript changed meaning" "" { target c++23 } }
+ U e; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_only } .-1 }
+ ++e[0, 1]; // { dg-warning "top-level comma expression in array subscript changed meaning" "" { target c++23 } }
+ extern V f[2]; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_only } .-1 }
+ ++f[0, 1]; // { dg-warning "top-level comma expression in array subscript changed meaning" "" { target c++23 } }
+ return 0; // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_only } .-1 }
+}
+
+int f[2];
+
+int
+main ()
+{
+ B b;
+ ++b[0, 1]; // { dg-warning "top-level comma expression in array subscript changed meaning" "" { target c++23 } }
+ // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_only } .-1 }
+ ++c[0, 1]; // { dg-warning "top-level comma expression in array subscript changed meaning" "" { target c++23 } }
+ foo <B, int> (); // { dg-warning "top-level comma expression in array subscript is deprecated" "" { target c++20_only } .-1 }
+ if (b.operator[] (1) != 3 || c[1] != 2 || f[1] != 1)
+ __builtin_abort ();
+}