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: 1897408 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=RxGQBhMr; 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 4TXYd52Pqxz23g7 for ; Sun, 11 Feb 2024 14:54:47 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 74A713858C30 for ; Sun, 11 Feb 2024 03:54:43 +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-Google-Smtp-Source: AGHT+IH8nVvleG+gDjaRG7/BpBes2omdjn1I1l2Uy9wCrjTLmeBGX7iYT/qRh42gDrZQCe7wp635Nw== 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+incoming=patchwork.ozlabs.org@gcc.gnu.org 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 }