aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarek Polacek <polacek@redhat.com>2020-04-07 14:24:52 -0400
committerMarek Polacek <polacek@redhat.com>2020-04-08 08:56:07 -0400
commita6479aa4c0532ee9ad1f098b4e82de9dc684e036 (patch)
tree27a48b54e66d8f29800f434fa5a11010af19cb79
parent542f73539db1433303a4dd16bd2cfc5e7e12eda8 (diff)
downloadgcc-a6479aa4c0532ee9ad1f098b4e82de9dc684e036.zip
gcc-a6479aa4c0532ee9ad1f098b4e82de9dc684e036.tar.gz
gcc-a6479aa4c0532ee9ad1f098b4e82de9dc684e036.tar.bz2
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.
-rw-r--r--gcc/cp/ChangeLog6
-rw-r--r--gcc/cp/method.c11
-rw-r--r--gcc/testsuite/ChangeLog5
-rw-r--r--gcc/testsuite/g++.dg/cpp2a/spaceship-err4.C7
4 files changed, 29 insertions, 0 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 561eb4e..3fb9100 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2020-04-08 Marek Polacek <polacek@redhat.com>
+
+ 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 <tobias@codesourcery.com>
PR middle-end/94120
diff --git a/gcc/cp/method.c b/gcc/cp/method.c
index 41b9ff8..9a21bfc 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 3913d21..278330f 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,8 @@
+2020-04-08 Marek Polacek <polacek@redhat.com>
+
+ PR c++/94478 - ICE with defaulted comparison operator
+ * g++.dg/cpp2a/spaceship-err4.C: New test.
+
2020-04-08 Alexandre Oliva <oliva@adacore.com>
* 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 0000000..00f90ce
--- /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" }