diff mbox series

c++: Ensure DECL_CONTEXT is set for temporary vars [PR114005]

Message ID 65e0f6e4.a70a0220.12e52.86bd@mx.google.com
State New
Headers show
Series c++: Ensure DECL_CONTEXT is set for temporary vars [PR114005] | expand

Commit Message

Nathaniel Shead Feb. 29, 2024, 9:28 p.m. UTC
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

Comments

Jason Merrill March 1, 2024, 1:12 p.m. UTC | #1
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
diff mbox series

Patch

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