From patchwork Sun Aug 18 01:45:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 1973518 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20230601 header.b=g3wWvIVZ; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wmdqw0WrNz1yXZ for ; Sun, 18 Aug 2024 11:46:34 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F1506386102A for ; Sun, 18 Aug 2024 01:46:32 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oo1-xc2f.google.com (mail-oo1-xc2f.google.com [IPv6:2607:f8b0:4864:20::c2f]) by sourceware.org (Postfix) with ESMTPS id B192F3858402 for ; Sun, 18 Aug 2024 01:45:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B192F3858402 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 B192F3858402 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::c2f ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723945518; cv=none; b=cZvDog9mXzHsDnz5vca+BxtirVPnmLDI34a72//ACZTABByAuz1xBhXGuahYPxn9vHF10azt8NgXiXtiOaAd/HsOqqkx8m/aRIi3XfXV8QCqEVEoWEJu1zg8I1Ny4KZwQ2ZHiwH2grJrFUhRFAFQ/gAn9bt1LUJFFFzUb3FvEDE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1723945518; c=relaxed/simple; bh=sDLuxVWZli931WTxtPQ+8gtah4vMuO/bf9BUfajdJto=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=FZ6UzcKp8WL1LYWLpBuYdx6DmR6Z/4ZzZ6jDFkGIPSFQ8Ga9SO7dXCCamd7u4GWiDq5wtfOgQafzp1Y1wLZ2WMCMClqrdLT2RkT2bY+SpWh5OnTIcgDnYYxX69EhlJ8Z3Bcp3wOXZvZiPYIK7mfNC3/zsWVEwQ2aBm4GmwJuVq8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oo1-xc2f.google.com with SMTP id 006d021491bc7-5d5c7f23f22so1999369eaf.0 for ; Sat, 17 Aug 2024 18:45:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1723945507; x=1724550307; 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=4DKofbNa3kCaBlI1ActKXXzSlaoL0P5m5qFLe99BSqM=; b=g3wWvIVZzVkHTtLkqnsFT64eUw2b/4jNcR79YXnjljjEcSKzke0PmJVJfZt/jN3KQH pT7PJ7kf8AIj5/Y9cS+fo5o6ssPJOojkMfuvwLDrzO3eB2Qn+1RV/2kX8LMFQblmVC4u z6cLP27MaZojwvI+8iaDqfEw/pH5lMN2ftnlfdEd3+tMQFFg9491oVZMscozrg6E64AO kFtmg7trO0qQG7DDpPeRjp4TV65v5HcUW3KsTiLZAkRhqBrLFd28o+2IZS6n8a8Hel1r PkNRE24vis9SbhV2Y4SMxnZM8yBNc1xy3UnWvEjqagulRk78COXVNxMnUQJ985pUOg+E wUIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723945507; x=1724550307; 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=4DKofbNa3kCaBlI1ActKXXzSlaoL0P5m5qFLe99BSqM=; b=Sb1sDmasCzxRmjfkabQETnv62IztT62Q1nLPStcW0OwLHR9u0kI1XomQoKYWObfvPG ttgzSQdNe9IFsA6MUiIh8om8lZO0TA2oArZcnASkkfkDeyi1jU8Y/D3/1owVPV6Kdsip xNiaAjZz/B9iF0HnEDHcemH1fZswx14Ibp/uesRc31RT90jUn5qmqWNaXvFWO4ZKSiT5 wVGzZcyU719466aGe0EDozurlY1dZS/hqi7Rlb+AtHT0YsGqlYd3/qBTOLHx/b9xP99O wxApTySFvfRlWs8tXh/NNGRCjpuH2ylbq7wXMS2Vg1JxCB0brxtXL6J46B8GKQJ/4TRu DVMg== X-Gm-Message-State: AOJu0Yw3gnaGRWcxDRAfCfk73hDagDjnXFsmYVgOmPzX8QmGMRENo9cV NogippqaxJvFe30veiPO8PQlAdava/7AzOznp10Q1DUKx+7wTZ9stC2wTA== X-Google-Smtp-Source: AGHT+IG9OtNsODvz8aGpL9UECJKlBDo2Bkp7I9JGOwmxgE+5tidiS/pThruKebqJyJIMrChG1qUgHQ== X-Received: by 2002:a05:6358:528a:b0:1b1:acf1:3272 with SMTP id e5c5f4694b2df-1b3931c0ce6mr1002360955d.18.1723945507190; Sat, 17 Aug 2024 18:45:07 -0700 (PDT) Received: from Thaum. (110-175-236-231.static.tpgi.com.au. [110.175.236.231]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-7127addea8asm4739027b3a.11.2024.08.17.18.45.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Aug 2024 18:45:06 -0700 (PDT) Message-ID: <66c15222.620a0220.48a2.fa9e@mx.google.com> X-Google-Original-Message-ID: Date: Sun, 18 Aug 2024 11:45:02 +1000 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH] c++/modules: Handle transitive reachability for deduction guides [PR116403] MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-12.2 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~incoming=patchwork.ozlabs.org@gcc.gnu.org Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? -- >8 -- Currently we implement [temp.deduct.guide] p1 by forcing all deduction guides to be considered as exported. However this is not sufficient: for transitive non-exported imports we will still hide the deduction guide from name lookup, causing errors. This patch instead adjusts name lookup to have a new ANY_REACHABLE flag to allow for this case. Currently this is only used by deduction guides but there are some other circumstances where this may be useful in the future (e.g. finding existing temploid friends). PR c++/116403 gcc/cp/ChangeLog: * pt.cc (deduction_guides_for): Use ANY_REACHABLE for lookup of deduction guides. * module.cc (depset::hash::add_deduction_guides): Likewise. (module_state::write_cluster): No longer override deduction guides as exported. * name-lookup.cc (name_lookup::search_namespace_only): Ignore visibility when LOOK_want::ANY_REACHABLE is specified. (check_module_override): Ignore visibility when checking for ambiguating deduction guides. * name-lookup.h (LOOK_want): New flag 'ANY_REACHABLE'. gcc/testsuite/ChangeLog: * g++.dg/modules/dguide-4_a.C: New test. * g++.dg/modules/dguide-4_b.C: New test. * g++.dg/modules/dguide-4_c.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/module.cc | 7 +---- gcc/cp/name-lookup.cc | 38 ++++++++++++++++++----- gcc/cp/name-lookup.h | 5 ++- gcc/cp/pt.cc | 3 +- gcc/testsuite/g++.dg/modules/dguide-4_a.C | 18 +++++++++++ gcc/testsuite/g++.dg/modules/dguide-4_b.C | 9 ++++++ gcc/testsuite/g++.dg/modules/dguide-4_c.C | 15 +++++++++ 7 files changed, 79 insertions(+), 16 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/dguide-4_a.C create mode 100644 gcc/testsuite/g++.dg/modules/dguide-4_b.C create mode 100644 gcc/testsuite/g++.dg/modules/dguide-4_c.C diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index f4d137b13a1..6eb4cbf2911 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -13640,7 +13640,7 @@ depset::hash::add_deduction_guides (tree decl) if (find_binding (ns, name)) return; - tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL, + tree guides = lookup_qualified_name (ns, name, LOOK_want::ANY_REACHABLE, /*complain=*/false); if (guides == error_mark_node) return; @@ -15223,11 +15223,6 @@ module_state::write_cluster (elf_out *to, depset *scc[], unsigned size, flags |= cbf_hidden; else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound))) flags |= cbf_export; - else if (deduction_guide_p (bound)) - /* Deduction guides are always exported so that they are - visible to name lookup whenever their class template - is reachable. */ - flags |= cbf_export; } gcc_checking_assert (DECL_P (bound)); diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc index 872f1af0b2e..70ad4cbf3b5 100644 --- a/gcc/cp/name-lookup.cc +++ b/gcc/cp/name-lookup.cc @@ -916,7 +916,8 @@ name_lookup::search_namespace_only (tree scope) if (unsigned base = cluster->indices[jx].base) if (unsigned span = cluster->indices[jx].span) do - if (bitmap_bit_p (imports, base)) + if (bool (want & LOOK_want::ANY_REACHABLE) + || bitmap_bit_p (imports, base)) goto found; while (++base, --span); continue; @@ -960,9 +961,17 @@ name_lookup::search_namespace_only (tree scope) dup_detect |= dup; } - if (STAT_TYPE_VISIBLE_P (bind)) - type = STAT_TYPE (bind); - bind = STAT_VISIBLE (bind); + if (bool (want & LOOK_want::ANY_REACHABLE)) + { + type = STAT_TYPE (bind); + bind = STAT_DECL (bind); + } + else + { + if (STAT_TYPE_VISIBLE_P (bind)) + type = STAT_TYPE (bind); + bind = STAT_VISIBLE (bind); + } } /* And process it. */ @@ -3761,6 +3770,10 @@ check_module_override (tree decl, tree mvec, bool hiding, tree nontmpl = STRIP_TEMPLATE (decl); bool attached = DECL_LANG_SPECIFIC (nontmpl) && DECL_MODULE_ATTACH_P (nontmpl); + /* For deduction guides we don't do normal name lookup, but rather consider + any reachable declaration, so we should check for overriding here too. */ + bool any_reachable = deduction_guide_p (decl); + if (BINDING_VECTOR_SLOTS_PER_CLUSTER == BINDING_SLOTS_FIXED) { cluster++; @@ -3775,7 +3788,8 @@ check_module_override (tree decl, tree mvec, bool hiding, continue; if (!cluster->indices[jx].base) continue; - if (!bitmap_bit_p (imports, cluster->indices[jx].base)) + if (!any_reachable + && !bitmap_bit_p (imports, cluster->indices[jx].base)) continue; /* Is it loaded? */ if (cluster->slots[jx].is_lazy ()) @@ -3795,9 +3809,17 @@ check_module_override (tree decl, tree mvec, bool hiding, /* If there was a matching STAT_TYPE here then xref_tag should have found it, but we need to check anyway because a conflicting using-declaration may exist. */ - if (STAT_TYPE_VISIBLE_P (bind)) - type = STAT_TYPE (bind); - bind = STAT_VISIBLE (bind); + if (any_reachable) + { + type = STAT_TYPE (bind); + bind = STAT_DECL (bind); + } + else + { + if (STAT_TYPE_VISIBLE_P (bind)) + type = STAT_TYPE (bind); + bind = STAT_VISIBLE (bind); + } } if (type) diff --git a/gcc/cp/name-lookup.h b/gcc/cp/name-lookup.h index 7c4193444dd..f39f5309150 100644 --- a/gcc/cp/name-lookup.h +++ b/gcc/cp/name-lookup.h @@ -385,9 +385,12 @@ enum class LOOK_want TYPE = 1 << 1, /* We only want TYPE_DECLS. */ NAMESPACE = 1 << 2, /* We only want NAMESPACE_DECLS. */ - HIDDEN_FRIEND = 1 << 3, /* See hidden friends. */ + HIDDEN_FRIEND = 1 << 3, /* See hidden friends. */ HIDDEN_LAMBDA = 1 << 4, /* See lambda-ignored entities. */ + ANY_REACHABLE = 1 << 5, /* Include reachable module declarations not + normally visible to name lookup. */ + TYPE_NAMESPACE = TYPE | NAMESPACE, /* Either NAMESPACE or TYPE. */ }; constexpr LOOK_want operator| (LOOK_want a, LOOK_want b) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 8725a5eeb3f..5c6f4e7f7ff 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -30731,7 +30731,8 @@ deduction_guides_for (tree tmpl, bool &any_dguides_p, tsubst_flags_t complain) { guides = lookup_qualified_name (CP_DECL_CONTEXT (tmpl), dguide_name (tmpl), - LOOK_want::NORMAL, /*complain*/false); + LOOK_want::ANY_REACHABLE, + /*complain=*/false); if (guides == error_mark_node) guides = NULL_TREE; else diff --git a/gcc/testsuite/g++.dg/modules/dguide-4_a.C b/gcc/testsuite/g++.dg/modules/dguide-4_a.C new file mode 100644 index 00000000000..5a9da44cdd6 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/dguide-4_a.C @@ -0,0 +1,18 @@ +// PR c++/116403 +// { dg-additional-options "-fmodules-ts -Wno-global-module" } +// { dg-module-cmi A } + +module; + +template struct GMF { + GMF(int); +}; +GMF(int) -> GMF; + +export module A; +export using ::GMF; + +export template struct Attached { + Attached(int); +}; +Attached(int) -> Attached; diff --git a/gcc/testsuite/g++.dg/modules/dguide-4_b.C b/gcc/testsuite/g++.dg/modules/dguide-4_b.C new file mode 100644 index 00000000000..9276ffbb3c8 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/dguide-4_b.C @@ -0,0 +1,9 @@ +// PR c++/116403 +// { dg-additional-options "-fmodules-ts" } +// { dg-module-cmi B } + +export module B; +import A; // not exported + +export using ::GMF; +export using ::Attached; diff --git a/gcc/testsuite/g++.dg/modules/dguide-4_c.C b/gcc/testsuite/g++.dg/modules/dguide-4_c.C new file mode 100644 index 00000000000..898ffa6ac81 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/dguide-4_c.C @@ -0,0 +1,15 @@ +// PR c++/116403 +// { dg-additional-options "-fmodules-ts" } + +import B; + +int main() { + GMF gmf(123); + GMF test_gmf = gmf; + + Attached attached(456); + Attached test_attached = attached; +} + +GMF(int) -> GMF; // { dg-error "ambiguating" } +Attached(int) -> Attached; // { dg-error "ambiguating" }