public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-8437] c++: ICE with <=> fallback [PR100367]
@ 2021-05-19 20:17 Jason Merrill
0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2021-05-19 20:17 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:3bdd3e45955ef94a1f2db51a2af1ded54d41f670
commit r11-8437-g3bdd3e45955ef94a1f2db51a2af1ded54d41f670
Author: Jason Merrill <jason@redhat.com>
Date: Tue May 18 12:29:33 2021 -0400
c++: ICE with <=> fallback [PR100367]
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.
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.
Diff:
---
gcc/cp/method.c | 42 ++++++++++++++++++++----
gcc/testsuite/g++.dg/cpp2a/spaceship-fallback1.C | 17 ++++++++++
2 files changed, 52 insertions(+), 7 deletions(-)
diff --git a/gcc/cp/method.c b/gcc/cp/method.c
index 0f416bec35b..1c3cf834bf4 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,53 @@ 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);
+ r = fold_build3 (COND_EXPR, type, comp, gt, uo);
+ }
+ else
+ {
+ comp = build_new_op (loc, LT_EXPR, flags, op1, op0, complain);
+ r = build_conditional_expr (loc, comp, gt, uo, complain);
+ }
}
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);
+ r = fold_build3 (COND_EXPR, type, comp, lt, r);
+ }
+ else
+ {
+ comp = build_new_op (loc, LT_EXPR, flags, op0, op1, complain);
+ r = build_conditional_expr (loc, comp, lt, r, complain);
+ }
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);
+ r = fold_build3 (COND_EXPR, type, comp, eq, r);
+ }
+ else
+ {
+ comp = build_new_op (loc, EQ_EXPR, flags, op0, op1, complain);
+ r = build_conditional_expr (loc, comp, eq, r, complain);
+ }
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;
+}
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2021-05-19 20:17 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-19 20:17 [gcc r11-8437] c++: ICE with <=> fallback [PR100367] Jason Merrill
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).