From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 731A63858C55 for ; Thu, 13 Oct 2022 15:27:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 731A63858C55 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665674833; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=gFGTQ73zSdgfp8emj+LypZao9Tb251hdRI2HyVtFhUQ=; b=er59KPbRJAHNzV/K6VIdNPgt30DSzlpQmbS/DeFDZfsIwmqIHH3h5hmeenkCJe5hf9rIOu ZFX+AJ40L3xoaNVkwqQJDgcCo88tGOcmAXjJlb28X6dQpSpbWAOoEXBH3sOkaAlbVi5Wr0 OKmmmnz98f39m+1OVdqA5Ftmh52IaZ0= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-554-Hg2sFTfwNsOzAAPTBgxjpg-1; Thu, 13 Oct 2022 11:27:11 -0400 X-MC-Unique: Hg2sFTfwNsOzAAPTBgxjpg-1 Received: by mail-qt1-f198.google.com with SMTP id j16-20020a05622a039000b0039cd13a7c15so1175464qtx.13 for ; Thu, 13 Oct 2022 08:27:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gFGTQ73zSdgfp8emj+LypZao9Tb251hdRI2HyVtFhUQ=; b=1wwrEa0OEml+eK9A/R1xEoapjlHYJT6sHpPA1S5adtfnH38FoveJwWmE2IUrlmawbi AzpPJJGl/SPQGosGQCVvahzBhedEopO+ik8tRmuUIbEketmb2jMyXN0x3SEWbEFU37/h XJtHgASMz75S6glQLzqgWy0nt+PrDDnNYx7JnXul7BQYrnfFiXp0c8/EueyhAnrbsjzB MZ01PHYpBt3UmRWZ0vT0cm71yOyUJGW1glhuz4Lc/2QQVhadRcVskhsyegQoDCKSbY3T 77InB8C78eWOFOImHylpXa/SmABMedTZ0zBqE/lnddj4wFE17Q+fIrP74XwgNPMmtIv0 UL8g== X-Gm-Message-State: ACrzQf2uzh0cSyi2Ze1M96MsQkpbUGFOuNYUpb1S9V7Ijj5SnTXRc1eC ga26VhXlUeOWolctgpGvs4tSR//ezc2gY5iChs6hAbyVTlZd8ntx9sqd5QFSrwDvncYA27OTM8a kuPRoKOUYzNwutcTztw== X-Received: by 2002:a05:6214:4104:b0:4af:8576:e65a with SMTP id kc4-20020a056214410400b004af8576e65amr424701qvb.70.1665674831335; Thu, 13 Oct 2022 08:27:11 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7a30M/bCBN+5dwXqxo4Ypg/K1LNuBUpjtMcshblPY/LMyP80fjFKo8tvuXs/PG9pHle6U5WQ== X-Received: by 2002:a05:6214:4104:b0:4af:8576:e65a with SMTP id kc4-20020a056214410400b004af8576e65amr424680qvb.70.1665674831084; Thu, 13 Oct 2022 08:27:11 -0700 (PDT) Received: from [192.168.1.101] (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id w2-20020a05620a444200b006ce1bfbd603sm19503071qkp.124.2022.10.13.08.27.10 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 13 Oct 2022 08:27:10 -0700 (PDT) Message-ID: <072f5a5d-2e17-5f29-7707-3d43472053e6@redhat.com> Date: Thu, 13 Oct 2022 11:27:09 -0400 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.3.2 Subject: Re: [PATCH] c++ modules: ICE with templated friend and std namespace [PR100134] To: Nathan Sidwell , Patrick Palka , gcc-patches@gcc.gnu.org References: <20221011153507.784631-1-ppalka@redhat.com> <4a5f7f70-9626-f067-0ba9-9bf993da8a34@acm.org> From: Jason Merrill In-Reply-To: <4a5f7f70-9626-f067-0ba9-9bf993da8a34@acm.org> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,BODY_8BITS,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 10/11/22 13:40, Nathan Sidwell wrote: > On 10/11/22 11:35, Patrick Palka wrote: >> IIUC the function depset::hash::add_binding_entity has an assert >> verifying that if a namespace contains an exported entity, then >> the namespace must have been opened in the module purview: >> >>    if (data->hash->add_namespace_entities (decl, data->partitions)) >>      { >>        /* It contains an exported thing, so it is exported.  */ >>        gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl)); >>        DECL_MODULE_EXPORT_P (decl) = true; >>      } >> >> We're tripping over this assert in the below testcase because by >> instantiating and exporting std::A, we end up in turn defining >> and exporting the hidden friend std::f without ever having opening >> the enclosing namespace std within the module purview and thus >> DECL_MODULE_PURVIEW_P (std_node) is false. >> >> Note that it's important that the enclosing namespace is std here: if we >> use a different namespace then the ICE disappears.  This probably has >> something to do with the fact that we predefine std via push_namespace >> from cxx_init_decl_processing (which makes it look like we've opened the >> namespace in the TU), whereas with another namespace we would instead >> lazily obtain the NAMESPACE_DECL from add_imported_namespace. >> >> Since templated frined functions are special in that they give us a way >> to declare a new namespace-scope function without having to explicitly >> open the namespace, this patch proposes to fix this issue by propagating >> DECL_MODULE_PURVIEW_P from a friend function to the enclosing namespace >> when instantiating the friend. > > ouch.  This is ok, but I think we have a bug -- what is the module > ownership of the friend introduced by the instantiation? > > Haha, there's a note on 13.7.5/3 -- the attachment is to the same module > as the befriending class. > > That means we end up creating and writing out entities that exist in the > symbol table (albeit hidden) whose module ownership is neither the > global module or the tu's module.  That's not something the module > machinery anticipates. We'll get the mangling wrong for starters. Hmm. > > These are probably rare.  Thinking about the right solution though ... This seems closely connected to https://cplusplus.github.io/CWG/issues/2588.html Jason > nathan > > >> >> Tested on x86_64-pc-linux-gnu, does this look like the right fix?  Other >> solutions that seem to work are to set DECL_MODULE_PURVIEW_P on std_node >> after the fact from declare_module, or simply to suppress the assert for >> std_node. >> >>     PR c++/100134 >> >> gcc/cp/ChangeLog: >> >>     * pt.cc (tsubst_friend_function): Propagate DECL_MODULE_PURVIEW_P >>     from the new declaration to the enclosing namespace scope. >> >> gcc/testsuite/ChangeLog: >> >>     * g++.dg/modules/tpl-friend-8_a.H: New test. >>     * g++.dg/modules/tpl-friend-8_b.C: New test. >> --- >>   gcc/cp/pt.cc                                  | 7 +++++++ >>   gcc/testsuite/g++.dg/modules/tpl-friend-8_a.H | 9 +++++++++ >>   gcc/testsuite/g++.dg/modules/tpl-friend-8_b.C | 8 ++++++++ >>   3 files changed, 24 insertions(+) >>   create mode 100644 gcc/testsuite/g++.dg/modules/tpl-friend-8_a.H >>   create mode 100644 gcc/testsuite/g++.dg/modules/tpl-friend-8_b.C >> >> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc >> index 5b9fc588a21..9e3085f3fa6 100644 >> --- a/gcc/cp/pt.cc >> +++ b/gcc/cp/pt.cc >> @@ -11448,6 +11448,13 @@ tsubst_friend_function (tree decl, tree args) >>            by duplicate_decls.  */ >>         new_friend = old_decl; >>       } >> + >> +      /* We've just added a new namespace-scope entity to the purview >> without >> +     necessarily having opened the enclosing namespace, so make sure the >> +     enclosing namespace is in the purview now too.  */ >> +      if (TREE_CODE (DECL_CONTEXT (new_friend)) == NAMESPACE_DECL) >> +    DECL_MODULE_PURVIEW_P (DECL_CONTEXT (new_friend)) >> +      |= DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (new_friend)); >>       } >>     else >>       { >> diff --git a/gcc/testsuite/g++.dg/modules/tpl-friend-8_a.H >> b/gcc/testsuite/g++.dg/modules/tpl-friend-8_a.H >> new file mode 100644 >> index 00000000000..bd2290460b5 >> --- /dev/null >> +++ b/gcc/testsuite/g++.dg/modules/tpl-friend-8_a.H >> @@ -0,0 +1,9 @@ >> +// PR c++/100134 >> +// { dg-additional-options -fmodule-header } >> +// { dg-module-cmi {} } >> + >> +namespace std { >> +  template struct A { >> +    friend void f(A) { } >> +  }; >> +} >> diff --git a/gcc/testsuite/g++.dg/modules/tpl-friend-8_b.C >> b/gcc/testsuite/g++.dg/modules/tpl-friend-8_b.C >> new file mode 100644 >> index 00000000000..76d7447c2eb >> --- /dev/null >> +++ b/gcc/testsuite/g++.dg/modules/tpl-friend-8_b.C >> @@ -0,0 +1,8 @@ >> +// PR c++/100134 >> +// { dg-additional-options -fmodules-ts } >> +// { dg-module-cmi pr100134 } >> +export module pr100134; >> + >> +import "tpl-friend-8_a.H"; >> + >> +export std::A a; >