From patchwork Sat Nov 9 08:22:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 2008871 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=KkO2BtRS; 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 4Xlpj31dx7z1xyr for ; Sat, 9 Nov 2024 19:23:01 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 21A743858CD1 for ; Sat, 9 Nov 2024 08:23:00 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pf1-x42e.google.com (mail-pf1-x42e.google.com [IPv6:2607:f8b0:4864:20::42e]) by sourceware.org (Postfix) with ESMTPS id 53A5B3858D20 for ; Sat, 9 Nov 2024 08:22:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 53A5B3858D20 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 53A5B3858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::42e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731140560; cv=none; b=UyS7zxckbSurTn+cm5YOT299GOAZYUVvStAV2UVvEwb+MTNxAwTm8jbVi0o8BDtN39d0Fn1x4qQ/GVgIsiPRfqrkn9laMKN9BI+XhWvuIY/8WYaFjVqCQNGC0TmWpmO1Tw/yvQCbGqAA7RWQp0fbblY0g8SzjE0nlX4YiNqINvY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731140560; c=relaxed/simple; bh=JeCiv53WJz8ggP3oVGpz9FinTqHBkofIyaSgo+X7z/U=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=pH8DReqggASS34mdDQWaa1TYixvs4AiZ8qGt1fYBH3IX+JyLEG4eAWzQqDEjyQmGGFbIL2qcFxTth7ZrhZJxazwWgwy7et651HqmtY2gSCHLTBJOFW/E4m28MVyfydAXePh9cEmh6T3h7D4UMe95mNlRpn6pkigK4KWclccWxWk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pf1-x42e.google.com with SMTP id d2e1a72fcca58-71e49d5deeeso206854b3a.0 for ; Sat, 09 Nov 2024 00:22:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731140555; x=1731745355; 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=+JZdvQ1Wx5KWGX0j6hM/brPIh9MTaa8MK7RES/1yFsM=; b=KkO2BtRS4rxmlmHZIfKR4gKoNDMkA2XQ4J7AJfZyewLSvH74+8zjWtEc3+LDnXOKhQ ILUSLb0qyC1SbjOo7xdPnSiLCID3IOhVPh9xQSEEW6C+VTG5EYm5uNt5ADqy5DpL+qG8 XxlgnrRjFnQfNVMoMTgSsj3PeIKRQ402cLpoBR5DQHmhlfRgciOJ1e9B0DwkZwWGiKgX tOAOVL9X7tlCLqOF3QtQlfBfCSTboB/9kRPK9SIx1If6xIQhOPH0/Sv3FwM0sWnUngCP keFr3uhdaE03i+vrnZkgmZadX/hh2/cAE6At/+aywtp0E+RwsgztTQLs1AOnAqxqgESQ D7Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731140555; x=1731745355; 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=+JZdvQ1Wx5KWGX0j6hM/brPIh9MTaa8MK7RES/1yFsM=; b=IAar2RHFo6EmW9IuONx4kztij4Jj4HDPh8hfXXSPTP+9He5YFZDSkEudfJs5/nBxwm nD57eVCzayn+xEQXYJ3i5uL5Mxus8Ks+1nBzUx9hY3y9232/ESctfCPKdM+4U8czQJD/ OJ8PI/D18hvGtQKbc8OEKWN8mfA1Psd5LnCmVX1NlUCN0Qk9ZTDOkLJaU0dR1QldWqvm n5H7HC9Z+Q6G/H9Lw9HMAG7MipUi6MMRgwSMCZ6+noSVQWk2TkqIF2C1T6Jg6IxmKSRb vV2YVwBrNqsAIkm5///xWE71B2UktpQg4n3aXd4sO7niiwQAno+5+koXRhrzmpRFHIWg L84A== X-Gm-Message-State: AOJu0Yw563qeSI6+sMPe+nVxxzH0fBtE8P+ZY4eaXXarr/qWIt8BuqCL O4Q++Nk/kB9CUDmvppUOmrx8rI3GAK3opcDw+Jm1BUSETS+cc2IFRlbg0g== X-Google-Smtp-Source: AGHT+IFdhbzcxILfd0uN+VJosiJVJ577INH4idAZ8fs9FfkshfGJGmM9zNqSBkNINZ9o7ThZgvDOzQ== X-Received: by 2002:a05:6a00:2da5:b0:71e:4a06:8a62 with SMTP id d2e1a72fcca58-7241339a53dmr3275889b3a.7.1731140555119; Sat, 09 Nov 2024 00:22:35 -0800 (PST) Received: from Thaum. ([163.47.68.2]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-724078a9058sm5065044b3a.77.2024.11.09.00.22.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Nov 2024 00:22:34 -0800 (PST) Message-ID: <672f1bca.050a0220.1f2cda.8a47@mx.google.com> X-Google-Original-Message-ID: Date: Sat, 9 Nov 2024 19:22:29 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill Subject: [PATCH v2 1/3] c++: Fix mangling of otherwise unattached class-scope lambdas [PR107741] MIME-Version: 1.0 Content-Disposition: inline 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? Given that this doesn't actually fix the modules PR c++/116568 anymore I've pulled my workaround for that out as a separate patch (3/3). -- >8 -- This is a step closer to implementing the suggested changes for https://github.com/itanium-cxx-abi/cxx-abi/pull/85. Most lambdas defined within a class should have an extra scope of that class so that uses across different TUs are properly merged by the linker. This also needs to happen during template instantiation. While I was working on this I found some other cases where the mangling of lambdas was incorrect and causing issues, notably the testcase lambda-ctx3.C which currently emits the same mangling for the base class and member lambdas, causing mysterious assembler errors. However, this doesn't fix the A::x case of the linker PR at this time so I've left that as an XFAIL. One notable case not handled either here or in the ABI is what is supposed to happen with lambdas declared in alias templates; see lambda-ctx4.C. I believe that by the C++ standard, such lambdas should also dedup across TUs, but this isn't currently implemented (for class-scope or not). I wasn't able to work out how to fix the mangling logic for this case easily so I've just excluded alias templates from the class-scope mangling rules in template instantiation. PR c++/107741 gcc/cp/ChangeLog: * cp-tree.h (LAMBDA_EXPR_EXTRA_SCOPE): Adjust comment. * parser.cc (cp_parser_class_head): Start (and do not finish) lambda scope for all valid types. (cp_parser_class_specifier): Finish lambda scope after parsing members instead. (cp_parser_member_declaration): Adjust comment to mention missing lambda scoping for static member initializers. * pt.cc (instantiate_class_template): Add lambda scoping. (instantiate_template): Likewise. gcc/testsuite/ChangeLog: * g++.dg/abi/lambda-ctx2.C: New test. * g++.dg/abi/lambda-ctx3.C: New test. * g++.dg/abi/lambda-ctx4.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/cp-tree.h | 3 ++- gcc/cp/parser.cc | 31 ++++++++++++++--------- gcc/cp/pt.cc | 14 ++++++++++- gcc/testsuite/g++.dg/abi/lambda-ctx2.C | 34 ++++++++++++++++++++++++++ gcc/testsuite/g++.dg/abi/lambda-ctx3.C | 21 ++++++++++++++++ gcc/testsuite/g++.dg/abi/lambda-ctx4.C | 22 +++++++++++++++++ 6 files changed, 111 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/g++.dg/abi/lambda-ctx2.C create mode 100644 gcc/testsuite/g++.dg/abi/lambda-ctx3.C create mode 100644 gcc/testsuite/g++.dg/abi/lambda-ctx4.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index f98a1de42ca..f6cf1754d86 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -1513,7 +1513,8 @@ enum cp_lambda_default_capture_mode_type { (((struct tree_lambda_expr *)LAMBDA_EXPR_CHECK (NODE))->locus) /* The mangling scope for the lambda: FUNCTION_DECL, PARM_DECL, VAR_DECL, - FIELD_DECL or NULL_TREE. If this is NULL_TREE, we have no linkage. */ + FIELD_DECL, TYPE_DECL, or NULL_TREE. If this is NULL_TREE, we have no + linkage. */ #define LAMBDA_EXPR_EXTRA_SCOPE(NODE) \ (((struct tree_lambda_expr *)LAMBDA_EXPR_CHECK (NODE))->extra_scope) diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index c1375ecdbb5..7f22384d8a7 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -27107,6 +27107,8 @@ cp_parser_class_specifier (cp_parser* parser) if (!braces.require_open (parser)) { pop_deferring_access_checks (); + if (type != error_mark_node) + finish_lambda_scope (); return error_mark_node; } @@ -27171,7 +27173,10 @@ cp_parser_class_specifier (cp_parser* parser) if (cp_parser_allow_gnu_extensions_p (parser)) attributes = cp_parser_gnu_attributes_opt (parser); if (type != error_mark_node) - type = finish_struct (type, attributes); + { + type = finish_struct (type, attributes); + finish_lambda_scope (); + } if (nested_name_specifier_p) pop_inner_scope (old_scope, scope); @@ -28011,6 +28016,12 @@ cp_parser_class_head (cp_parser* parser, if (flag_concepts) type = associate_classtype_constraints (type); + /* Lambdas in bases and members must have the same mangling scope for ABI. + We open this scope now, and will close it in cp_parser_class_specifier + after parsing the member list. */ + if (type && type != error_mark_node) + start_lambda_scope (TYPE_NAME (type)); + /* We will have entered the scope containing the class; the names of base classes should be looked up in that context. For example: @@ -28025,16 +28036,10 @@ cp_parser_class_head (cp_parser* parser, if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) { if (type) - { - pushclass (type); - start_lambda_scope (TYPE_NAME (type)); - } + pushclass (type); bases = cp_parser_base_clause (parser); if (type) - { - finish_lambda_scope (); - popclass (); - } + popclass (); } else bases = NULL_TREE; @@ -28700,9 +28705,11 @@ cp_parser_member_declaration (cp_parser* parser) pure-specifier. It is not correct to parse the initializer before registering the member declaration since the member declaration should be in scope while - its initializer is processed. However, the rest of the - front end does not yet provide an interface that allows - us to handle this correctly. */ + its initializer is processed. And similarly, the ABI of + lambdas declared in the initializer should be scoped to + the member. However, the rest of the front end does not + yet provide an interface that allows us to handle this + correctly. */ if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)) { /* In [class.mem]: diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index f4213f88b99..06d13fda872 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -12508,6 +12508,10 @@ instantiate_class_template (tree type) gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern)) || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type))); + /* When instantiating nested lambdas, ensure that they get the mangling + scope of the new class type. */ + start_lambda_scope (TYPE_NAME (type)); + base_list = NULL_TREE; /* Defer access checking while we substitute into the types named in the base-clause. */ @@ -12867,6 +12871,8 @@ instantiate_class_template (tree type) finish_struct_1 (type); TYPE_BEING_DEFINED (type) = 0; + finish_lambda_scope (); + /* Remember if instantiating this class ran into errors, so we can avoid instantiating member functions in limit_bad_template_recursion. We set this flag even if the problem was in another instantiation triggered by @@ -22275,6 +22281,8 @@ instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain) ctx = tsubst_entering_scope (DECL_CONTEXT (gen_tmpl), targ_ptr, complain, gen_tmpl); push_nested_class (ctx); + if (!DECL_ALIAS_TEMPLATE_P (gen_tmpl)) + start_lambda_scope (TYPE_NAME (ctx)); } tree pattern = DECL_TEMPLATE_RESULT (gen_tmpl); @@ -22304,7 +22312,11 @@ instantiate_template (tree tmpl, tree orig_args, tsubst_flags_t complain) if (fndecl == NULL_TREE) fndecl = tsubst_decl (pattern, targ_ptr, complain, /*use_spec_table=*/false); if (DECL_CLASS_SCOPE_P (gen_tmpl)) - pop_nested_class (); + { + if (!DECL_ALIAS_TEMPLATE_P (gen_tmpl)) + finish_lambda_scope (); + pop_nested_class (); + } pop_from_top_level (); if (fndecl == error_mark_node) diff --git a/gcc/testsuite/g++.dg/abi/lambda-ctx2.C b/gcc/testsuite/g++.dg/abi/lambda-ctx2.C new file mode 100644 index 00000000000..26896105a6c --- /dev/null +++ b/gcc/testsuite/g++.dg/abi/lambda-ctx2.C @@ -0,0 +1,34 @@ +// PR c++/107741 +// { dg-do compile { target c++17 } } + +struct A { + // We currently parse static member initializers for non-templates before we + // see their decls, and so don't get the chance to attach it as scope. + static constexpr auto x = []{ return 1; }; +}; + +template +struct B { + static constexpr auto x = []{ return 2; }; +}; + +template +struct C { + static int x; +}; + +void side_effect(); + +template +int C::x = (side_effect(), []{ return 3; }()); + +template int C::x; + +void f() { + A::x(); + B::x(); +} + +// { dg-final { scan-assembler {_ZNK1A1xMUlvE_clEv:} { xfail *-*-* } } } +// { dg-final { scan-assembler {_ZNK1BIiE1xMUlvE_clEv:} } } +// { dg-final { scan-assembler {_ZNK1CIiE1xMUlvE_clEv:} } } diff --git a/gcc/testsuite/g++.dg/abi/lambda-ctx3.C b/gcc/testsuite/g++.dg/abi/lambda-ctx3.C new file mode 100644 index 00000000000..f92f2500531 --- /dev/null +++ b/gcc/testsuite/g++.dg/abi/lambda-ctx3.C @@ -0,0 +1,21 @@ +// { dg-do compile { target c++20 } } +// { dg-additional-options "-fkeep-inline-functions" } +// See also https://github.com/itanium-cxx-abi/cxx-abi/pull/85 + +struct A { + decltype([]{ return 1; }) f; +}; + +struct B : decltype([]{ return 2; }) { + decltype([]{ return 3; }) f; +}; + +struct C : decltype([]{ return 4; }) { + decltype([]{ return 5; }) f; +}; + +// { dg-final { scan-assembler {_ZNK1AUlvE_clEv:} } } +// { dg-final { scan-assembler {_ZNK1BUlvE_clEv:} } } +// { dg-final { scan-assembler {_ZNK1BUlvE0_clEv:} } } +// { dg-final { scan-assembler {_ZNK1CUlvE_clEv:} } } +// { dg-final { scan-assembler {_ZNK1CUlvE0_clEv:} } } diff --git a/gcc/testsuite/g++.dg/abi/lambda-ctx4.C b/gcc/testsuite/g++.dg/abi/lambda-ctx4.C new file mode 100644 index 00000000000..d6544a84652 --- /dev/null +++ b/gcc/testsuite/g++.dg/abi/lambda-ctx4.C @@ -0,0 +1,22 @@ +// { dg-do compile { target c++20 } } +// { dg-additional-options "-fkeep-inline-functions" } + +struct S { + template + using T = decltype([]{ return I; }); +}; + +S::T<0> a; +S::T<1> b; + +int main() { + a(); + b(); +} + +// Currently we don't implement any special mangling rules for template aliases +// (though we probably should; an alias template is a definable item by +// [basic.def.odr] p1.5 and as such contained lambdas in different TUs should have +// the same type, see [basic.def.odr] p15.6) +// { scan_assembler {_ZNK1SUlvE_clEv:} } +// { scan_assembler {_ZNK1SUlvE0_clEv:} } From patchwork Sat Nov 9 08:23:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 2008872 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=Or2N80E2; 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 4XlpkS3ddwz1xyr for ; Sat, 9 Nov 2024 19:24:16 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B17233858C42 for ; Sat, 9 Nov 2024 08:24:14 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x102a.google.com (mail-pj1-x102a.google.com [IPv6:2607:f8b0:4864:20::102a]) by sourceware.org (Postfix) with ESMTPS id 083093858D20 for ; Sat, 9 Nov 2024 08:23:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 083093858D20 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 083093858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::102a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731140636; cv=none; b=Rx6mHTORCVeZNzaMgHcC2F/H+lrUVGL37DI/iu06MwXK1Knxzke3E4u5aNtYkXCctTT37/J+nzEwgncWeXe4DyhG2xCp0jw566JlSrdOykhbB67GSL8weHP61GdXRysRSK/J1rxZipUa7meAm/noCtdArgo26N6dQsBqClX7Nqo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731140636; c=relaxed/simple; bh=GRXTxcDY1VprmimS6FaAx9T6LIp+D15Ok4K1PUPLZRc=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=HMqryfdqL43nBOOyXvcfnx9XLDASFHvxs3CLHB1rTRPtnqvPXjBY171sTxf8/b3PEoaw3lBNg0Jjl3zXmAGf8QxPvvO6GVV8z5ZRC1f7MCgL0L4lstbmBQbxSNOsf2nFifIhgZUGGBIL/vxDNqcqd0aiLmZQaXD2c04gaea8wd0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x102a.google.com with SMTP id 98e67ed59e1d1-2e56750bb13so392281a91.2 for ; Sat, 09 Nov 2024 00:23:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731140633; x=1731745433; 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=p9cZvBByv6i4YPxo5NQcgXM9VzM6LKu94lt6UdDzMQw=; b=Or2N80E2uWGi+jojcvhC29lj30KKvlFHcZDxMqgybgWIQDi/uOttGb1j3+lk6ZVuij LuyapWf5GQVVcVZWVqmQYDXJkmm0w7WAhguAfzWAfCLiOwgO//MfUcPugy54OjJ50Fpy 19F8e+GrEBhtaKPLHnT2iOvy4aXcMMzsupfcqQO5NmxJjHv5mGsO/OsT0mKRyvoQg66+ OS4HEDo04j0pmUs2KDx9lIfSnxPLRCsv42Y3BaNQ/9jthSy3iOfz76cRyYOtH4aMbnPq HTMl1PTdITPfuOQfJR95B2zjHwcueQ2CI51dR+1KPkLIcEjFYsqKgjp2bD+y0jBxZyN5 HW5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731140633; x=1731745433; 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=p9cZvBByv6i4YPxo5NQcgXM9VzM6LKu94lt6UdDzMQw=; b=l3UsahP2gWLeUbWE+iDJELAmAewJZ9uWaAnx36K/sFWr44VzGZcKpLeUlXxJA2Ac9d mTXhfEUS5gZeJbSOuePuPWM+z7mY9z+BuRa/xqswLHzVv81EnZ3NvZLnHaieuW/n6bqR aJcmrTvgKBAG+vAxOf9HzjMcopucteEJZ9sSpGxacMJnuAou6tx3/oXwKFOvyTwxbK8d aE/fBrl+E6bzMzFiLTje3/WU0lvdOEFpH8A+hH728L4UVgT+XXlD+3UGzVSYetthKu2z Zv69geKv/GUoEhmy5BIddI1JZh/9Lwxd+/9zBNN10KhPHLsaDml/1egWdq/zfUn8Su4a XBgA== X-Gm-Message-State: AOJu0YwYnP8nM/zgF9pBMprSCs1aoJo6ZFPTT4vWa2KKCW1hdmVqnYyc Blj+WLKBqLR9lWYInfLLSOljjWjfa16I6TlZModXfWtUixWHrYLAWOo+CA== X-Google-Smtp-Source: AGHT+IEjVfaGSRlBGn5uxxRUbIrAdRY5X3FntWAlM9PBJzFFmsUQ5LzJpNvadDaOvRkjb0oQM1OGlQ== X-Received: by 2002:a17:902:ecd0:b0:20e:5777:b6f9 with SMTP id d9443c01a7336-21183573665mr33090365ad.10.1731140632754; Sat, 09 Nov 2024 00:23:52 -0800 (PST) Received: from Thaum. ([163.47.68.2]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21177e4a154sm41129325ad.126.2024.11.09.00.23.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Nov 2024 00:23:52 -0800 (PST) Message-ID: <672f1c18.170a0220.e2dd2.5300@mx.google.com> X-Google-Original-Message-ID: Date: Sat, 9 Nov 2024 19:23:48 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill Subject: [PATCH v2 2/3] c++: Update mangling of lambdas in expressions References: <672f1bca.050a0220.1f2cda.8a47@mx.google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <672f1bca.050a0220.1f2cda.8a47@mx.google.com> 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 -- https://github.com/itanium-cxx-abi/cxx-abi/pull/85 clarifies that mangling a lambda expression should use 'L' rather than "tl". gcc/cp/ChangeLog: * mangle.cc (write_expression): Update mangling for lambdas. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/lambda-generic-mangle1.C: Update mangling. * g++.dg/cpp2a/lambda-generic-mangle1a.C: Likewise. Signed-off-by: Nathaniel Shead --- gcc/cp/mangle.cc | 2 +- gcc/testsuite/g++.dg/cpp2a/lambda-generic-mangle1.C | 2 +- gcc/testsuite/g++.dg/cpp2a/lambda-generic-mangle1a.C | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gcc/cp/mangle.cc b/gcc/cp/mangle.cc index 4d46c0917a9..42fcdc34353 100644 --- a/gcc/cp/mangle.cc +++ b/gcc/cp/mangle.cc @@ -3761,7 +3761,7 @@ write_expression (tree expr) equivalent. So just use the closure type mangling. */ - write_string ("tl"); + write_char ('L'); write_type (LAMBDA_EXPR_CLOSURE (expr)); write_char ('E'); } diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-generic-mangle1.C b/gcc/testsuite/g++.dg/cpp2a/lambda-generic-mangle1.C index 0051307f53d..306959a4f9f 100644 --- a/gcc/testsuite/g++.dg/cpp2a/lambda-generic-mangle1.C +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-generic-mangle1.C @@ -6,4 +6,4 @@ struct C { void f(decltype([](T, auto) { return 0; })) {} }; void g() { C().f({}); } -// { dg-final { scan-assembler "_ZN1C1fIiEEvDTtlNS_UlT_TL0__E_EEE" } } +// { dg-final { scan-assembler "_ZN1C1fIiEEvDTLNS_UlT_TL0__E_EEE" } } diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-generic-mangle1a.C b/gcc/testsuite/g++.dg/cpp2a/lambda-generic-mangle1a.C index dc7b0125631..b7bd4ecdd46 100644 --- a/gcc/testsuite/g++.dg/cpp2a/lambda-generic-mangle1a.C +++ b/gcc/testsuite/g++.dg/cpp2a/lambda-generic-mangle1a.C @@ -7,4 +7,4 @@ struct C { void f(decltype([](T, auto) { return 0; })) {} }; void g() { C().f({}); } -// { dg-final { scan-assembler "_ZN1C1fIiEEvDTtlNS_UlT_T_E_EEE" } } +// { dg-final { scan-assembler "_ZN1C1fIiEEvDTLNS_UlT_T_E_EEE" } } From patchwork Sat Nov 9 08:28:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 2008873 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=hRlXRJJf; 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 4XlprH6mwSz1xyk for ; Sat, 9 Nov 2024 19:29:19 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C29EB3858C50 for ; Sat, 9 Nov 2024 08:29:17 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x1031.google.com (mail-pj1-x1031.google.com [IPv6:2607:f8b0:4864:20::1031]) by sourceware.org (Postfix) with ESMTPS id DCEC73858D20 for ; Sat, 9 Nov 2024 08:28:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DCEC73858D20 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 DCEC73858D20 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::1031 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731140940; cv=none; b=p1xKBRLDZ8AA3yAUi13SlTREqs/yOLkl+sfbkX6lGh8jQ5oXTmJDxqjqIL3HnsvvC1fISI5MMyarVwzKvybW2pFFRTfJxgCzHv29yUe25/+hCs0ofwMW35M7Tk3HGkH1DqmBkWiyS4xINsjtb+Z8vn/YtUaQBTuN8cnEkPUVrps= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1731140940; c=relaxed/simple; bh=XHREGB0uFTLkxc1gk1irB96MuA71aS6ATTwKOY6dgAE=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=uwekPVF9/xPAIGjq5mBm6f0ROo62hDVcKuoEgToqeeh7w8fnsMYCh/lL0i5K1AaVbCRx9h0X+y7CXZ/JWx3z6WPkwLqSIpZHPloWMADwIYIZGKTz6TefYFgXI/F0oY+ToUunby1sSvQgnIPK7drlROw+fhX8d1lklKFpfwlhxIs= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pj1-x1031.google.com with SMTP id 98e67ed59e1d1-2e2c2a17aa4so378008a91.0 for ; Sat, 09 Nov 2024 00:28:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1731140937; x=1731745737; 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=E7cW5HEbVTm6rLbGJEfV2gaovabjP+qUDxRbJri11VQ=; b=hRlXRJJfyJdGuR96NKxifydaErDU8NOVAHuOkVt+5RVMm6MsN+FZhrA2kb3B971KJH Xxh6YGHB7rMGNPOn0lDHhXUR3Fw6H58unguh+5bXQrV3kOUlxNnKDkGRknSnLp2hUrWA re/dhzAJGuorW9r1ifwSiDeyjodz7E3zAQfyK0Rz7vr5XdOZ0sp2fBYmVyfxgi6N4JbZ 5WPXhwcx7ye9VAMl3g007NZGEJj2CCEZcgNEkhge2GRtu9PbJl1iwoUemsH7YNHtHrSh 38WzeWoZnab5sNMqlslMDY8XiQfexy1kWb5rpEVR1hMZ7jMGFh4YfSAk6o3c5ac0Ka05 F/nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1731140937; x=1731745737; 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=E7cW5HEbVTm6rLbGJEfV2gaovabjP+qUDxRbJri11VQ=; b=ZeKK3x/wnuqY3j1zSnr/W8wccxE0tlniePgAMlHnUIN9pj1pSo0Cn6LlkyQl7u/GRy Vpw9bNzprqIU7BHz8sHvkm/d5VYEYwXfwdFgZ5g5pewoQQ89CwOp8LCyW1HL4zAvV2yj F9K+cJBEX0VDMywFWzSNLIV7gogK4Imx8UljW8NGZF0/+MXfJJ7qkfn0L3vI02EDTabD zP3zmIKPk6m1r6yZJirr+OvRHrrL7L00Mn40bBr3LnYi9wxt11GHRks165p7sdLDv+tE ITWAj3MqfvDfulksrZ5aTNAeiiVde4dg1QpfQ3BgZceCrGtOHwIJkU51hWPgqjbjfGdF yhEw== X-Gm-Message-State: AOJu0YwAlIaTIG5H68hCUJXKmJOcyZSM/Mxyxw8b6Aq8bEGUkPg93F1Z dvxrIp+xeEvbTzL0Un6jkK2+VOrVjlemGthPwfCBNI9cj5s2oKuIiXfENg== X-Google-Smtp-Source: AGHT+IEtqk8S5jeJ8CvqQ751+joo7CKvdQhYjwEKo2EKjZ41bixaZVaT/VYbhaSFRbZwLjt716rlwQ== X-Received: by 2002:a17:90b:1e0f:b0:2e2:abab:c458 with SMTP id 98e67ed59e1d1-2e9b16aabbbmr3411005a91.8.1731140936775; Sat, 09 Nov 2024 00:28:56 -0800 (PST) Received: from Thaum. ([163.47.68.2]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2e99a541c95sm7613007a91.17.2024.11.09.00.28.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 09 Nov 2024 00:28:56 -0800 (PST) Message-ID: <672f1d48.170a0220.4887.9d62@mx.google.com> X-Google-Original-Message-ID: Date: Sat, 9 Nov 2024 19:28:52 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill Subject: [PATCH v2 3/3] c++/modules: Prevent ICE when writing class-scope lambdas without mangling scope [PR116568] References: <672f1bca.050a0220.1f2cda.8a47@mx.google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <672f1bca.050a0220.1f2cda.8a47@mx.google.com> 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? Alternatively, after this I'll work on an update of my P1815 (TU-local entities) patch series [1] which would also solve this ICE by erroring early due to attempting to emit a TU-local entity. As discussed in patch #1 I believe this is incorrect but also seems out of scope of this particular modification. I still think this patch would be useful however, if only for the clarification of the behaviour of streaming lambdas without an extra mangling scope. [1]: https://gcc.gnu.org/pipermail/gcc-patches/2024-October/665108.html -- >8 -- If a lambda doesn't have an extra mangling scope despite being in class scope (such as associated with a type alias, currently), we shouldn't ICE; this behaviour is fine as long as we don't need to merge. In the future such lambdas should be rejected as being TU-local, but for now this at least clarifies the intended behaviour if this edge case occurs again. PR c++/116568 gcc/cp/ChangeLog: * module.cc (trees_out::get_merge_kind): All lambdas without extra mangling scope now marked MK_unique. gcc/testsuite/ChangeLog: * g++.dg/modules/lambda-8.h: New test. * g++.dg/modules/lambda-8_a.H: New test. * g++.dg/modules/lambda-8_b.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/module.cc | 30 ++++++++++++++--------- gcc/testsuite/g++.dg/modules/lambda-8.h | 7 ++++++ gcc/testsuite/g++.dg/modules/lambda-8_a.H | 5 ++++ gcc/testsuite/g++.dg/modules/lambda-8_b.C | 5 ++++ 4 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/lambda-8.h create mode 100644 gcc/testsuite/g++.dg/modules/lambda-8_a.H create mode 100644 gcc/testsuite/g++.dg/modules/lambda-8_b.C diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 4eefb2d3584..877e442bc41 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -10737,18 +10737,24 @@ trees_out::get_merge_kind (tree decl, depset *dep) g++.dg/modules/lambda-6_a.C. */ if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)) && LAMBDA_TYPE_P (TREE_TYPE (decl))) - if (tree scope = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl))) - { - /* Lambdas attached to fields are keyed to its class. */ - if (TREE_CODE (scope) == FIELD_DECL) - scope = TYPE_NAME (DECL_CONTEXT (scope)); - if (DECL_LANG_SPECIFIC (scope) - && DECL_MODULE_KEYED_DECLS_P (scope)) - { - mk = MK_keyed; - break; - } - } + { + if (tree scope = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl))) + { + /* Lambdas attached to fields are keyed to its class. */ + if (TREE_CODE (scope) == FIELD_DECL) + scope = TYPE_NAME (DECL_CONTEXT (scope)); + if (DECL_LANG_SPECIFIC (scope) + && DECL_MODULE_KEYED_DECLS_P (scope)) + { + mk = MK_keyed; + break; + } + } + /* Lambdas without extra mangling scope are unique to the TU, + and cannot be merged; make them unique. */ + mk = MK_unique; + break; + } if (TREE_CODE (decl) == TEMPLATE_DECL && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)) diff --git a/gcc/testsuite/g++.dg/modules/lambda-8.h b/gcc/testsuite/g++.dg/modules/lambda-8.h new file mode 100644 index 00000000000..0c66f053b20 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/lambda-8.h @@ -0,0 +1,7 @@ +template struct S { + template static constexpr auto x = []{}; + template using t = decltype([]{}); +}; + +inline auto x = S::x; +using t = S::t; diff --git a/gcc/testsuite/g++.dg/modules/lambda-8_a.H b/gcc/testsuite/g++.dg/modules/lambda-8_a.H new file mode 100644 index 00000000000..d20958ee140 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/lambda-8_a.H @@ -0,0 +1,5 @@ +// PR c++/116568 +// { dg-additional-options "-fmodules-ts -std=c++20" } +// { dg-module-cmi {} } + +#include "lambda-8.h" diff --git a/gcc/testsuite/g++.dg/modules/lambda-8_b.C b/gcc/testsuite/g++.dg/modules/lambda-8_b.C new file mode 100644 index 00000000000..05ea4afd8c1 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/lambda-8_b.C @@ -0,0 +1,5 @@ +// PR c++/116568 +// { dg-additional-options "-fmodules-ts -std=c++20" } + +#include "lambda-8.h" +import "lambda-8_a.H";