]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Ensure DECL_CONTEXT is set for temporary vars [PR114005]
authorNathaniel Shead <nathanieloshead@gmail.com>
Thu, 29 Feb 2024 11:49:13 +0000 (22:49 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Fri, 1 Mar 2024 22:04:15 +0000 (09:04 +1100)
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): Use current_scope instead of
current_function_decl.

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
gcc/testsuite/g++.dg/modules/pr114005_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/pr114005_b.C [new file with mode: 0644]

index 1a341f7e606189a9865726174e340d6ce19d04e5..d2586fad86b12da82863c4cfca5d76a1e5b980f9 100644 (file)
@@ -4258,7 +4258,7 @@ create_temporary_var (tree type)
   TREE_USED (decl) = 1;
   DECL_ARTIFICIAL (decl) = 1;
   DECL_IGNORED_P (decl) = 1;
-  DECL_CONTEXT (decl) = current_function_decl;
+  DECL_CONTEXT (decl) = current_scope ();
 
   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 (file)
index 0000000..4046834
--- /dev/null
@@ -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 (file)
index 0000000..88317ce
--- /dev/null
@@ -0,0 +1,7 @@
+// { dg-additional-options "-fmodules-ts" }
+
+import M;
+
+int main() {
+  return foo.size();
+}