From patchwork Thu May 23 13:27:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 1938347 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=KYr4N7qG; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4VlTW91GTGz1ynR for ; Thu, 23 May 2024 23:27:49 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 7351F3865490 for ; Thu, 23 May 2024 13:27:47 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-oo1-xc35.google.com (mail-oo1-xc35.google.com [IPv6:2607:f8b0:4864:20::c35]) by sourceware.org (Postfix) with ESMTPS id 0307A3858D38 for ; Thu, 23 May 2024 13:27:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0307A3858D38 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 0307A3858D38 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::c35 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716470847; cv=none; b=Y4NL0SD7qBkQ6r8HfP6PRIvj3ZAXzhS98G6mIiWtlLUz8pxc3bwbTnKXZHptTOt/Yv1nct3JSxB3QfWmYnVVi680VLc+3OvDQd1smrdYR8WEq9VBm9YslKRxMteIKiYF8Vj99O7fTW7XE1Th58DFSnj8KjtIqTbTIc3dXUDMqyo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716470847; c=relaxed/simple; bh=L6IijXIKVR76qAKOomCROLfBBhBxgDrJsu15x2FLooo=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=fw2UVpsprWON6qS0Lhuz6QhRmLDVc58aJKVpSTWAMO2b13rM2GvRMWn9lPlN/VsOiLkQRJEblbcuGjEap9GjbQF1OKcrNx74Q8zV0NE+sElaouYQgXsU1Jh9FsRcivBnzT034eIFR/zxfgxaK7Ou/5Auptzh9H3jQfLhrkacdDU= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-oo1-xc35.google.com with SMTP id 006d021491bc7-5b27c5603ddso3762149eaf.1 for ; Thu, 23 May 2024 06:27:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716470844; x=1717075644; darn=gcc.gnu.org; h=in-reply-to:content-disposition:mime-version:references:subject:cc :to:from:date:message-id:from:to:cc:subject:date:message-id:reply-to; bh=5k9K1vqEVS22/VzeGhFA2a1KjY82DJYiZm7NkZfrX4Y=; b=KYr4N7qGmzB8W/g3OCb6nScp2u5BKTd/ckTne1r51VFbdmQE8JDNG7PjQk4HepnpP2 IEjY8Zz1JNNQCbja4tp1NbkUUZyhnL/72/NPXOWl2eBEUEW4mFoJmrpMTAPnKlq6dStZ ZPkI4mmr6XVq/V90CgkNdAmX33YxlXkTpsxR+/zzdRM7JWsJgIsOn73Lhd0BuhZIT/6E uLg2D39moJd/PU8TcY4heBe3nmPqg73deQIiVNQmWxeHaRO0vj+kjX+Zth35JIZ7xLX4 C5yCqwjadlc9rV2zS9RBux7ShyqokGVn0qMDc8m5HFHvlCFaFhzJFaIYhWSz9RgcoCHK prfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716470844; x=1717075644; h=in-reply-to:content-disposition:mime-version:references:subject:cc :to:from:date:message-id:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=5k9K1vqEVS22/VzeGhFA2a1KjY82DJYiZm7NkZfrX4Y=; b=UBRNEJmCpRFlhMqMMNJWuULaEemA59LXYs8Go8iMBdVG5QwThG2AE/GNlu37r9fbc3 iza1QQAHjeKU3vhS8I9auyH13BLwgAIXeQLc0a29shWE1OPOExdUrP0w1THfQXqW1My7 zlZxgClOo8ttW4xOoUURJzDQZAQNxM+ttP8B0iyuBuCm88wjaQJMkzfoThpmny/7dGne UUJ6Arii+IF6oh88cSfeDBILZYJo9/hWDWwWQXg0+wjg6RiL7+KiGNA4C6XayGx7nCWa fsr7kW+ArpP9wn8z2+p7RFoMk8TOnMEEUw+z/hRtpaV/ZarMUFQX18IZgvDm1i7EzMgU dmEw== X-Gm-Message-State: AOJu0YzaCNmlc0u70CSszZ098RNddB9IpSJhcpni/8JtcEwqbal2QgLq IU3bIyEFibgDrntQXLH+4lWpHFV+AougiDGcQbXkrXBbHGxHbS2t X-Google-Smtp-Source: AGHT+IF2Spdj0Ckm0Z62PGf/t+qfPCcdx/6rAxrx8HTskRkEt6CwusBB/8NkPGSK5aqj+a6x1hKV4w== X-Received: by 2002:a05:6358:cc16:b0:192:9d35:b027 with SMTP id e5c5f4694b2df-19791b77aa7mr411102555d.15.1716470843925; Thu, 23 May 2024 06:27:23 -0700 (PDT) Received: from Thaum. (14-200-72-253.tpgi.com.au. [14.200.72.253]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2a66590sm25089010b3a.1.2024.05.23.06.27.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 May 2024 06:27:23 -0700 (PDT) Message-ID: <664f443b.620a0220.3bb2.27d7@mx.google.com> X-Google-Original-Message-ID: Date: Thu, 23 May 2024 23:27:18 +1000 From: Nathaniel Shead To: Jason Merrill Cc: gcc-patches@gcc.gnu.org, Nathan Sidwell Subject: [PATCH 1/2] c++/modules: Fix treatment of unnamed types References: <664181e2.650a0220.dbfd8.e3d5@mx.google.com> <10d5ff7c-3d30-4317-97d9-91ea2370bbfa@redhat.com> <6646f5cb.170a0220.fb17d.75a5@mx.google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, WEIRD_PORT 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 On Mon, May 20, 2024 at 06:00:09PM -0400, Jason Merrill wrote: > On 5/17/24 02:14, Nathaniel Shead wrote: > > On Tue, May 14, 2024 at 06:21:48PM -0400, Jason Merrill wrote: > > > On 5/12/24 22:58, Nathaniel Shead wrote: > > > > Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? > > > > > > OK. > > > > > > > I realised as I was looking over this again that I might have spoken too > > soon with the header unit example being supported. Doing the following: > > > > // a.H > > struct { int y; } s; > > decltype(s) f(decltype(s)); // { dg-error "used but never defined" } > > inline auto x = f({ 123 }); > > // b.C > > struct {} unrelated; > > import "a.H"; > > decltype(s) f(decltype(s) x) { > > return { 456 + x.y }; > > } > > > > // c.C > > import "linkage-3_a.H"; > > int main() { auto a = x.y; } > > > > Actually does fail to link, because in 'c.C' we call 'f(.anon_0)' but > > the definition 'b.C' is f(.anon_1). > > > > I don't think this is fixable, so I don't think this direction is > > workable. > > Since namespace-scope anonymous types are TU-local, we don't need to support > that for proper modules, but it's not clear to me that we don't need to > support it for header units. > > OTOH, https://eel.is/c++draft/module#import-5.3 allows c.C to import a > different header unit than b.C, in which case the type is different and x > violates the odr. > Right; I think at this stage I don't know how to support this for header units (and even for module interface units it doesn't actually work; more on this below), so I think saying that this is actually an ODR violation is OK. > > That said, I think that it might still be worth making header modules > > satisfy 'module_has_cmi_p', since that is true to the name, and will be > > useful in other places we currently use 'module_p ()': in which case we > > could instead make all the callers in 'no_linkage_check' do > > 'module_maybe_has_cmi_p () && !header_module_p ()'; something like the > > following, perhaps? > > If we need that condition, it should be its own predicate rather than > expecting callers to do that combined check. > > But it's not clear to me how this is different from a type in the GMF of a > named module, which is exactly the maybe_has_cmi case; there we could again > see a different version of the type if another TU includes the header. > > Jason > This made me go back and double-check for named modules and it actually does fail as well; the following sample ICEs, even: export module M; struct {} s; int h(decltype(s)); int x = h(s); // ICE in write_unnamed_type_name, cp/mangle.cc:1806 So I think maybe the way to go here is to just not treat unnamed types as something that could possibly be accessed from a different TU, like the below. Then we don't need to do the special handling for header units, since as you say, they're not materially different anyway. Thoughts? (And I've moved the original change to 'module_has_cmi_p' to a separate patch given it's somewhat unrelated now.) Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk (and maybe 14.2)? -- >8 -- In r14-9530 we relaxed "depending on type with no-linkage" errors for declarations that could actually be accessed from different TUs anyway. However, this also enabled it for unnamed types, which never work. In a normal module interface, an unnamed type is TU-local by [basic.link] p15.2, and so cannot be exposed or the program is ill-formed. We don't yet implement this checking but we should assume that we will later; currently supporting this actually causes ICEs when attempting to create the mangled name in some situations. For a header unit, by [module.import] p5.3 it is unspecified whether two TUs importing a header unit providing such a declaration are importing the same header unit. In this case, we would require name mangling changes to somehow allow the (anonymous) type exported by such a header unit to correspond across different TUs in the presence of other anonymous declarations, so for this patch just assume that this case would be an ODR violation instead. gcc/cp/ChangeLog: * tree.cc (no_linkage_check): Anonymous types can't be accessed in a different TU. gcc/testsuite/ChangeLog: * g++.dg/modules/linkage-1_a.C: Remove anonymous type test. * g++.dg/modules/linkage-1_b.C: Likewise. * g++.dg/modules/linkage-1_c.C: Likewise. * g++.dg/modules/linkage-2.C: Add note about anonymous types. Signed-off-by: Nathaniel Shead Signed-off-by: Nathaniel Shead --- gcc/cp/tree.cc | 10 +--------- gcc/testsuite/g++.dg/modules/linkage-1_a.C | 4 ---- gcc/testsuite/g++.dg/modules/linkage-1_b.C | 1 - gcc/testsuite/g++.dg/modules/linkage-1_c.C | 1 - gcc/testsuite/g++.dg/modules/linkage-2.C | 9 +++++---- 5 files changed, 6 insertions(+), 19 deletions(-) diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index 9d37d255d8d..e2d0d3229c1 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -3016,15 +3016,7 @@ no_linkage_check (tree t, bool relaxed_p) /* Only treat unnamed types as having no linkage if they're at namespace scope. This is core issue 966. */ if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t)) - { - if (relaxed_p - && TREE_PUBLIC (CP_TYPE_CONTEXT (t)) - && module_maybe_has_cmi_p ()) - /* This type could possibly be accessed outside this TU. */ - return NULL_TREE; - else - return t; - } + return t; for (r = CP_TYPE_CONTEXT (t); ; ) { diff --git a/gcc/testsuite/g++.dg/modules/linkage-1_a.C b/gcc/testsuite/g++.dg/modules/linkage-1_a.C index 750e31ff347..1d81312e94f 100644 --- a/gcc/testsuite/g++.dg/modules/linkage-1_a.C +++ b/gcc/testsuite/g++.dg/modules/linkage-1_a.C @@ -9,7 +9,3 @@ auto f() { } decltype(f()) g(); // { dg-warning "used but not defined" "" { target c++17_down } } export auto x = g(); - -struct {} s; -decltype(s) h(); // { dg-warning "used but not defined" "" { target c++17_down } } -export auto y = h(); diff --git a/gcc/testsuite/g++.dg/modules/linkage-1_b.C b/gcc/testsuite/g++.dg/modules/linkage-1_b.C index f23962d76b7..5bc0d1b888d 100644 --- a/gcc/testsuite/g++.dg/modules/linkage-1_b.C +++ b/gcc/testsuite/g++.dg/modules/linkage-1_b.C @@ -3,4 +3,3 @@ module M; decltype(f()) g() { return {}; } -decltype(s) h() { return {}; } diff --git a/gcc/testsuite/g++.dg/modules/linkage-1_c.C b/gcc/testsuite/g++.dg/modules/linkage-1_c.C index f1406b99032..9ff1491b67e 100644 --- a/gcc/testsuite/g++.dg/modules/linkage-1_c.C +++ b/gcc/testsuite/g++.dg/modules/linkage-1_c.C @@ -5,5 +5,4 @@ import M; int main() { auto a = x; - auto b = y; } diff --git a/gcc/testsuite/g++.dg/modules/linkage-2.C b/gcc/testsuite/g++.dg/modules/linkage-2.C index eb4d7b051af..f69bd7ff728 100644 --- a/gcc/testsuite/g++.dg/modules/linkage-2.C +++ b/gcc/testsuite/g++.dg/modules/linkage-2.C @@ -13,14 +13,15 @@ namespace { return A{}; } decltype(f()) g(); // { dg-error "used but never defined" } - - struct {} s; - decltype(s) h(); // { dg-error "used but never defined" } } export void use() { g(); - h(); } +// Additionally, unnamed types have no linkage but are also TU-local, +// and thus cannot be in a module interface unit. +// We don't yet implement this checking however. +struct {} s; // { dg-error "TU-local" "" { xfail *-*-* } } + // { dg-prune-output "not writing module" }