[pushed] c++: Fix ICE with implicit operator== [PR94462]

Message ID 20200407041134.27735-1-jason@redhat.com
State New
Headers show
Series
  • [pushed] c++: Fix ICE with implicit operator== [PR94462]
Related show

Commit Message

Fangrui Song via Gcc-patches April 7, 2020, 4:11 a.m.
duplicate_decls assumed that any TREE_ARTIFICIAL function at namespace scope
was a built-in function, but now in C++20 it's possible to have an
implicitly declared hidden friend operator==.  We just need to move the
assert into the if condition.

Tested x86_64-pc-linux-gnu, applying to trunk.

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

	PR c++/94462
	* decl.c (duplicate_decls): Fix handling of DECL_HIDDEN_FRIEND_P.
---
 gcc/cp/decl.c                              |  5 +++--
 gcc/testsuite/g++.dg/cpp2a/spaceship-eq9.C | 17 +++++++++++++++++
 2 files changed, 20 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/spaceship-eq9.C


base-commit: c72a1b6f8b26de37d1a922a8af143af009747498
-- 
2.18.1

Patch

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 69a238997b4..a127734af69 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -1451,9 +1451,10 @@  duplicate_decls (tree newdecl, tree olddecl, bool newdecl_is_friend)
 
   /* Check for redeclaration and other discrepancies.  */
   if (TREE_CODE (olddecl) == FUNCTION_DECL
-      && DECL_ARTIFICIAL (olddecl))
+      && DECL_ARTIFICIAL (olddecl)
+      /* A C++20 implicit friend operator== uses the normal path (94462).  */
+      && !DECL_HIDDEN_FRIEND_P (olddecl))
     {
-      gcc_assert (!DECL_HIDDEN_FRIEND_P (olddecl));
       if (TREE_CODE (newdecl) != FUNCTION_DECL)
 	{
 	  /* Avoid warnings redeclaring built-ins which have not been
diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-eq9.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-eq9.C
new file mode 100644
index 00000000000..4f5df226410
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-eq9.C
@@ -0,0 +1,17 @@ 
+// PR c++/94462
+// { dg-do compile { target c++2a } }
+
+namespace std {
+  struct strong_ordering { };
+}
+
+namespace Synth {
+  struct B {
+    friend std::strong_ordering operator<=>(B, B) = default;
+  };
+
+  struct C {
+    friend bool operator==(C, C);
+  };
+}
+