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

Reply via email to