c++: Fix up handling of non-dependent subscript with static operator[] [PR108437]
authorJakub Jelinek <jakub@redhat.com>
Thu, 19 Jan 2023 22:31:15 +0000 (23:31 +0100)
committerJakub Jelinek <jakub@redhat.com>
Thu, 19 Jan 2023 22:31:15 +0000 (23:31 +0100)
commit86caab6c5d1e26e1c54c3dceacc873d6e27bfc09
tree1ceb7d654a64ea530709578cd1bbb50869c14394
parent9b9a989adc042b304572fd6d4ade46b47be6ccb8
c++: Fix up handling of non-dependent subscript with static operator[] [PR108437]

As the following testcases shows, when adding static operator[]
support I've missed that the 2 build_min_non_dep_op_overload functions
need to be adjusted.  The first one we only use for the single index
case, but as cp_tree_code_length (ARRAY_REF) is 2, we were running
into an assertion there which compared nargs and expected_nargs.
For ARRAY_REF, the operator[] is either a non-static member or newly
static member, never out of class and for the static member case
if user uses single index the operator[] needs to have a single
argument as well, but the function is called with 2 - the object
it is invoked on and the index.  We need to evaluate side-effects
of the object and use just a single argument in the call - the index.
The other build_min_non_dep_op_overload overload has been added
solely for ARRAY_REF - CALL_EXPR is the other operator that accepts
variable number of operands but that one goes through different
routines.  There we asserted it is a METHOD_TYPE, so again
we shouldn't assert that but handle the case when it is not one
by making sure object's side-effects are evaluated if needed and
passing all the index arguments to the static operator[].

2023-01-19  Jakub Jelinek  <jakub@redhat.com>

PR c++/108437
* cp-tree.h (keep_unused_object_arg): Declare.
* call.cc (keep_unused_object_arg): No longer static.
* tree.cc (build_min_non_dep_op_overload): Handle ARRAY_REF
with overload being static member function.

* g++.dg/cpp23/subscript12.C: New test.
* g++.dg/cpp23/subscript13.C: New test.
gcc/cp/call.cc
gcc/cp/cp-tree.h
gcc/cp/tree.cc
gcc/testsuite/g++.dg/cpp23/subscript12.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp23/subscript13.C [new file with mode: 0644]