On 4/7/20 3:14 PM, Marek Polacek wrote:
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.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

        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/method.c                             | 11 +++++++++++
  gcc/testsuite/g++.dg/cpp2a/spaceship-err4.C |  7 +++++++
  2 files changed, 18 insertions(+)
  create mode 100644 gcc/testsuite/g++.dg/cpp2a/spaceship-err4.C

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/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" }

base-commit: 3d947f1f27188e3a61ba7f42399b1c348469fe13


Reply via email to