Here, when genericizing lexicographical_compare_three_way, we haven't yet walked the operands, so (a == a) still sees ADDR_EXPR <a>, but this is after we've changed the type of a to REFERENCE_TYPE. When we try to fold (a == a) by constexpr evaluation, the constexpr code doesn't understand trying to take the address of a reference, and we end up crashing.
Fixed by avoiding constexpr evaluation in genericize_spaceship, by using fold_build2 instead of build_new_op on scalar operands. Class operands should have been expanded during parsing. Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/100367 PR c++/96299 gcc/cp/ChangeLog: * method.c (genericize_spaceship): Use fold_build2 for scalar operands. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/spaceship-fallback1.C: New test. --- gcc/cp/method.c | 27 ++++++++++++++----- .../g++.dg/cpp2a/spaceship-fallback1.C | 17 ++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/spaceship-fallback1.C diff --git a/gcc/cp/method.c b/gcc/cp/method.c index f8c9456d720..75effb0d698 100644 --- a/gcc/cp/method.c +++ b/gcc/cp/method.c @@ -1087,7 +1087,8 @@ genericize_spaceship (location_t loc, tree type, tree op0, tree op1) gcc_checking_assert (tag < cc_last); tree r; - if (SCALAR_TYPE_P (TREE_TYPE (op0))) + bool scalar = SCALAR_TYPE_P (TREE_TYPE (op0)); + if (scalar) { op0 = save_expr (op0); op1 = save_expr (op1); @@ -1097,26 +1098,38 @@ genericize_spaceship (location_t loc, tree type, tree op0, tree op1) int flags = LOOKUP_NORMAL; tsubst_flags_t complain = tf_none; + tree comp; if (tag == cc_partial_ordering) { /* op0 == op1 ? equivalent : op0 < op1 ? less : op1 < op0 ? greater : unordered */ tree uo = lookup_comparison_result (tag, type, 3); - tree comp = build_new_op (loc, LT_EXPR, flags, op1, op0, complain); - r = build_conditional_expr (loc, comp, gt, uo, complain); + if (scalar) + /* For scalars use the low level operations; using build_new_op causes + trouble with constexpr eval in the middle of genericize (100367). */ + comp = fold_build2 (LT_EXPR, boolean_type_node, op1, op0); + else + comp = build_new_op (loc, LT_EXPR, flags, op1, op0, complain); + r = fold_build3 (COND_EXPR, type, comp, gt, uo); } else /* op0 == op1 ? equal : op0 < op1 ? less : greater */ r = gt; tree lt = lookup_comparison_result (tag, type, 2); - tree comp = build_new_op (loc, LT_EXPR, flags, op0, op1, complain); - r = build_conditional_expr (loc, comp, lt, r, complain); + if (scalar) + comp = fold_build2 (LT_EXPR, boolean_type_node, op0, op1); + else + comp = build_new_op (loc, LT_EXPR, flags, op0, op1, complain); + r = fold_build3 (COND_EXPR, type, comp, lt, r); tree eq = lookup_comparison_result (tag, type, 0); - comp = build_new_op (loc, EQ_EXPR, flags, op0, op1, complain); - r = build_conditional_expr (loc, comp, eq, r, complain); + if (scalar) + comp = fold_build2 (EQ_EXPR, boolean_type_node, op0, op1); + else + comp = build_new_op (loc, EQ_EXPR, flags, op0, op1, complain); + r = fold_build3 (COND_EXPR, type, comp, eq, r); return r; } diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-fallback1.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-fallback1.C new file mode 100644 index 00000000000..5ce49490fe5 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-fallback1.C @@ -0,0 +1,17 @@ +// PR c++/100367 +// { dg-do compile { target c++20 } } + +#include <compare> + +struct iter { + bool current; + iter(iter &); +}; + +constexpr bool operator==(const iter &, const iter &y) { + return y.current; +} + +void lexicographical_compare_three_way(iter a) { + (a == a) <=> true; +} base-commit: 873c5188fd5d2e17430cab1522aa36fa62582ea7 -- 2.27.0