public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: Ensure DECL_CONTEXT is set for temporary vars [PR114005]
@ 2024-02-29 21:28 Nathaniel Shead
  2024-03-01 13:12 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Nathaniel Shead @ 2024-02-29 21:28 UTC (permalink / raw)
  To: gcc-patches; +Cc: Jason Merrill, Nathan Sidwell

Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk?

Alternatively we could update 'DECL_CONTEXT' only for
'make_temporary_var_for_ref_to_temp' in call.cc, as a more targetted
fix, but I felt that this way it'd also fix any other similar issues
that have gone uncaught so far.

-- >8 --

Modules streaming requires DECL_CONTEXT to be set for anything streamed.
This patch ensures that 'create_temporary_var' does set a DECL_CONTEXT
for these variables (such as the backing storage for initializer_lists)
even if not inside a function declaration.

	PR c++/114005

gcc/cp/ChangeLog:

	* init.cc (create_temporary_var): Set DECL_CONTEXT to
	current_namespace if at namespace scope.

gcc/testsuite/ChangeLog:

	* g++.dg/modules/pr114005_a.C: New test.
	* g++.dg/modules/pr114005_b.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
---
 gcc/cp/init.cc                            | 2 ++
 gcc/testsuite/g++.dg/modules/pr114005_a.C | 8 ++++++++
 gcc/testsuite/g++.dg/modules/pr114005_b.C | 7 +++++++
 3 files changed, 17 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/modules/pr114005_a.C
 create mode 100644 gcc/testsuite/g++.dg/modules/pr114005_b.C

diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
index ac37330527e..e6fca7b3226 100644
--- a/gcc/cp/init.cc
+++ b/gcc/cp/init.cc
@@ -4258,6 +4258,8 @@ create_temporary_var (tree type)
   DECL_ARTIFICIAL (decl) = 1;
   DECL_IGNORED_P (decl) = 1;
   DECL_CONTEXT (decl) = current_function_decl;
+  if (!DECL_CONTEXT (decl))
+    DECL_CONTEXT (decl) = current_namespace;
 
   return decl;
 }
diff --git a/gcc/testsuite/g++.dg/modules/pr114005_a.C b/gcc/testsuite/g++.dg/modules/pr114005_a.C
new file mode 100644
index 00000000000..404683484ec
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/pr114005_a.C
@@ -0,0 +1,8 @@
+// { dg-additional-options "-fmodules-ts" }
+// { dg-module-cmi M }
+
+module;
+#include <initializer_list>
+
+export module M;
+export constexpr std::initializer_list<int> foo{ 1, 2, 3 };
diff --git a/gcc/testsuite/g++.dg/modules/pr114005_b.C b/gcc/testsuite/g++.dg/modules/pr114005_b.C
new file mode 100644
index 00000000000..88317ce11f8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/pr114005_b.C
@@ -0,0 +1,7 @@
+// { dg-additional-options "-fmodules-ts" }
+
+import M;
+
+int main() {
+  return foo.size();
+}
-- 
2.43.2


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

* Re: [PATCH] c++: Ensure DECL_CONTEXT is set for temporary vars [PR114005]
  2024-02-29 21:28 [PATCH] c++: Ensure DECL_CONTEXT is set for temporary vars [PR114005] Nathaniel Shead
@ 2024-03-01 13:12 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2024-03-01 13:12 UTC (permalink / raw)
  To: Nathaniel Shead, gcc-patches; +Cc: Nathan Sidwell

On 2/29/24 16:28, Nathaniel Shead wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk?
> 
> Alternatively we could update 'DECL_CONTEXT' only for
> 'make_temporary_var_for_ref_to_temp' in call.cc, as a more targetted
> fix, but I felt that this way it'd also fix any other similar issues
> that have gone uncaught so far.
> 
> -- >8 --
> 
> Modules streaming requires DECL_CONTEXT to be set for anything streamed.
> This patch ensures that 'create_temporary_var' does set a DECL_CONTEXT
> for these variables (such as the backing storage for initializer_lists)
> even if not inside a function declaration.
> 
> 	PR c++/114005
> 
> gcc/cp/ChangeLog:
> 
> 	* init.cc (create_temporary_var): Set DECL_CONTEXT to
> 	current_namespace if at namespace scope.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/modules/pr114005_a.C: New test.
> 	* g++.dg/modules/pr114005_b.C: New test.
> 
> Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
> ---
>   gcc/cp/init.cc                            | 2 ++
>   gcc/testsuite/g++.dg/modules/pr114005_a.C | 8 ++++++++
>   gcc/testsuite/g++.dg/modules/pr114005_b.C | 7 +++++++
>   3 files changed, 17 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/modules/pr114005_a.C
>   create mode 100644 gcc/testsuite/g++.dg/modules/pr114005_b.C
> 
> diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc
> index ac37330527e..e6fca7b3226 100644
> --- a/gcc/cp/init.cc
> +++ b/gcc/cp/init.cc
> @@ -4258,6 +4258,8 @@ create_temporary_var (tree type)
>     DECL_ARTIFICIAL (decl) = 1;
>     DECL_IGNORED_P (decl) = 1;
>     DECL_CONTEXT (decl) = current_function_decl;
> +  if (!DECL_CONTEXT (decl))
> +    DECL_CONTEXT (decl) = current_namespace;

Maybe always set it to current_scope () instead of current_function_decl?

OK with that change.

Jason


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

end of thread, other threads:[~2024-03-01 13:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-29 21:28 [PATCH] c++: Ensure DECL_CONTEXT is set for temporary vars [PR114005] Nathaniel Shead
2024-03-01 13:12 ` Jason Merrill

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