]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Support lambdas in static template member initialisers [PR107398]
authorNathaniel Shead <nathanieloshead@gmail.com>
Mon, 13 Nov 2023 09:11:15 +0000 (20:11 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Fri, 24 Nov 2023 02:31:11 +0000 (13:31 +1100)
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>
gcc/cp/module.cc
gcc/testsuite/g++.dg/modules/lambda-6_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/lambda-6_b.C [new file with mode: 0644]

index 4f5b6e2747a7de3b5187ebbea5306e77966a1b1a..33fcf396875b3c2c1d62d33fc095eb3db667a76f 100644 (file)
@@ -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 (file)
index 0000000..28bfb35
--- /dev/null
@@ -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 (file)
index 0000000..ab0c4ab
--- /dev/null
@@ -0,0 +1,9 @@
+// PR c++/107398
+// { dg-additional-options "-fmodules-ts" }
+
+import Lambda6;
+
+int main() {
+  if (foo() != 1)
+    __builtin_abort();
+}