public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-5810] c++: Support lambdas in static template member initialisers [PR107398]
@ 2023-11-24  2:32 Nathaniel Shead
  0 siblings, 0 replies; only message in thread
From: Nathaniel Shead @ 2023-11-24  2:32 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:cff1fa6625d1273fcfaf473e436ba918262d8afa

commit r14-5810-gcff1fa6625d1273fcfaf473e436ba918262d8afa
Author: Nathaniel Shead <nathanieloshead@gmail.com>
Date:   Mon Nov 13 20:11:15 2023 +1100

    c++: Support lambdas in static template member initialisers [PR107398]
    
    The testcase noted in the PR fails because the context of the lambda is
    not in namespace scope, but rather in class scope. This patch removes
    the assertion that the context must be a namespace and ensures that
    lambdas in class scope still get the correct merge_kind.
    
            PR c++/107398
    
    gcc/cp/ChangeLog:
    
            * module.cc (trees_out::get_merge_kind): Handle lambdas in class
            scope.
            (maybe_key_decl): Remove assertion and fix whitespace.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/modules/lambda-6_a.C: New test.
            * g++.dg/modules/lambda-6_b.C: New test.
    
    Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>

Diff:
---
 gcc/cp/module.cc                          | 38 +++++++++++++++++++------------
 gcc/testsuite/g++.dg/modules/lambda-6_a.C | 16 +++++++++++++
 gcc/testsuite/g++.dg/modules/lambda-6_b.C |  9 ++++++++
 3 files changed, 48 insertions(+), 15 deletions(-)

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index 4f5b6e2747a..33fcf396875 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -10418,13 +10418,16 @@ trees_out::get_merge_kind (tree decl, depset *dep)
 
 	  case RECORD_TYPE:
 	  case UNION_TYPE:
+	  case NAMESPACE_DECL:
 	    if (DECL_NAME (decl) == as_base_identifier)
-	      mk = MK_as_base;
-	    else if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
-	      mk = MK_field;
-	    break;
+	      {
+		mk = MK_as_base;
+		break;
+	      }
 
-	  case NAMESPACE_DECL:
+	    /* A lambda may have a class as its context, even though it
+	       isn't a member in the traditional sense; see the test
+	       g++.dg/modules/lambda-6_a.C.  */
 	    if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
 		&& LAMBDA_TYPE_P (TREE_TYPE (decl)))
 	      if (tree scope
@@ -10437,6 +10440,13 @@ trees_out::get_merge_kind (tree decl, depset *dep)
 		    break;
 		  }
 
+	    if (RECORD_OR_UNION_TYPE_P (ctx))
+	      {
+		if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
+		  mk = MK_field;
+		break;
+	      }
+
 	    if (TREE_CODE (decl) == TEMPLATE_DECL
 		&& DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
 	      mk = MK_local_friend;
@@ -18893,18 +18903,16 @@ maybe_key_decl (tree ctx, tree decl)
   if (TREE_CODE (ctx) != VAR_DECL)
     return;
 
-  gcc_checking_assert (DECL_NAMESPACE_SCOPE_P (ctx));
-
- if (!keyed_table)
+  if (!keyed_table)
     keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
 
- auto &vec = keyed_table->get_or_insert (ctx);
- if (!vec.length ())
-   {
-     retrofit_lang_decl (ctx);
-     DECL_MODULE_KEYED_DECLS_P (ctx) = true;
-   }
- vec.safe_push (decl);
+  auto &vec = keyed_table->get_or_insert (ctx);
+  if (!vec.length ())
+    {
+      retrofit_lang_decl (ctx);
+      DECL_MODULE_KEYED_DECLS_P (ctx) = true;
+    }
+  vec.safe_push (decl);
 }
 
 /* Create the flat name string.  It is simplest to have it handy.  */
diff --git a/gcc/testsuite/g++.dg/modules/lambda-6_a.C b/gcc/testsuite/g++.dg/modules/lambda-6_a.C
new file mode 100644
index 00000000000..28bfb358afb
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/lambda-6_a.C
@@ -0,0 +1,16 @@
+// PR c++/107398
+// { dg-additional-options "-fmodules-ts" }
+// { dg-module-cmi Lambda6 }
+
+export module Lambda6;
+
+template <typename T>
+struct R { static int x; };
+
+template <typename T>
+int R<T>::x = []{int i; return 1;}();
+
+export int foo();
+int foo() {
+  return R<int>::x;
+}
diff --git a/gcc/testsuite/g++.dg/modules/lambda-6_b.C b/gcc/testsuite/g++.dg/modules/lambda-6_b.C
new file mode 100644
index 00000000000..ab0c4ab4805
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/lambda-6_b.C
@@ -0,0 +1,9 @@
+// PR c++/107398
+// { dg-additional-options "-fmodules-ts" }
+
+import Lambda6;
+
+int main() {
+  if (foo() != 1)
+    __builtin_abort();
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-11-24  2:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-24  2:32 [gcc r14-5810] c++: Support lambdas in static template member initialisers [PR107398] Nathaniel Shead

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).