public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Nathaniel Shead <nathanieloshead@gmail.com>
To: gcc-patches@gcc.gnu.org
Cc: Jason Merrill <jason@redhat.com>, Nathan Sidwell <nathan@acm.org>
Subject: [PATCH] c++: Ensure DECL_CONTEXT is set for temporary vars [PR114005]
Date: Fri, 1 Mar 2024 08:28:00 +1100	[thread overview]
Message-ID: <65e0f6e4.a70a0220.12e52.86bd@mx.google.com> (raw)

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


             reply	other threads:[~2024-02-29 21:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-29 21:28 Nathaniel Shead [this message]
2024-03-01 13:12 ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=65e0f6e4.a70a0220.12e52.86bd@mx.google.com \
    --to=nathanieloshead@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --cc=nathan@acm.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).