On Thu, Nov 23, 2023 at 11:49:39AM -0500, Nathan Sidwell wrote:
> On 11/13/23 06:58, Nathaniel Shead wrote:
> > Bootstrapped and regtested on x86_64-pc-linux-gnu. I don't have write 
> > access.
> > 
> > -- >8 --
> > 
> > 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.
> 
> ok, could you put a comment in the lambda chcking part that it's context
> might be a class, even though it's not member in the conventional sense?
> 
> nathan
> 

Thanks, I've committed the following patch.

-- >8 --

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 <nathanielosh...@gmail.com>

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();
+}

Reply via email to