public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++/modules: Support lambdas in static template member initialisers [PR107398]
@ 2023-11-13 11:58 Nathaniel Shead
  2023-11-23 16:49 ` Nathan Sidwell
  0 siblings, 1 reply; 3+ messages in thread
From: Nathaniel Shead @ 2023-11-13 11:58 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jason Merrill, Nathan Sidwell

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.

	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                          | 35 +++++++++++++----------
 gcc/testsuite/g++.dg/modules/lambda-6_a.C | 16 +++++++++++
 gcc/testsuite/g++.dg/modules/lambda-6_b.C |  9 ++++++
 3 files changed, 45 insertions(+), 15 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/modules/lambda-6_a.C
 create mode 100644 gcc/testsuite/g++.dg/modules/lambda-6_b.C

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index c1c8c226bc1..434caf22d1d 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -10412,13 +10412,13 @@ 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:
 	    if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
 		&& LAMBDA_TYPE_P (TREE_TYPE (decl)))
 	      if (tree scope
@@ -10431,6 +10431,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;
@@ -18887,18 +18894,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();
+}
-- 
2.42.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] c++/modules: Support lambdas in static template member initialisers [PR107398]
  2023-11-13 11:58 [PATCH] c++/modules: Support lambdas in static template member initialisers [PR107398] Nathaniel Shead
@ 2023-11-23 16:49 ` Nathan Sidwell
  2023-11-24  2:37   ` Nathaniel Shead
  0 siblings, 1 reply; 3+ messages in thread
From: Nathan Sidwell @ 2023-11-23 16:49 UTC (permalink / raw)
  To: Nathaniel Shead, gcc-patches; +Cc: Jason Merrill

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

> 
> 	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                          | 35 +++++++++++++----------
>   gcc/testsuite/g++.dg/modules/lambda-6_a.C | 16 +++++++++++
>   gcc/testsuite/g++.dg/modules/lambda-6_b.C |  9 ++++++
>   3 files changed, 45 insertions(+), 15 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/modules/lambda-6_a.C
>   create mode 100644 gcc/testsuite/g++.dg/modules/lambda-6_b.C
> 
> diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
> index c1c8c226bc1..434caf22d1d 100644
> --- a/gcc/cp/module.cc
> +++ b/gcc/cp/module.cc
> @@ -10412,13 +10412,13 @@ 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:
>   	    if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
>   		&& LAMBDA_TYPE_P (TREE_TYPE (decl)))
>   	      if (tree scope
> @@ -10431,6 +10431,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;
> @@ -18887,18 +18894,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();
> +}

-- 
Nathan Sidwell


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] c++/modules: Support lambdas in static template member initialisers [PR107398]
  2023-11-23 16:49 ` Nathan Sidwell
@ 2023-11-24  2:37   ` Nathaniel Shead
  0 siblings, 0 replies; 3+ messages in thread
From: Nathaniel Shead @ 2023-11-24  2:37 UTC (permalink / raw)
  To: Nathan Sidwell; +Cc: gcc-patches, Jason Merrill

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-11-24  2:37 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-13 11:58 [PATCH] c++/modules: Support lambdas in static template member initialisers [PR107398] Nathaniel Shead
2023-11-23 16:49 ` Nathan Sidwell
2023-11-24  2:37   ` 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).