public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Nathan Sidwell <nathan@acm.org>
To: Patrick Palka <ppalka@redhat.com>, gcc-patches@gcc.gnu.org
Cc: jason@redhat.com
Subject: Re: [PATCH] c++ modules: uninstantiated template friend class [PR104234]
Date: Thu, 2 Feb 2023 19:56:44 -0500	[thread overview]
Message-ID: <0c6cd4d0-1f01-76e0-3c90-42bb4698649a@acm.org> (raw)
In-Reply-To: <20230125201643.506666-1-ppalka@redhat.com>

That might be sufficient for this case, but temploid friends violate an 
assumption of the implementation -- namely that module A cannot create an entity 
that belongs in module B's symbol table.  This causes a bunch of excitement, 
particularly around handling (well formed) duplicatd instantions.

I'm not sure of the way to handle that, but I suspect something along the lines 
of a flag on such decls and a new hash table to hold these exceptions.

nathan

On 1/25/23 15:16, Patrick Palka wrote:
> 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.
> ---
>   gcc/cp/module.cc                          |  8 ++++++--
>   gcc/cp/pt.cc                              |  3 +++
>   gcc/testsuite/g++.dg/modules/friend-6_a.C | 10 ++++++++++
>   3 files changed, 19 insertions(+), 2 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/modules/friend-6_a.C
> 
> diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
> index 7133009dba5..234ce43b70f 100644
> --- a/gcc/cp/module.cc
> +++ b/gcc/cp/module.cc
> @@ -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 ())
>       {
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index cbe5898b553..f2ee74025e7 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -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))
>   	    {
> diff --git a/gcc/testsuite/g++.dg/modules/friend-6_a.C b/gcc/testsuite/g++.dg/modules/friend-6_a.C
> new file mode 100644
> index 00000000000..97017e4ee78
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/modules/friend-6_a.C
> @@ -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 { };

-- 
Nathan Sidwell


      reply	other threads:[~2023-02-03  0:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-25 20:16 Patrick Palka
2023-02-03  0:56 ` Nathan Sidwell [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0c6cd4d0-1f01-76e0-3c90-42bb4698649a@acm.org \
    --to=nathan@acm.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=ppalka@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).