c++: implicit operator== with previous decl [PR94583]
authorJason Merrill <jason@redhat.com>
Fri, 24 Apr 2020 20:27:26 +0000 (16:27 -0400)
committerJason Merrill <jason@redhat.com>
Sat, 25 Apr 2020 04:17:30 +0000 (00:17 -0400)
P2085 clarified that a defaulted comparison operator must be the first
declaration of the function.  Rejecting that avoids the ICE trying to
compare the noexcept-specifications.

gcc/cp/ChangeLog
2020-04-24  Jason Merrill  <jason@redhat.com>

PR c++/94583
* decl.c (redeclaration_error_message): Reject defaulted comparison
operator that has been previously declared.

gcc/cp/ChangeLog
gcc/cp/decl.c
gcc/testsuite/g++.dg/cpp2a/spaceship-synth6.C [new file with mode: 0644]

index 2a5b417136685b5cea1bfae4a556a07ac6323383..f0c62783101581029497d7ccc11c28cea938d073 100644 (file)
@@ -1,3 +1,9 @@
+2020-04-24  Jason Merrill  <jason@redhat.com>
+
+       PR c++/94583
+       * decl.c (redeclaration_error_message): Reject defaulted comparison
+       operator that has been previously declared.
+
 2020-04-25  Patrick Palka  <ppalka@redhat.com>
 
        * parser.c (cp_parser_diagnose_invalid_type_name): Suggest enabling
index c8c2f080763de2f12c37a0696e0ba4792f90a5c8..31b5884ca3a917416670bd5fcdc3733e0910eda6 100644 (file)
@@ -2972,6 +2972,14 @@ redeclaration_error_message (tree newdecl, tree olddecl)
            }
        }
 
+      /* [class.compare.default]: A definition of a comparison operator as
+        defaulted that appears in a class shall be the first declaration of
+        that function.  */
+      special_function_kind sfk = special_function_p (olddecl);
+      if (sfk == sfk_comparison && DECL_DEFAULTED_FN (newdecl))
+       return G_("comparison operator %q+D defaulted after "
+                 "its first declaration");
+
       check_abi_tag_redeclaration
        (olddecl, lookup_attribute ("abi_tag", DECL_ATTRIBUTES (olddecl)),
         lookup_attribute ("abi_tag", DECL_ATTRIBUTES (newdecl)));
diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-synth6.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-synth6.C
new file mode 100644 (file)
index 0000000..e8296bb
--- /dev/null
@@ -0,0 +1,11 @@
+// PR c++/94583
+// { dg-do compile { target c++2a } }
+
+namespace std { struct strong_ordering { }; }
+
+bool operator==(const struct Q&, const struct Q&);
+struct Q {
+  // { dg-error "defaulted after its first declaration" "" { target *-*-* } .+1 }
+  friend std::strong_ordering operator<=>(const Q&, const Q&) = default;
+};
+bool b = Q() == Q();