c++: repeated friend template [PR101894]
authorJason Merrill <jason@redhat.com>
Fri, 1 Apr 2022 20:18:31 +0000 (16:18 -0400)
committerJason Merrill <jason@redhat.com>
Mon, 4 Apr 2022 14:44:06 +0000 (10:44 -0400)
Since olddecl isn't a definition, it doesn't get DECL_FRIEND_CONTEXT, so we
need to copy it from newdecl when we merge the declarations.

PR c++/101894

gcc/cp/ChangeLog:

* decl.cc (duplicate_decls): Copy DECL_FRIEND_CONTEXT.

gcc/testsuite/ChangeLog:

* g++.dg/lookup/friend22.C: New test.

gcc/cp/decl.cc
gcc/testsuite/g++.dg/lookup/friend22.C [new file with mode: 0644]

index 69f60a6..0ff13e9 100644 (file)
@@ -2344,6 +2344,9 @@ duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
              for (parm = DECL_ARGUMENTS (old_result); parm;
                   parm = DECL_CHAIN (parm))
                DECL_CONTEXT (parm) = old_result;
+
+             if (tree fc = DECL_FRIEND_CONTEXT (new_result))
+               SET_DECL_FRIEND_CONTEXT (old_result, fc);
            }
        }
 
@@ -2667,6 +2670,8 @@ duplicate_decls (tree newdecl, tree olddecl, bool hiding, bool was_hidden)
             otherwise it is a DECL_FRIEND_CONTEXT.  */
          if (DECL_VIRTUAL_P (newdecl))
            SET_DECL_THUNKS (newdecl, DECL_THUNKS (olddecl));
+         else if (tree fc = DECL_FRIEND_CONTEXT (newdecl))
+           SET_DECL_FRIEND_CONTEXT (olddecl, fc);
        }
       else if (VAR_P (newdecl))
        {
diff --git a/gcc/testsuite/g++.dg/lookup/friend22.C b/gcc/testsuite/g++.dg/lookup/friend22.C
new file mode 100644 (file)
index 0000000..f52a7d7
--- /dev/null
@@ -0,0 +1,7 @@
+// PR c++/101894
+
+struct A
+{
+  template<int> friend void foo();
+  template<int> friend void foo() {}
+};