From a6479aa4c0532ee9ad1f098b4e82de9dc684e036 Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Tue, 7 Apr 2020 14:24:52 -0400 Subject: [PATCH] c++: ICE with defaulted comparison operator [PR94478] Here we ICE because early_check_defaulted_comparison passed a null ctx to same_type_p. The attached test is ill-formed according to [class.compare.default]/1, so fixed by detecting this case early. PR c++/94478 - ICE with defaulted comparison operator * method.c (early_check_defaulted_comparison): Give an error when the context is null. * g++.dg/cpp2a/spaceship-err4.C: New test. --- gcc/cp/ChangeLog | 6 ++++++ gcc/cp/method.c | 11 +++++++++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/g++.dg/cpp2a/spaceship-err4.C | 7 +++++++ 4 files changed, 29 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp2a/spaceship-err4.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 561eb4e76f4..3fb9100e023 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2020-04-08 Marek Polacek + + PR c++/94478 - ICE with defaulted comparison operator + * method.c (early_check_defaulted_comparison): Give an error when the + context is null. + 2020-04-08 Tobias Burnus PR middle-end/94120 diff --git a/gcc/cp/method.c b/gcc/cp/method.c index 41b9ff86bdd..9a21bfc1f66 100644 --- a/gcc/cp/method.c +++ b/gcc/cp/method.c @@ -1102,6 +1102,17 @@ early_check_defaulted_comparison (tree fn) return false; } + if (!ctx) + { + if (DECL_OVERLOADED_OPERATOR_IS (fn, SPACESHIP_EXPR)) + error_at (loc, "three-way comparison operator can only be defaulted " + "in a class definition"); + else + error_at (loc, "equality comparison operator can only be defaulted " + "in a class definition"); + return false; + } + if (!DECL_OVERLOADED_OPERATOR_IS (fn, SPACESHIP_EXPR) && !same_type_p (TREE_TYPE (TREE_TYPE (fn)), boolean_type_node)) { diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 3913d212c69..278330f528f 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2020-04-08 Marek Polacek + + PR c++/94478 - ICE with defaulted comparison operator + * g++.dg/cpp2a/spaceship-err4.C: New test. + 2020-04-08 Alexandre Oliva * gcc.target/arm/polytypes.c: Add quotes around diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-err4.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-err4.C new file mode 100644 index 00000000000..00f90ced255 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-err4.C @@ -0,0 +1,7 @@ +// PR c++/94478 - ICE with defaulted comparison operator. +// { dg-do compile { target c++2a } } + +struct B {}; +bool operator!=(const B&, const B&) = default; // { dg-error "equality comparison operator can only be defaulted in a class definition" } +bool operator==(const B&, const B&) = default; // { dg-error "equality comparison operator can only be defaulted in a class definition" } +bool operator<=>(const B&, const B&) = default; // { dg-error "three-way comparison operator can only be defaulted in a class definition" } -- 2.30.2