Skip to content

Commit

Permalink
c++ modules: uninstantiated template friend class [PR104234]
Browse files Browse the repository at this point in the history
Here we're not clearing DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P for
the instantiated/injected template friend class B, which confuses a
later call to get_originating_module_decl for B.  This patch fixes this
by clearing the flag in tsubst_friend_class (as is already done for
template friend functions by r11-5730-gf7aeb823d9b0de).

After fixing that, we still fail to compile the testcase, rejecting the
later definition of B with

  friend-6_a.C:10:26: error: cannot declare ‘struct B<T>’ in a different module

ultimately because DECL_MODULE_ATTACH_P wasn't set on the original
(injected) declaration of B.  This patch fixes this by calling
set_originating_module in tsubst_friend_class, but for that to work it
seems we need to relax the assert in this latter function since
get_originating_module_decl when called on the TYPE_DECL for B returns
the corresponding TEMPLATE_DECL.

(Alternatively we can instead call set_originating_module on the
TYPE_DECL B as soon as it's created in lookup_template_class (which is
what pushtag does), which doesn't need this assert change because at
this point the TYPE_DECL doesn't have any TEMPLATE_INFO so
get_originating_module_decl becomes a no-op.  Would that be preferable?)

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?

	PR c++/104234

gcc/cp/ChangeLog:

	* module.cc (set_originating_module): Document default argument.
	Relax assert to look through DECL_TEMPLATE_RESULT in the result
	of get_originating_module_decl.
	* pt.cc (tsubst_friend_class): Clear
	DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P and call
	set_originating_module on the instantiated template friend class.

gcc/testsuite/ChangeLog:

	* g++.dg/modules/friend-6_a.C: New test.
  • Loading branch information
Patrick Palka authored and ouuleilei-bot committed Jan 25, 2023
1 parent 9fb9da3 commit feabb05
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
8 changes: 6 additions & 2 deletions gcc/cp/module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18843,14 +18843,18 @@ set_defining_module (tree decl)
}

void
set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
set_originating_module (tree decl, bool friend_p /* = false */)
{
set_instantiating_module (decl);

if (!DECL_NAMESPACE_SCOPE_P (decl))
return;

gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
if (!friend_p)
{
tree o = get_originating_module_decl (decl);
gcc_checking_assert (STRIP_TEMPLATE (o) == decl);
}

if (module_attach_p ())
{
Expand Down
3 changes: 3 additions & 0 deletions gcc/cp/pt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11520,6 +11520,9 @@ tsubst_friend_class (tree friend_tmpl, tree args)
CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl))
= INNERMOST_TEMPLATE_ARGS (CLASSTYPE_TI_ARGS (TREE_TYPE (tmpl)));

DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (tmpl) = false;
set_originating_module (DECL_TEMPLATE_RESULT (tmpl));

/* Substitute into and set the constraints on the new declaration. */
if (tree ci = get_constraints (friend_tmpl))
{
Expand Down
10 changes: 10 additions & 0 deletions gcc/testsuite/g++.dg/modules/friend-6_a.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// PR c++/104234
// { dg-additional-options "-fmodules-ts" }
// { dg-module-cmi pr104234 }
export module pr104234;

template<class> struct A {
template<class T> friend struct B;
};
A<int> a;
template<class T> struct B { };

0 comments on commit feabb05

Please sign in to comment.