c++: Fix instantiation of imported temploid friends [PR114275]
commitb5f6a56940e70838a07e885de03a92e2bd64674a
authorNathaniel Shead <nathanieloshead@gmail.com>
Mon, 29 Apr 2024 07:00:13 +0000 (29 17:00 +1000)
committerNathaniel Shead <nathanieloshead@gmail.com>
Tue, 30 Apr 2024 05:02:17 +0000 (30 15:02 +1000)
tree3a813f9171150f71c9380728386eb35c8ed2dcfb
parent2faf040335f9b49c33ba6d40cf317920f27ce431
c++: Fix instantiation of imported temploid friends [PR114275]

This patch fixes a number of issues with the handling of temploid friend
declarations.

The primary issue is that instantiations of friend declarations should
attach the declaration to the same module as the befriending class, by
[module.unit] p7.1 and [temp.friend] p2; this could be a different
module from the current TU, and so needs special handling.

The other main issue here is that we can't assume that just because name
lookup didn't find a definition for a hidden class template, that it
doesn't exist at all: it could be a non-exported entity that we've
nevertheless streamed in from an imported module.  We need to ensure
that when instantiating template friend classes that we return the same
TEMPLATE_DECL that we got from our imports, otherwise we will get later
issues with 'duplicate_decls' (rightfully) complaining that they're
different when trying to merge.

This doesn't appear necessary for function templates due to the existing
name lookup handling already finding these hidden declarations.

PR c++/105320
PR c++/114275

gcc/cp/ChangeLog:

* cp-tree.h (propagate_defining_module): Declare.
(lookup_imported_hidden_friend): Declare.
* decl.cc (duplicate_decls): Also check if hidden decls can be
redeclared in this module.
* module.cc (imported_temploid_friends): New.
(init_modules): Initialize it.
(trees_out::decl_value): Write it; don't consider imported
temploid friends as attached to a module.
(trees_in::decl_value): Read it.
(get_originating_module_decl): Follow the owning decl for an
imported temploid friend.
(propagate_defining_module): New.
* name-lookup.cc (get_mergeable_namespace_binding): New.
(lookup_imported_hidden_friend): New.
* pt.cc (tsubst_friend_function): Propagate defining module for
new friend functions.
(tsubst_friend_class): Lookup imported hidden friends.  Check
for valid module attachment of existing names.  Propagate
defining module for new classes.

gcc/testsuite/ChangeLog:

* g++.dg/modules/tpl-friend-10_a.C: New test.
* g++.dg/modules/tpl-friend-10_b.C: New test.
* g++.dg/modules/tpl-friend-10_c.C: New test.
* g++.dg/modules/tpl-friend-10_d.C: New test.
* g++.dg/modules/tpl-friend-11_a.C: New test.
* g++.dg/modules/tpl-friend-11_b.C: New test.
* g++.dg/modules/tpl-friend-12_a.C: New test.
* g++.dg/modules/tpl-friend-12_b.C: New test.
* g++.dg/modules/tpl-friend-12_c.C: New test.
* g++.dg/modules/tpl-friend-12_d.C: New test.
* g++.dg/modules/tpl-friend-12_e.C: New test.
* g++.dg/modules/tpl-friend-12_f.C: New test.
* g++.dg/modules/tpl-friend-13_a.C: New test.
* g++.dg/modules/tpl-friend-13_b.C: New test.
* g++.dg/modules/tpl-friend-13_c.C: New test.
* g++.dg/modules/tpl-friend-13_d.C: New test.
* g++.dg/modules/tpl-friend-13_e.C: New test.
* g++.dg/modules/tpl-friend-13_f.C: New test.
* g++.dg/modules/tpl-friend-13_g.C: New test.
* g++.dg/modules/tpl-friend-14_a.C: New test.
* g++.dg/modules/tpl-friend-14_b.C: New test.
* g++.dg/modules/tpl-friend-14_c.C: New test.
* g++.dg/modules/tpl-friend-14_d.C: New test.
* g++.dg/modules/tpl-friend-9.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
29 files changed:
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/cp/module.cc
gcc/cp/name-lookup.cc
gcc/cp/pt.cc
gcc/testsuite/g++.dg/modules/tpl-friend-10_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-10_b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-10_c.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-10_d.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-11_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-11_b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-12_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-12_b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-12_c.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-12_d.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-12_e.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-12_f.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-13_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-13_b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-13_c.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-13_d.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-13_e.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-13_f.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-13_g.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-14_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-14_b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-14_c.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-14_d.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/tpl-friend-9.C [new file with mode: 0644]