From patchwork Sat Feb 10 22:57:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nathaniel Shead X-Patchwork-Id: 199322 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp1684889dyd; Sat, 10 Feb 2024 14:58:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IHj3L4BENxNJgJMbLrkW1VsV9JdDbk7JI8TebpCdPerq24fZMhdLdzrpo78a0hs5YOF8LSz X-Received: by 2002:a05:622a:13c7:b0:42c:5d43:53cd with SMTP id p7-20020a05622a13c700b0042c5d4353cdmr4428708qtk.29.1707605893101; Sat, 10 Feb 2024 14:58:13 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707605893; cv=pass; d=google.com; s=arc-20160816; b=vktVX9GafYAJDoMdBIW6NZVOsKUmLWVpyNwtrC/It4HAq6oY2t/cnIR6SHXPGITXTB K3Qm/JZKtYubhy3NA8O41s9ZN+/b6Ay/nwRNkP9KOt14KSrUDQ0QMUn76PvEYUaGahdQ GgN+UX3SRBnhTeJqgrbb53iYBfwKkerji8ZZTqWt/uIAwVujpZzwe79/kRxfgsq/HWIE wXvYfforUqFoMIx9cGDxJOrv+54CEsN3RKtCWYEGxqjpOurymQVF/cW1Q46xdgtijamJ C/qrYpkacMIdWig+nGWs04B5VzCdYdanx8M7+IrXRbswHtFbtyqAh0qGxL68VJ/i4Tna g0TA== 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=IiLtKOZ3Vq4QqKW1LeWzkmp3nMYEidurOPBfvpUE+SA=; fh=+an1mzo2FeBUzhm7WFt30LRrIO5N3fQ3dP7lq4ZhLPs=; b=vZFc7VYamOpyQvA6qFnJqz0HjkjRpwmsxYdrNPSQblKH/eTccS8qSLH3Z5/xaSqLld EfaaY4QvQBWyBOzhK674RH9jiJmH8w+ICUp65pTccEFgZsCgycfpL/ODYC3BZ0RfnunU WNVaq5nN3apxwcl+lb8qSk/9yHMadjp87JRPNIABwH789N+sYrmO2g92We6N1X41MZ5h 3YFBOc0ytdN7ghF1fKFqVMBOgSMjnhE6pt0DFcSYbj8nWIqsqD/EYRR06kfMFzIF9Eix Hq3BUBVbd84qCBnarc2EgIhp5rGyai/46wAzmEAz/tiDNHzLNZWdRvm8VGeH0ikF1hAg Y/HA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=TnDON41m; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 X-Forwarded-Encrypted: i=2; AJvYcCVENe8T24kj+C7Jmx4HTGtFKfSbBuoUXYZMpguiiJtRqYZ6mJCCEdVgSFAEFCDJdpxBDETLEBQxnADufRat7srximEP/Q== Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id t25-20020a05622a181900b0042c189b36c8si5109878qtc.599.2024.02.10.14.58.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Feb 2024 14:58:13 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=TnDON41m; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 C9EF73858CDB for ; Sat, 10 Feb 2024 22:58:12 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-il1-x129.google.com (mail-il1-x129.google.com [IPv6:2607:f8b0:4864:20::129]) by sourceware.org (Postfix) with ESMTPS id C43933858D35 for ; Sat, 10 Feb 2024 22:57:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C43933858D35 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 C43933858D35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::129 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707605845; cv=none; b=vwHt7W5T49Eg0v97t2sakedT5GK+E04Nc/dMDm8tyJ1xvlS29aCovPH50cWnTPnmK28Uwyh44idbwwFbInyxDUYFG4vZjsobAGELPgYgsNCUONAl4ehiCLtSzcT8PVvfBB/azC1gvVo94wmFEvJLXV8jp1r1PgyCa74zQPscros= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707605845; c=relaxed/simple; bh=A2Y4fkF/x5oi+tJlQfaASv4/3AHxKw0NLstjDArRsjk=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=wQ4DkmK/w28GEUuvvBclhE0UMxNnsGujdkerkXHQq3XAmLGkU0C6Wa9mFdlZREqPzeQ1ggrk1Ltj6bcttcCc0AilMn8/H+24SvJdYz+t/H3a5ha5Iyz3/pUcIF07WmmZJtLDWJ2yTXLtjQSQ3dVcSQtDWbQJFfAU+a8p9YSIxM8= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-il1-x129.google.com with SMTP id e9e14a558f8ab-363bfbc7b25so5580005ab.3 for ; Sat, 10 Feb 2024 14:57:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707605842; x=1708210642; 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=IiLtKOZ3Vq4QqKW1LeWzkmp3nMYEidurOPBfvpUE+SA=; b=TnDON41mdEwWsobeVopxVcgpNeSEiSNxyLWcryEXBx+zgkq2ynX8BiuUqdl1CJdTnj Bmow1JZ4gpU5O7RepkmW7IbyUenrO/pTaGLNsiYVB6+EXt/CchSah2lzAyOmp+tTqDzQ qPKjxwRt2nWOeQZeRaidh6Pxlbv87CDHjYzYvJvTiUCGLWesAx8AbBvZhmiP+Ebwk8N6 tYngyg9BBJMtRb2dbD3Y7EKR/fZvCLHXw0dZ8c/Yyricm/AfAiZgSKmO0LiW6vofHzDF p3lVMPdWYORX8+hBoeHohamxFiQ5vGbAUShFv8UWYsRtGhxT8Wg35GjroZYLZADguXI7 WPNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707605842; x=1708210642; 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=IiLtKOZ3Vq4QqKW1LeWzkmp3nMYEidurOPBfvpUE+SA=; b=EJ7I+01U1HAsflHVze1U2r+AWAMo90sN6lWt/eY2UK/aMeXcDQyjGaCgjn/F9C+br4 UD69aSx5wHp84JLjZCj5lXQUgSgNCSbjefNtXQxjxJX88p/kGkiDzVc6V6W5Q1n8QXwe GU9Fhwf4B2RFvjNOtWmIsijX48YWC2CI9NgAwufqmzGVU77/ULgr59y8eHZBQpeMvhho WLCUlKzayMzdwxTKJ5p55SzMk7S7bMZggW+fITzpSSkUnh7PlhMaI2wtd/zvXKIdqNPk WqN3naEqw/GluC+hsjoxTEQ70T1/2uQ7iQZWAlKXVCwtNDVy7dZEQ/wxKiCQCK07dURz S0NA== X-Gm-Message-State: AOJu0Yy+VmtIUdjKCubmNvKz5Xk9mtwIk4bL0YxY/aPwndtKIxNYmujP PWl9zgPPd7poDg3l4pauKQkywRCzUd9IxReQQHBNx/UEgRjV671tm0H5HA2O X-Received: by 2002:a92:d7cf:0:b0:363:e1d0:3030 with SMTP id g15-20020a92d7cf000000b00363e1d03030mr3636954ilq.22.1707605841732; Sat, 10 Feb 2024 14:57:21 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCV0Cp0QfXGaBjmLdxfrWzEz5RHWnTQyygscbV0RixXNo/R7D2PYDcEiLxR0L0H9w4NkCb3BwDIES1sNq7VWTw== Received: from Thaum. (123-243-206-49.tpgi.com.au. [123.243.206.49]) by smtp.gmail.com with ESMTPSA id jc5-20020a17090325c500b001d9d4375149sm3458956plb.215.2024.02.10.14.57.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Feb 2024 14:57:21 -0800 (PST) Message-ID: <65c7ff51.170a0220.8dcc0.a404@mx.google.com> X-Google-Original-Message-ID: Date: Sun, 11 Feb 2024 09:57:16 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH] c++: Support lambdas attached to FIELD_DECLs in modules [PR111710] MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-11.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, URIBL_BLACK 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: 1790554556569416168 X-GMAIL-MSGID: 1790554556569416168 Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk? Or should I re-introduce the tree checking and just add checks for the new kinds of declarations that could be have keyed decls? -- >8 -- The fix for PR107398 weakened the restrictions that lambdas must belong to namespace scope. However this was not sufficient: we also need to allow lambdas keyed to FIELD_DECLs or PARM_DECLs. Because this now requires 'DECL_MODULE_KEYED_DECLS_P' to be checked on a fairly large number of different kinds of DECLs, and that in general it's safe to just get 'false' as a result of a check on an unexpected DECL type, this patch also removes the tree checking from the accessor. gcc/cp/ChangeLog: * cp-tree.h (DECL_MODULE_KEYED_DECLS_P): Remove tree checking. (struct lang_decl_base): Update comments and fix whitespace. * module.cc (trees_out::lang_decl_bools): Always write module_keyed_decls_p flag... (trees_in::lang_decl_bools): ...and always read it. (trees_out::decl_value): Also handle keyed decls on decls other than VAR_DECL or FUNCTION_DECL. (trees_in::decl_value): Likewise. (trees_out::get_merge_kind): Likewise. (maybe_key_decl): Also handle lambdas attached to FIELD_DECLs and PARM_DECLS. (trees_out::key_mergeable): Likewise. (trees_in::key_mergeable): Support keyed decls in a TYPE_DECL container. gcc/testsuite/ChangeLog: * g++.dg/modules/lambda-7_a.C: New test. * g++.dg/modules/lambda-7_b.C: New test. Signed-off-by: Nathaniel Shead --- gcc/cp/cp-tree.h | 23 ++++---- gcc/cp/module.cc | 70 +++++++++++------------ gcc/testsuite/g++.dg/modules/lambda-7_a.C | 20 +++++++ gcc/testsuite/g++.dg/modules/lambda-7_b.C | 16 ++++++ 4 files changed, 81 insertions(+), 48 deletions(-) create mode 100644 gcc/testsuite/g++.dg/modules/lambda-7_a.C create mode 100644 gcc/testsuite/g++.dg/modules/lambda-7_b.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 334c11396c2..6ab82dc2d0f 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -1773,9 +1773,8 @@ check_constraint_info (tree t) (DECL_LANG_SPECIFIC (DECL_MODULE_CHECK (NODE))->u.base.module_entity_p) /* DECL that has attached decls for ODR-relatedness. */ -#define DECL_MODULE_KEYED_DECLS_P(NODE) \ - (DECL_LANG_SPECIFIC (TREE_CHECK2(NODE,FUNCTION_DECL,VAR_DECL))\ - ->u.base.module_keyed_decls_p) +#define DECL_MODULE_KEYED_DECLS_P(NODE) \ + (DECL_LANG_SPECIFIC (DECL_MODULE_CHECK (NODE))->u.base.module_keyed_decls_p) /* Whether this is an exported DECL. Held on any decl that can appear at namespace scope (function, var, type, template, const or @@ -2887,21 +2886,19 @@ struct GTY(()) lang_decl_base { unsigned friend_or_tls : 1; /* var, fn, type or template */ unsigned unknown_bound_p : 1; /* var */ unsigned odr_used : 1; /* var or fn */ - unsigned concept_p : 1; /* applies to vars and functions */ + unsigned concept_p : 1; /* applies to vars and functions */ unsigned var_declared_inline_p : 1; /* var */ unsigned dependent_init_p : 1; /* var */ /* The following apply to VAR, FUNCTION, TYPE, CONCEPT, & NAMESPACE decls. */ - unsigned module_purview_p : 1; // in named-module purview - unsigned module_attach_p : 1; // attached to named module - unsigned module_import_p : 1; /* from an import */ - unsigned module_entity_p : 1; /* is in the entitity ary & - hash. */ - /* VAR_DECL or FUNCTION_DECL has keyed decls. */ - unsigned module_keyed_decls_p : 1; - - /* 12 spare bits. */ + unsigned module_purview_p : 1; /* in named-module purview */ + unsigned module_attach_p : 1; /* attached to named module */ + unsigned module_import_p : 1; /* from an import */ + unsigned module_entity_p : 1; /* is in the entitity ary & hash */ + unsigned module_keyed_decls_p : 1; /* has keys, applies to all decls */ + + /* 11 spare bits. */ }; /* True for DECL codes which have template info and access. */ diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 560d8f3b614..9742bca922c 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -5664,8 +5664,7 @@ trees_out::lang_decl_bools (tree t) want to mark them as in module purview. */ WB (lang->u.base.module_purview_p && !header_module_p ()); WB (lang->u.base.module_attach_p); - if (VAR_OR_FUNCTION_DECL_P (t)) - WB (lang->u.base.module_keyed_decls_p); + WB (lang->u.base.module_keyed_decls_p); switch (lang->u.base.selector) { default: @@ -5738,8 +5737,7 @@ trees_in::lang_decl_bools (tree t) RB (lang->u.base.dependent_init_p); RB (lang->u.base.module_purview_p); RB (lang->u.base.module_attach_p); - if (VAR_OR_FUNCTION_DECL_P (t)) - RB (lang->u.base.module_keyed_decls_p); + RB (lang->u.base.module_keyed_decls_p); switch (lang->u.base.selector) { default: @@ -7869,8 +7867,7 @@ trees_out::decl_value (tree decl, depset *dep) install_entity (decl, dep); } - if (VAR_OR_FUNCTION_DECL_P (inner) - && DECL_LANG_SPECIFIC (inner) + if (DECL_LANG_SPECIFIC (inner) && DECL_MODULE_KEYED_DECLS_P (inner) && !is_key_order ()) { @@ -8170,8 +8167,7 @@ trees_in::decl_value () bool installed = install_entity (existing); bool is_new = existing == decl; - if (VAR_OR_FUNCTION_DECL_P (inner) - && DECL_LANG_SPECIFIC (inner) + if (DECL_LANG_SPECIFIC (inner) && DECL_MODULE_KEYED_DECLS_P (inner)) { /* Read and maybe install the attached entities. */ @@ -10482,8 +10478,7 @@ trees_out::get_merge_kind (tree decl, depset *dep) if (tree scope = LAMBDA_EXPR_EXTRA_SCOPE (CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (decl)))) - if (TREE_CODE (scope) == VAR_DECL - && DECL_MODULE_KEYED_DECLS_P (scope)) + if (DECL_MODULE_KEYED_DECLS_P (scope)) { mk = MK_keyed; break; @@ -10787,7 +10782,9 @@ trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner, gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (inner))); tree scope = LAMBDA_EXPR_EXTRA_SCOPE (CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (inner))); - gcc_checking_assert (TREE_CODE (scope) == VAR_DECL); + gcc_checking_assert (TREE_CODE (scope) == VAR_DECL + || TREE_CODE (scope) == FIELD_DECL + || TREE_CODE (scope) == PARM_DECL); auto *root = keyed_table->get (scope); unsigned ix = root->length (); /* If we don't find it, we'll write a really big number @@ -11065,6 +11062,26 @@ trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner, } } } + else if (mk == MK_keyed + && DECL_LANG_SPECIFIC (name) + && DECL_MODULE_KEYED_DECLS_P (name)) + { + gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL + || TREE_CODE (container) == TYPE_DECL); + if (auto *set = keyed_table->get (name)) + if (key.index < set->length ()) + { + existing = (*set)[key.index]; + if (existing) + { + gcc_checking_assert + (DECL_IMPLICIT_TYPEDEF_P (existing)); + if (inner != decl) + existing + = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing)); + } + } + } else switch (TREE_CODE (container)) { @@ -11072,27 +11089,8 @@ trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner, gcc_unreachable (); case NAMESPACE_DECL: - if (mk == MK_keyed) - { - if (DECL_LANG_SPECIFIC (name) - && VAR_OR_FUNCTION_DECL_P (name) - && DECL_MODULE_KEYED_DECLS_P (name)) - if (auto *set = keyed_table->get (name)) - if (key.index < set->length ()) - { - existing = (*set)[key.index]; - if (existing) - { - gcc_checking_assert - (DECL_IMPLICIT_TYPEDEF_P (existing)); - if (inner != decl) - existing - = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing)); - } - } - } - else if (is_attached - && !(state->is_module () || state->is_partition ())) + if (is_attached + && !(state->is_module () || state->is_partition ())) kind = "unique"; else { @@ -18981,9 +18979,11 @@ maybe_key_decl (tree ctx, tree decl) if (!modules_p ()) return; - // FIXME: For now just deal with lambdas attached to var decls. - // This might be sufficient? - if (TREE_CODE (ctx) != VAR_DECL) + /* We only need to deal with lambdas attached to var, field, + or parm decls. */ + if (TREE_CODE (ctx) != VAR_DECL + && TREE_CODE (ctx) != FIELD_DECL + && TREE_CODE (ctx) != PARM_DECL) return; if (!keyed_table) diff --git a/gcc/testsuite/g++.dg/modules/lambda-7_a.C b/gcc/testsuite/g++.dg/modules/lambda-7_a.C new file mode 100644 index 00000000000..289285cd926 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/lambda-7_a.C @@ -0,0 +1,20 @@ +// { dg-additional-options "-fmodules-ts" } +// { dg-module-cmi foo } + +export module foo; + +export struct S { + int (*a)(int) = [](int x) { return x * 2; }; + + int b(int x, int (*f)(int) = [](int x) { return x * 3; }) { + return f(x); + } + + static int c(int x, int (*f)(int) = [](int x) { return x * 4; }) { + return f(x); + } +}; + +export inline int d(int x, int (*f)(int) = [](int x) { return x * 5; }) { + return f(x); +} diff --git a/gcc/testsuite/g++.dg/modules/lambda-7_b.C b/gcc/testsuite/g++.dg/modules/lambda-7_b.C new file mode 100644 index 00000000000..a8762399ee1 --- /dev/null +++ b/gcc/testsuite/g++.dg/modules/lambda-7_b.C @@ -0,0 +1,16 @@ +// { dg-module-do run } +// { dg-additional-options "-fmodules-ts" } + +import foo; + +int main() { + S s; + if (s.a(10) != 20) + __builtin_abort(); + if (s.b(10) != 30) + __builtin_abort(); + if (s.c(10) != 40) + __builtin_abort(); + if (d(10) != 50) + __builtin_abort(); +} From patchwork Sun Feb 11 03:54: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: 199367 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:50ea:b0:106:860b:bbdd with SMTP id r10csp1763319dyd; Sat, 10 Feb 2024 19:55:02 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVhLM6Hne6G0rqwgA6MPLOK6zaf6dBmztRlnO/snwQAUZWRMUUVeViOyCWfS9lRUM+EmX3ZQR+bkUakyI0J3QCEZehrkQ== X-Google-Smtp-Source: AGHT+IGAyfFY8taEqRzv7TWNDf8w8SVKFi0kJKtHEgX8kUHuqfDrVXuDj/vJ555v0SekKAkydrPv X-Received: by 2002:a05:622a:1909:b0:42c:2645:53aa with SMTP id w9-20020a05622a190900b0042c264553aamr4308516qtc.38.1707623702639; Sat, 10 Feb 2024 19:55:02 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707623702; cv=pass; d=google.com; s=arc-20160816; b=ML7MMo7bD3zAXnJBTRz2v644N3sw0t6cfdl4A8UvdGV0uGXdmzC3Gw+1jODxrrZrdj SK58EZmWdusWL9mTsPk8uqqetEkl+2GidHUOkMHevCJax8/q1rBiRy6eAxMU3S1AH1PC 3Gj0zKC1ICwJkoD2172wpK5Eh+irphltdXZR/Q3/AmiGZcx9ovQTor9myIHegvgU166L 66PQiO96onn6Hf2YypzBdLczylu85fiiJVo3KOyiFYGRxZgzMUHlIxHopmHDxuDiugPn Vi/7t7sjkrYYpuvYsNwe1A69Ty81nTnf+XdIewYo7GNHkezftM4L6paoTrhtM1S9x5Gf DZ/Q== 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:in-reply-to:content-disposition :mime-version:references:subject:cc:to:from:date:message-id :dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=TW+fuQ/Wd/k+ovKSTYwdORmZ/bK0ashnvrbvVg2hy9Y=; fh=xX5FCvAO5JSSgoGc+qWtxQMeY9e5KJSPgPovlW4IR/0=; b=ucfbEYOhsHgxs0X5UgxUCQ8LhSNDYrRxUWk0R8lEx6Bxa1BVFmieEhmudrLvBEYRLU YozxVGVkS0v33j/TL60L6w99AMghumeJkkDQ9+MkfswHeNHTwCSrXx6WKprEn8bUl2Ha SDpjOobp+UKTDID5oEGwFDmWHc+/rfHlKYQNe1nvPQXESuAp+6D6NVJSzN0tehpscQKP Xq1VSfWcJ7rT0o4EwxaAwZiNvotUDW/tCmiIEdXRYmQTqFEOyNoVZpaHkxcyHe7Y1mGQ arme7AI9tQ+dID6A/0W/8CdkrGpr70Kf6z8B4JjKRNXj27zHc5o4iu8tJxipSXV7LJVO GZ9g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=RxGQBhMr; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 X-Forwarded-Encrypted: i=2; AJvYcCXry4oTOy9VHOSmFnUkcc7f7eCKdmooqyPldv2Pi4JWm+LJcaJ+FSgAZOfMD2YD20AiqkQph+mBcxI09voABEGsAyYE1w== Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id ay35-20020a05622a22a300b0042c63b2267fsi3334477qtb.49.2024.02.10.19.55.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Feb 2024 19:55:02 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=RxGQBhMr; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 57EF33858404 for ; Sun, 11 Feb 2024 03:55:02 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by sourceware.org (Postfix) with ESMTPS id 5B14A3858CDB for ; Sun, 11 Feb 2024 03:54:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5B14A3858CDB 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 5B14A3858CDB Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::633 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707623667; cv=none; b=eB2DsWMSb18AMw8crLdtxuXTuW4flhl3ZRkNV2CxTvLabCRsZlovytxum7/8TKT/Fg8J3HE0IjmQPVJ6NHQVHYpn9BG9ulQ5gyBC1/Vo+Il7TWQ1jbHf52HV/JcNXRclLk6ni0D5Gh0sEiGYH5HV/RUKSSrvGbZSIfysNkawYt8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707623667; c=relaxed/simple; bh=A9+9OOxm8wRSy+EwodRbGtoPoWp2yqReCfh5fF4QG+I=; h=DKIM-Signature:Message-ID:Date:From:To:Subject:MIME-Version; b=ZnwArcD6XeHTPK+1YePx01R5BgwJGwc8AsAvUpOMhXqRhB9nbdVucy51CF7tQT6/7p51g1Yl5LPYt27CjdB0QhB8eYZEpWG1LhHBF29+KrcDXoLQEg/8lH5c242dZivCR0lVY1CPyggPi6hgVFihkK46k/aDfPs+g37G3a1HVUM= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1d76671e5a4so18587025ad.0 for ; Sat, 10 Feb 2024 19:54:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707623663; x=1708228463; 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=TW+fuQ/Wd/k+ovKSTYwdORmZ/bK0ashnvrbvVg2hy9Y=; b=RxGQBhMrMTzxrcyejXJmRyCm68tQT7SaXKzojbDTHPZXKqCM+4SyRt+vjdSvYQqReg dQY9YYYS9fyj8IhahuznyNmfdR7FCe+84/3kcFaBhWRpenVNJqAxfbM/QbD4dt7wGETO TBUJFKQz+c+aUyP1AUs7mHi3qB0mYNT1ceVP3Tq+NGU8XpgN2ZH1Qw+mUxNrzMre22uJ nQPJ8ikfrwlOCZhBRhSefP8BDT6kUyHXg88hQNKASpwtrMA3xKgbPj8VWo4SbYMNfMwx p96trOBFRkF+op4XgPt+opRG8NYPKWcc+B5SN+YxIRiQZ0V7OgRI1sRaSAlUtgY6pnWv 36xQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707623663; x=1708228463; 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=TW+fuQ/Wd/k+ovKSTYwdORmZ/bK0ashnvrbvVg2hy9Y=; b=gPqT8S/0S+eRJgg/wpYbIrwookV6Qvd5Ew8qC4Ok14fhvz4V5Pr+o6iWGWOSSoURyA d1xf/WZArunVWbw2rCjH4MWrxgCIrm+cCz2GmUjNynrp6Zi/B8Ijd/u5zvmDlGtvSSzq nacWwMowpk5Jc/6fmPel6O0QDaCl1sDFmD970KCyAyMaEPl58Rk1Oq5Usq+53OZQlGEm aji/CJoA3AewKJXIXar2TPk53k2iUQonRoMyNVRGfH9vNUIhSf5YhZY5xGc+SpMhU6rR LwBDgt1JaltSDEvxsffPI2aHzoe7qBvHoeGheyeN3BDLVnqlyRiKkuh8zhscDmQL9u3T mzFQ== X-Gm-Message-State: AOJu0YwHCeqbd8iRxr4gyVnUXkVUFyawCAtC9E+dj81J17zyljIi6ekf wrT+9guF72CPOawPJaXm7HEINH7uDeT7N9rIc1TLBvxMFo/K2W9sPEiKS+4B X-Received: by 2002:a17:902:a387:b0:1d8:ef82:ea99 with SMTP id x7-20020a170902a38700b001d8ef82ea99mr3947917pla.61.1707623663064; Sat, 10 Feb 2024 19:54:23 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCVnbJwio30VBMRJT1uLCduBEhH4ozHaH0fugxPfbvkpZfYS38bD5BWw4VQ/6Fs3bTYsGRc0YPRhsIC/ZJxElQ== Received: from Thaum. (123-243-206-49.tpgi.com.au. [123.243.206.49]) by smtp.gmail.com with ESMTPSA id q13-20020a170902c74d00b001d944b3c5f1sm3695776plq.178.2024.02.10.19.54.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 10 Feb 2024 19:54:22 -0800 (PST) Message-ID: <65c844ee.170a0220.88382.a8fa@mx.google.com> X-Google-Original-Message-ID: Date: Sun, 11 Feb 2024 14:54:18 +1100 From: Nathaniel Shead To: gcc-patches@gcc.gnu.org Cc: Jason Merrill , Nathan Sidwell Subject: [PATCH 2/1] c++: Also support lambdas attached to TYPE_DECLs in modules References: <65c7ff51.170a0220.8dcc0.a404@mx.google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <65c7ff51.170a0220.8dcc0.a404@mx.google.com> X-Spam-Status: No, score=-11.1 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, URIBL_BLACK 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: 1790573231674782788 X-GMAIL-MSGID: 1790573231674782788 Bootstrapped and regtested (so far just modules.exp and dg.exp) on x86_64-pc-linux-gnu, OK for trunk if full regtest succeeds? (Also I noticed I forgot to add the PR to the changelog in my last patch, I've fixed that locally.) -- >8 -- After fixing PR111710, I noticed that we currently ICE when declaring a type that derives from 'decltype([]{})'. As far as I can tell this should be legal code, since by [basic.link] p15.2 a lambda defined in a class-specifier should not be TU-local. This patch also adds a bunch of tests for unevaluated lambdas in other contexts, which generally seem to work now. One interesting case is 'E::f' in the attached testcase: it appears to get a merge kind of 'MK_field', rather than 'MK_keyed' as most other lambdas do. I'm not entirely sure if this will cause issues in the future, but I haven't been able to construct a testcase that causes problems with this, and conversely wrapping the class body in 'start_lambda_scope' causes issues with symbol duplication in COMDAT groups, so I've left it as-is for now. gcc/cp/ChangeLog: * module.cc (trees_out::key_mergeable): Also support TYPE_DECLs. (maybe_key_decl): Likewise. * parser.cc (cp_parser_class_head): Start a lambda scope when parsing base classes. gcc/testsuite/ChangeLog: * g++.dg/modules/lambda-7_a.C: * g++.dg/modules/lambda-7_b.C: Signed-off-by: Nathaniel Shead --- gcc/cp/module.cc | 8 +++++--- gcc/cp/parser.cc | 10 ++++++++-- gcc/testsuite/g++.dg/modules/lambda-7_a.C | 19 +++++++++++++++++++ gcc/testsuite/g++.dg/modules/lambda-7_b.C | 23 +++++++++++++++++++++++ 4 files changed, 55 insertions(+), 5 deletions(-) diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 9742bca922c..cceec79b26b 100644 --- a/gcc/cp/module.cc +++ b/gcc/cp/module.cc @@ -10784,7 +10784,8 @@ trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner, (TREE_TYPE (inner))); gcc_checking_assert (TREE_CODE (scope) == VAR_DECL || TREE_CODE (scope) == FIELD_DECL - || TREE_CODE (scope) == PARM_DECL); + || TREE_CODE (scope) == PARM_DECL + || TREE_CODE (scope) == TYPE_DECL); auto *root = keyed_table->get (scope); unsigned ix = root->length (); /* If we don't find it, we'll write a really big number @@ -18980,10 +18981,11 @@ maybe_key_decl (tree ctx, tree decl) return; /* We only need to deal with lambdas attached to var, field, - or parm decls. */ + parm, or type decls. */ if (TREE_CODE (ctx) != VAR_DECL && TREE_CODE (ctx) != FIELD_DECL - && TREE_CODE (ctx) != PARM_DECL) + && TREE_CODE (ctx) != PARM_DECL + && TREE_CODE (ctx) != TYPE_DECL) return; if (!keyed_table) diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc index 09ecfa23b5d..151e724ed66 100644 --- a/gcc/cp/parser.cc +++ b/gcc/cp/parser.cc @@ -27663,10 +27663,16 @@ cp_parser_class_head (cp_parser* parser, if (cp_lexer_next_token_is (parser->lexer, CPP_COLON)) { if (type) - pushclass (type); + { + pushclass (type); + start_lambda_scope (TYPE_NAME (type)); + } bases = cp_parser_base_clause (parser); if (type) - popclass (); + { + finish_lambda_scope (); + popclass (); + } } else bases = NULL_TREE; diff --git a/gcc/testsuite/g++.dg/modules/lambda-7_a.C b/gcc/testsuite/g++.dg/modules/lambda-7_a.C index 289285cd926..9a23827a280 100644 --- a/gcc/testsuite/g++.dg/modules/lambda-7_a.C +++ b/gcc/testsuite/g++.dg/modules/lambda-7_a.C @@ -18,3 +18,22 @@ export struct S { export inline int d(int x, int (*f)(int) = [](int x) { return x * 5; }) { return f(x); } + +// unevaluated lambdas +#if __cplusplus >= 202002L +export struct E : decltype([](int x) { return x * 6; }) { + decltype([](int x) { return x * 7; }) f; +}; + +export template +struct G : decltype([](int x) { return x * 8; }) { + decltype([](int x) { return x * 9; }) h; +}; + +template <> +struct G : decltype([](int x) { return x * 10; }) { + decltype([](int x) { return x * 11; }) i; +}; + +export decltype([](int x) { return x * 12; }) j; +#endif diff --git a/gcc/testsuite/g++.dg/modules/lambda-7_b.C b/gcc/testsuite/g++.dg/modules/lambda-7_b.C index a8762399ee1..59a82e05cbf 100644 --- a/gcc/testsuite/g++.dg/modules/lambda-7_b.C +++ b/gcc/testsuite/g++.dg/modules/lambda-7_b.C @@ -13,4 +13,27 @@ int main() { __builtin_abort(); if (d(10) != 50) __builtin_abort(); + +#if __cplusplus >= 202002L + E e; + if (e(10) != 60) + __builtin_abort(); + if (e.f(10) != 70) + __builtin_abort(); + + G g1; + if (g1(10) != 80) + __builtin_abort(); + if (g1.h(10) != 90) + __builtin_abort(); + + G g2; + if (g2(10) != 100) + __builtin_abort(); + if (g2.i(10) != 110) + __builtin_abort(); + + if (j(10) != 120) + __builtin_abort(); +#endif }