From patchwork Sun Mar 3 23:11:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 209354 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:fa17:b0:10a:f01:a869 with SMTP id ju23csp1124142dyc; Sun, 3 Mar 2024 15:12:35 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVKYc9l8Lx0UQZrmig71RLp1m/qDZqMPFrLOzjFPf3ciKSOhOxKkM4qFUkZ0YhP8PkYQyfxGzv5I1v1kGooKbcoK90Mzw== X-Google-Smtp-Source: AGHT+IGq9mNtTg4d1ibJ19n5UhPX2ArbfwN2GMU9W8MtipEnrYtykF+zGsarkLTFDdyahvVMrx98 X-Received: by 2002:a05:620a:2415:b0:788:2ac2:8457 with SMTP id d21-20020a05620a241500b007882ac28457mr806073qkn.71.1709507554625; Sun, 03 Mar 2024 15:12:34 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709507554; cv=pass; d=google.com; s=arc-20160816; b=XDp6lUkDHUIHxiaDCnWrgS/4+vEdKH4hE5zI+4HAG2ML/kG7aEdMvUCdrRRne4BXrh ANVpJO63CE5CSC5RQMekLeESDKp9cED/rSKb6cUyec+2Ff/Evpeb9hbBPbLBdJe647YP 7lcGP2zxnG3TfHr8ID6TFsqjQ8ieqtnBseUiRGumwlTHOLSDpfkXhxsPzxvGMv2QfeVH dCowiQWMnBSZhuchq+09MnL9Plnt4YbakcvKYWa2xAlKOK8EdunFR89/fp0rByKDX6pY yaRjeum9grR1qcdA8tTIh89YVHFort7f9OdTsntIIMz0xOA0WFeq3dUdUaHR5wXaA7wP hcLg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-disposition :mime-version:subject:cc:to:from:date:message-id:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=n8P1N8eV5n4z5fQ8uYFmqiw+ZpE74Q2/RxCGREtm6OQ=; fh=PSfnKtImOwcFiHTY31kD+1EEfyCGdToyJVXnD0Outng=; b=kiBZ14aNQM7JQCTMKonHQLYLAktJZEej4uy8Kmn5GPnZY7CmQmQX5/A1coDhIkM6Ib yQ8nGY7/OqqYhF0QpSN05TtQmwjM67PfGwjqRIzfqv0ipJtTl0Xo7esXM6D/M68FGd+j zfaJqs2WUUkFxCFgaf9Q+qwhkYPDb9GPvkhWmOMezBTQQLSq8pEVNRFVhkFCBu5peztW SA0k77ndjz4frkuvg3h51YXiZ1PFkxR6unRVcvmOZJuhbtMsac+oTLHD7W2jumPvKIDX ReAA9OOmwmtmv8fd3/Eu16TtGsiClFbpNV5v0mNrMpRRZEV/gI/JaiUzd4y5FVXkXpn8 smFQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=O9WRhWnv; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ou24-20020a05620a621800b00787c6ff2d1dsi8183877qkn.212.2024.03.03.15.12.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Mar 2024 15:12:34 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=O9WRhWnv; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 50EAE3858424 for ; Sun, 3 Mar 2024 23:12:34 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by sourceware.org (Postfix) with ESMTPS id 0D4E93858D28 for ; Sun, 3 Mar 2024 23:11:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0D4E93858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 0D4E93858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::535 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709507514; cv=none; b=I8XEPQ2su1j2ylOLTfZwb6TejGzYnu100SAdpls1j+jGJYiQ2PrE/hnA1LU4ZZ8Z3BN76uQToSWFMHZqfnAU6dgye6aSbcYyGi1uFqULTLW16law0dCtGnV3CdOz43GUWDj4yIc+b1nTJv3gDN/0PRkSSJAEeRObHy3HyYo28Dc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709507514; c=relaxed/simple; bh=IwKoHrGz3HBryCpavuPoqPRwQGXNxLARyHiP4WhV1Ng=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=MthtoDCpecEogOa0fai08ry+eYgjGYE/E5h1VyfDcNKyCwJQTHxlv72hvr7o29dTJHST7rETsXsWYOVJh7o4kViyObj1Yi6Lkx6zQM4u4VIEMawwyWnmrhFsB6Ghu8maYiKVEtXSD+AQMhzwBj5fKt3DJ2p/E7E0prRtjLsLs+g= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x535.google.com with SMTP id 41be03b00d2f7-5bdbe2de25fso3484418a12.3 for ; Sun, 03 Mar 2024 15:11:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709507511; x=1710112311; darn=gcc.gnu.org; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=n8P1N8eV5n4z5fQ8uYFmqiw+ZpE74Q2/RxCGREtm6OQ=; b=O9WRhWnvd4D/oZUTZ77zLEBL0wvw7f7nhq/dU5uoHejh7TdT679WmLKQNWMG5EJ1NA I+T4GtctiU2uqzJC6DSli6cF9KTjOxE996i9CqmAsNl+IbH/KAtigjNjO345oFIF0Mpw U3d3p5as+RGGZwFASetZS3m8oFVwH0bgamxKuh+all3POFtjOHeX5M1IwOGv5fcA0DUh VY5CBBjoKuxNMNr/WtqFTJ8w1oNGAfdrnPR+8GVzr65SQhzySPFANIcHOzjqZJb+h8om YHYot5BLLIubMJXGtuoI9+DBMZU78+G1op1uLf+lhFAuNfXwUSo3NlQEjWDhijxbk6WM tEeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709507511; x=1710112311; h=content-disposition:mime-version:subject:cc:to:from:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=n8P1N8eV5n4z5fQ8uYFmqiw+ZpE74Q2/RxCGREtm6OQ=; b=a7pBhmUeU3AO24Pfk7I35Sy6AZop4Y9COOrvR9STtBbR8mycpnq2ZsxYsAVWp/jhg6 GQ9/gNzNvtkORYyWbJ2dNsjqvrmMg3lniKGNL1zQ1w9xgFJlY6+SEkJBx8mWrH3fomwl ctuQiqfAsUar9KMDEwtaTrXeawBb4YrpPwFWwR06e4Rr9diq8AjVRZ1adaK4gJIL3hlp AJ455MrmGnHkkdyQfeqRjGpzreCcazrdjAW0WR/axam+ymd8nJwzyruIheMce2IZP4pu 9opbERMWaP1e5hrY4rQt4q3RhVint+5z7+TwYu+ctU3FbE1Rykd4Xxkir89cpcfvk9UR YRZQ== X-Gm-Message-State: AOJu0YzY5jCb0zeBOAodMA4uq1tLT0DlkdDXt0MK03+5apeBK1PAahLe A4yPnpk0mx+ya2kQ+xXtmpOKDG02Ii1vXqTGoIB3DjNUZ3y7KJ1HoAGOH5JC X-Received: by 2002:a17:902:650e:b0:1dc:d4f9:fd16 with SMTP id b14-20020a170902650e00b001dcd4f9fd16mr8126491plk.10.1709507510665; Sun, 03 Mar 2024 15:11:50 -0800 (PST) Received: from Thaum. (110-175-172-107.tpgi.com.au. [110.175.172.107]) by smtp.gmail.com with ESMTPSA id q15-20020a170902dacf00b001db5ecb2899sm4771440plx.162.2024.03.03.15.11.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 03 Mar 2024 15:11:50 -0800 (PST) Message-ID: <65e503b6.170a0220.22d59.01d0@mx.google.com> X-Google-Original-Message-ID: Date: Mon, 4 Mar 2024 10:11:45 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH] c++/modules: Support exporting using-decls in same namespace as target MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-12.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792548593604957421 X-GMAIL-MSGID: 1792548593604957421 Came across this issue while working on another PR. Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? Or otherwise for GCC 15? -- >8 -- Currently a using-declaration bringing a name into its own namespace is a no-op, except for functions. This prevents people from being able to redeclare a name brought in from the GMF as exported, however, which this patch fixes. Apart from marking declarations as exported they are also now marked as effectively being in the module purview (due to the using-decl) so that they are properly processed, as 'add_binding_entity' assumes that declarations not in the module purview cannot possibly be exported. gcc/cp/ChangeLog: * name-lookup.cc (walk_module_binding): Remove completed FIXME. (do_nonmember_using_decl): Mark redeclared entities as exported when needed. Check for re-exporting internal linkage types. gcc/testsuite/ChangeLog: * g++.dg/modules/using-12.C: New test. * g++.dg/modules/using-13.h: New test. * g++.dg/modules/using-13_a.C: New test. * g++.dg/modules/using-13_b.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/name-lookup.cc | 50 +++++++++++++--- gcc/testsuite/g++.dg/modules/using-12.C | 73 +++++++++++++++++++++++ gcc/testsuite/g++.dg/modules/using-13.h | 16 +++++ gcc/testsuite/g++.dg/modules/using-13_a.C | 15 +++++ gcc/testsuite/g++.dg/modules/using-13_b.C | 20 +++++++ 5 files changed, 166 insertions(+), 8 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/using-12.C create mode 100644 gcc/testsuite/g++.dg/modules/using-13.h create mode 100644 gcc/testsuite/g++.dg/modules/using-13_a.C create mode 100644 gcc/testsuite/g++.dg/modules/using-13_b.C diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index 6444db3f0eb..dce4caf8981 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -4189,7 +4189,7 @@ walk_module_binding (tree binding, bitmap partitions, void *data) { // FIXME: We don't quite deal with using decls naming stat hack - // type. Also using decls exporting something from the same scope. + // type. tree current = binding; unsigned count = 0; @@ -5238,13 +5238,36 @@ do_nonmember_using_decl (name_lookup &lookup, bool fn_scope_p, } else if (insert_p) { - value = lookup.value; - if (revealing_p && module_exporting_p ()) - check_can_export_using_decl (value); + if (revealing_p + && module_exporting_p () + && check_can_export_using_decl (lookup.value) + && lookup.value == value + && !DECL_MODULE_EXPORT_P (value)) + { + /* We're redeclaring the same value, but this time as + newly exported: make sure to mark it as such. */ + if (TREE_CODE (value) == TEMPLATE_DECL) + { + DECL_MODULE_EXPORT_P (value) = true; + + tree result = DECL_TEMPLATE_RESULT (value); + retrofit_lang_decl (result); + DECL_MODULE_PURVIEW_P (result) = true; + DECL_MODULE_EXPORT_P (result) = true; + } + else + { + retrofit_lang_decl (value); + DECL_MODULE_PURVIEW_P (value) = true; + DECL_MODULE_EXPORT_P (value) = true; + } + } + else + value = lookup.value; } /* Now the type binding. */ - if (lookup.type && lookup.type != type) + if (lookup.type) { if (type && !decls_match (lookup.type, type)) { @@ -5253,9 +5276,20 @@ do_nonmember_using_decl (name_lookup &lookup, bool fn_scope_p, } else if (insert_p) { - type = lookup.type; - if (revealing_p && module_exporting_p ()) - check_can_export_using_decl (type); + if (revealing_p + && module_exporting_p () + && check_can_export_using_decl (lookup.type) + && lookup.type == type + && !DECL_MODULE_EXPORT_P (type)) + { + /* We're redeclaring the same type, but this time as + newly exported: make sure to mark it as such. */ + retrofit_lang_decl (type); + DECL_MODULE_PURVIEW_P (type) = true; + DECL_MODULE_EXPORT_P (type) = true; + } + else + type = lookup.type; } } diff --git a/gcc/testsuite/g++.dg/modules/using-12.C b/gcc/testsuite/g++.dg/modules/using-12.C new file mode 100644 index 00000000000..54eacf7276e --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/using-12.C @@ -0,0 +1,73 @@ +// { dg-additional-options "-fmodules-ts" } +// { dg-module-cmi !bad } + +// Like using-10.C, but test exporting names within the same namespace. + +export module bad; + +// internal linkage +namespace s { + namespace { + struct a1 {}; // { dg-message "declared here with internal linkage" } + + template + struct b1; // { dg-message "declared here with internal linkage" } + + int x1; // { dg-message "declared here with internal linkage" } + + template + T y1; // { dg-message "declared here with internal linkage" } + + void f1(); // { dg-message "declared here with internal linkage" } + + template + void g1(); // { dg-message "declared here with internal linkage" } + + export using s::a1; // { dg-error "does not have external linkage" } + export using s::b1; // { dg-error "does not have external linkage" } + export using s::x1; // { dg-error "does not have external linkage" } + export using s::y1; // { dg-error "does not have external linkage" } + export using s::f1; // { dg-error "does not have external linkage" } + export using s::g1; // { dg-error "does not have external linkage" } + } +} + +// module linkage +namespace m { + struct a2 {}; // { dg-message "declared here with module linkage" } + + template + struct b2; // { dg-message "declared here with module linkage" } + + int x2; // { dg-message "declared here with module linkage" } + + template + T y2; // { dg-message "declared here with module linkage" } + + void f2(); // { dg-message "declared here with module linkage" } + + template + void g2(); // { dg-message "declared here with module linkage" } + + export using m::a2; // { dg-error "does not have external linkage" } + export using m::b2; // { dg-error "does not have external linkage" } + export using m::x2; // { dg-error "does not have external linkage" } + export using m::y2; // { dg-error "does not have external linkage" } + export using m::f2; // { dg-error "does not have external linkage" } + export using m::g2; // { dg-error "does not have external linkage" } +} + +namespace t { + using a = int; // { dg-message "declared here with no linkage" } + + template + using b = int; // { dg-message "declared here with no linkage" } + + typedef int c; // { dg-message "declared here with no linkage" } + + export using t::a; // { dg-error "does not have external linkage" } + export using t::b; // { dg-error "does not have external linkage" } + export using t::c; // { dg-error "does not have external linkage" } +} + +// { dg-prune-output "not writing module" } diff --git a/gcc/testsuite/g++.dg/modules/using-13.h b/gcc/testsuite/g++.dg/modules/using-13.h new file mode 100644 index 00000000000..b8ef2a11cc4 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/using-13.h @@ -0,0 +1,16 @@ +// Like using-11.h, but additional kinds of declarations. + +struct A {}; + +template struct B {}; +template <> struct B { using foo = int; }; +template struct B { using bar = T; }; + +using C = int; + +inline int D = 0; + +#if __cpp_concepts >= 201907L +template +concept E = true; +#endif diff --git a/gcc/testsuite/g++.dg/modules/using-13_a.C b/gcc/testsuite/g++.dg/modules/using-13_a.C new file mode 100644 index 00000000000..fed33ac2333 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/using-13_a.C @@ -0,0 +1,15 @@ +// { dg-additional-options "-fmodules-ts" } +// { dg-module-cmi M } + +module; +#include "using-13.h" + +export module M; +export using ::A; +export using ::B; +export using ::C; +export using ::D; + +#if __cpp_concepts >= 201907L +export using ::E; +#endif diff --git a/gcc/testsuite/g++.dg/modules/using-13_b.C b/gcc/testsuite/g++.dg/modules/using-13_b.C new file mode 100644 index 00000000000..49fa09d39ee --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/using-13_b.C @@ -0,0 +1,20 @@ +// { dg-additional-options "-fmodules-ts" } + +import M; + +int main() { + A a; + + // Check all specialisations are correctly exported + B b; + B::foo b1; + B::bar b2; + + C c; + + auto d = D; + +#if __cpp_concepts >= 201907L + auto e = E; +#endif +}