From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id 06ABA3857B9B; Tue, 11 Oct 2022 19:03:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 06ABA3857B9B DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665514984; bh=nZHagx9A+7AG9HptMHvfqXL9vqieodr+DrOoSHkb4nc=; h=From:To:Subject:Date:From; b=lKlLN1zPVAjJKEAn6Vk7cMf6EsVT+WCKJFYYxiW41H7MErIOffd6tDAS3t70uKrsL Xg2E4np8m5MgO07TTJNnM89L2Ffan2nU9TLEJZKQkpNzSRZxRnFt9cVbveK5YFZ6Q+ MRNCEnL5uMCAhH8YwgCP6c3vI/OuIdJ/YF+42P/I= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3236] c++ modules: ICE with templated friend and std namespace [PR100134] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: 2ceb4d531a303f3e70d8bb218c8759e6c0688f62 X-Git-Newrev: 9736a42e1fb8df30d72cf28594d9046bf50200c1 Message-Id: <20221011190304.06ABA3857B9B@sourceware.org> Date: Tue, 11 Oct 2022 19:03:04 +0000 (GMT) List-Id: https://gcc.gnu.org/g:9736a42e1fb8df30d72cf28594d9046bf50200c1 commit r13-3236-g9736a42e1fb8df30d72cf28594d9046bf50200c1 Author: Patrick Palka Date: Tue Oct 11 15:02:23 2022 -0400 c++ modules: ICE with templated friend and std namespace [PR100134] 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 in turn define and export the hidden friend std::f(A) without ever having opened the enclosing namespace std within the module purview, and thus DECL_MODULE_PURVIEW_P for std is false. 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 us predefining std via push_namespace from cxx_init_decl_processing (which makes it look like we've opened it within the TU), whereas with another namespace we would instead lazily create its NAMESPACE_DECL from add_imported_namespace. Since templated friend functions are special in that they give us a way to introduce a namespace-scope function without having to explicitly open the namespace, this patch proposes to fix this ICE by propagating DECL_MODULE_PURVIEW_P from the introduced function to the enclosing namespace during tsubst_friend_function. PR c++/100134 gcc/cp/ChangeLog: * pt.cc (tsubst_friend_function): Propagate DECL_MODULE_PURVIEW_P from the introduced namespace-scope function to the namespace. gcc/testsuite/ChangeLog: * g++.dg/modules/tpl-friend-8_a.H: New test. * g++.dg/modules/tpl-friend-8_b.C: New test. Diff: --- gcc/cp/pt.cc | 8 ++++++++ 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, 25 insertions(+) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 5b9fc588a21..b80e7ff1845 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -11448,6 +11448,14 @@ tsubst_friend_function (tree decl, tree args) by duplicate_decls. */ new_friend = old_decl; } + + /* We've just introduced a namespace-scope function in the purview + without necessarily having opened the enclosing namespace, so + make sure the namespace is in the purview now too. */ + if (modules_p () + && DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (new_friend)) + && TREE_CODE (DECL_CONTEXT (new_friend)) == NAMESPACE_DECL) + DECL_MODULE_PURVIEW_P (DECL_CONTEXT (new_friend)) = true; } 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;