https://gcc.gnu.org/g:ba5a6787374dea3e90f09771134d16b9f6d2714e
commit r16-2519-gba5a6787374dea3e90f09771134d16b9f6d2714e Author: Patrick Palka <ppa...@redhat.com> Date: Fri Jul 25 10:55:35 2025 -0400 c++: more name lookup for non-dep rewritten cmp ops As a follow-up to r16-2448-g7590c14b53a762, this patch attempts to teach build_min_non_dep_op_overload how to rebuild all rewritten comparison operators, not just != -> == ones, so that we don't incorrectly repeat the unqualified name lookup at instantiation time. Note that changes how we mangle such comparison ops, in a way that is overall more consistent with how we've been mangling non-rewritten operators, see PR c++/121239. gcc/cp/ChangeLog: * call.cc (build_new_op): If the selected candidate is rewritten, communicate the LOOKUP_REWRITTEN/REVERSED flags to the caller via the 'overload' out-parameter, and stop clearing '*overload' in that case. * tree.cc (build_min_non_dep_op_overload): Handle rebuilding all C++20 rewritten comparison operator expressions. gcc/testsuite/ChangeLog: * g++.dg/lookup/operator-8.C: Remove XFAILs and properly suppress all -Wunused-result warnings. Reviewed-by: Jason Merrill <ja...@redhat.com> Diff: --- gcc/cp/call.cc | 16 +++--- gcc/cp/tree.cc | 85 +++++++++++++++++++++++++++----- gcc/testsuite/g++.dg/lookup/operator-8.C | 16 +++--- 3 files changed, 92 insertions(+), 25 deletions(-) diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index c76b15bcef6b..0f3143375261 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -7491,7 +7491,16 @@ build_new_op (const op_location_t &loc, enum tree_code code, int flags, else if (TREE_CODE (cand->fn) == FUNCTION_DECL) { if (overload) - *overload = cand->fn; + { + if (cand->rewritten ()) + /* build_min_non_dep_op_overload needs to know whether the + candidate is rewritten/reversed. */ + *overload = build_tree_list (build_int_cst (integer_type_node, + cand->flags), + cand->fn); + else + *overload = cand->fn; + } if (resolve_args (arglist, complain) == NULL) result = error_mark_node; @@ -7540,11 +7549,6 @@ build_new_op (const op_location_t &loc, enum tree_code code, int flags, /* If this was a C++20 rewritten comparison, adjust the result. */ if (cand->rewritten ()) { - /* FIXME build_min_non_dep_op_overload can't handle rewrites. */ - if (code == NE_EXPR && !cand->reversed ()) - /* It can handle != rewritten to == though. */; - else if (overload) - *overload = NULL_TREE; switch (code) { case EQ_EXPR: diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index c260efb7f6ba..d56d73fe2179 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -3696,7 +3696,58 @@ build_min_non_dep_op_overload (enum tree_code op, int nargs, expected_nargs; tree fn, call, obj = NULL_TREE; - bool negated = (TREE_CODE (non_dep) == TRUTH_NOT_EXPR); + releasing_vec args; + va_start (p, overload); + + bool negated = false, rewritten = false, reversed = false; + if (cxx_dialect >= cxx20 && TREE_CODE (overload) == TREE_LIST) + { + /* Handle rebuilding a C++20 rewritten comparison operator expression, + e.g. !(x == y), y <=> x, (x <=> y) @ 0 etc, that resolved to a call + to a user-defined operator<=>/==. */ + gcc_checking_assert (TREE_CODE_CLASS (op) == tcc_comparison + || op == SPACESHIP_EXPR); + int flags = TREE_INT_CST_LOW (TREE_PURPOSE (overload)); + if (TREE_CODE (non_dep) == TRUTH_NOT_EXPR) + { + negated = true; + non_dep = TREE_OPERAND (non_dep, 0); + } + if (flags & LOOKUP_REWRITTEN) + rewritten = true; + if (flags & LOOKUP_REVERSED) + reversed = true; + if (rewritten + && DECL_OVERLOADED_OPERATOR_IS (TREE_VALUE (overload), + SPACESHIP_EXPR)) + { + /* Handle (x <=> y) @ 0 and 0 @ (y <=> x) by recursing to first + rebuild the <=>. Note that both OVERLOAD and the provided arguments + in this case already correspond to the selected operator<=>. */ + + tree spaceship_non_dep = CALL_EXPR_ARG (non_dep, reversed ? 1 : 0); + gcc_checking_assert (TREE_CODE (spaceship_non_dep) == CALL_EXPR); + tree spaceship_op0 = va_arg (p, tree); + tree spaceship_op1 = va_arg (p, tree); + if (reversed) + std::swap (spaceship_op0, spaceship_op1); + + /* Push the correct arguments for the operator OP expression, and + set OVERLOAD appropriately. */ + tree op0 = build_min_non_dep_op_overload (SPACESHIP_EXPR, + spaceship_non_dep, + TREE_VALUE (overload), + spaceship_op0, + spaceship_op1); + tree op1 = CALL_EXPR_ARG (non_dep, reversed ? 0 : 1); + gcc_checking_assert (integer_zerop (op1)); + vec_safe_push (args, op0); + vec_safe_push (args, op1); + overload = CALL_EXPR_FN (non_dep); + } + else + overload = TREE_VALUE (overload); + } non_dep = extract_call_expr (non_dep); nargs = call_expr_nargs (non_dep); @@ -3717,32 +3768,40 @@ build_min_non_dep_op_overload (enum tree_code op, expected_nargs += 1; gcc_assert (nargs == expected_nargs); - releasing_vec args; - va_start (p, overload); - if (!DECL_OBJECT_MEMBER_FUNCTION_P (overload)) { fn = overload; - if (op == ARRAY_REF) - obj = va_arg (p, tree); - for (int i = 0; i < nargs; i++) + if (vec_safe_length (args) != 0) + /* The correct arguments were already pushed above. */ + gcc_checking_assert (rewritten); + else { - tree arg = va_arg (p, tree); - vec_safe_push (args, arg); + if (op == ARRAY_REF) + obj = va_arg (p, tree); + for (int i = 0; i < nargs; i++) + { + tree arg = va_arg (p, tree); + vec_safe_push (args, arg); + } } + if (reversed) + std::swap ((*args)[0], (*args)[1]); } else { + gcc_checking_assert (vec_safe_length (args) == 0); tree object = va_arg (p, tree); - tree binfo = TYPE_BINFO (TREE_TYPE (object)); - tree method = build_baselink (binfo, binfo, overload, NULL_TREE); - fn = build_min (COMPONENT_REF, TREE_TYPE (overload), - object, method, NULL_TREE); for (int i = 0; i < nargs; i++) { tree arg = va_arg (p, tree); vec_safe_push (args, arg); } + if (reversed) + std::swap (object, (*args)[0]); + tree binfo = TYPE_BINFO (TREE_TYPE (object)); + tree method = build_baselink (binfo, binfo, overload, NULL_TREE); + fn = build_min (COMPONENT_REF, TREE_TYPE (overload), + object, method, NULL_TREE); } va_end (p); diff --git a/gcc/testsuite/g++.dg/lookup/operator-8.C b/gcc/testsuite/g++.dg/lookup/operator-8.C index 7fe6a57061bd..32d432dd8432 100644 --- a/gcc/testsuite/g++.dg/lookup/operator-8.C +++ b/gcc/testsuite/g++.dg/lookup/operator-8.C @@ -16,12 +16,16 @@ struct A { template<class T> void f() { A a; - (void)(a != 0); // We only handle this simple case, after PR121179 - (void)(0 != a); // { dg-bogus "deleted" "" { xfail *-*-* } } - (void)(a < 0, 0 < a); // { dg-bogus "deleted" "" { xfail *-*-* } } - (void)(a <= 0, 0 <= a); // { dg-bogus "deleted" "" { xfail *-*-* } } - (void)(a > 0, 0 > a); // { dg-bogus "deleted" "" { xfail *-*-* } } - (void)(a >= 0, 0 >= a); // { dg-bogus "deleted" "" { xfail *-*-* } } + (void)(a != 0); + (void)(0 != a); + (void)(a < 0); + (void)(0 < a); + (void)(a <= 0); + (void)(0 <= a); + (void)(a > 0); + (void)(0 > a); + (void)(a >= 0); + (void)(0 >= a); } // These later-declared namespace-scope overloads shouldn't be considered