public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Nathaniel Shead <nathanieloshead@gmail.com>
To: gcc-patches@gcc.gnu.org
Cc: Jason Merrill <jason@redhat.com>, Nathan Sidwell <nathan@acm.org>
Subject: [PATCH 2/1] c++: Also support lambdas attached to TYPE_DECLs in modules
Date: Sun, 11 Feb 2024 14:54:18 +1100	[thread overview]
Message-ID: <65c844ee.170a0220.88382.a8fa@mx.google.com> (raw)
In-Reply-To: <65c7ff51.170a0220.8dcc0.a404@mx.google.com>

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 <nathanieloshead@gmail.com>
---
 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 <typename T>
+struct G : decltype([](int x) { return x * 8; }) {
+  decltype([](int x) { return x * 9; }) h;
+};
+
+template <>
+struct G<double> : 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<int> g1;
+  if (g1(10) != 80)
+    __builtin_abort();
+  if (g1.h(10) != 90)
+    __builtin_abort();
+
+  G<double> g2;
+  if (g2(10) != 100)
+    __builtin_abort();
+  if (g2.i(10) != 110)
+    __builtin_abort();
+
+  if (j(10) != 120)
+    __builtin_abort();
+#endif
 }
-- 
2.43.0


  reply	other threads:[~2024-02-11  3:54 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-10 22:57 [PATCH] c++: Support lambdas attached to FIELD_DECLs in modules [PR111710] Nathaniel Shead
2024-02-11  3:54 ` Nathaniel Shead [this message]
2024-02-11  4:01   ` [PATCH 2/1] c++: Also support lambdas attached to TYPE_DECLs in modules Andrew Pinski
2024-02-11  4:24     ` Nathaniel Shead
2024-02-12 13:12   ` Marek Polacek
2024-02-14  2:44   ` Jason Merrill
2024-02-14  0:52 ` [PATCH] c++: Support lambdas attached to FIELD_DECLs in modules [PR111710] Jason Merrill
2024-02-16 12:21   ` [PATCH v2] c++/modules: Support lambdas attached to more places " Nathaniel Shead
2024-02-27 16:59     ` Patrick Palka
2024-02-28  4:12       ` [PATCH v3] " Nathaniel Shead
2024-02-28 17:34         ` Jason Merrill
2024-02-29  4:57           ` Nathaniel Shead

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=65c844ee.170a0220.88382.a8fa@mx.google.com \
    --to=nathanieloshead@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=nathan@acm.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).