public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: gcc-patches@gcc.gnu.org
Cc: jason@redhat.com, nathan@acm.org, Patrick Palka <ppalka@redhat.com>
Subject: [PATCH] c++ modules: ICE with dynamic_cast [PR106304]
Date: Thu, 13 Oct 2022 15:04:27 -0400	[thread overview]
Message-ID: <20221013190427.181432-1-ppalka@redhat.com> (raw)

The FUNCTION_DECL we build for __dynamic_cast has an empty DECL_CONTEXT,
but trees_out::tree_node expects all FUNCTION_DECLs to have non-empty
DECL_CONTEXT thus we crash when streaming out the dynamic_cast in the
below testcase.

This patch naively fixes this by setting DECL_CONTEXT for __dynamic_cast
appropriately.  Like for __cxa_atexit which is similarly lazily declared,
I suppose we should push it into the namespace too.

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

	PR c++/106304

gcc/cp/ChangeLog:

	* constexpr.cc (cxx_dynamic_cast_fn_p): Check for abi_node
	instead of global_namespace.
	* rtti.cc (build_dynamic_cast_1): Set DECL_CONTEXT and
	DECL_SOURCE_LOCATION on dynamic_cast_node, and push it
	into the namespace.

gcc/testsuite/ChangeLog:

	* g++.dg/modules/pr106304_a.C: New test.
	* g++.dg/modules/pr106304_b.C: New test.
---
 gcc/cp/constexpr.cc                       |  2 +-
 gcc/cp/rtti.cc                            |  4 ++++
 gcc/testsuite/g++.dg/modules/pr106304_a.C | 12 ++++++++++++
 gcc/testsuite/g++.dg/modules/pr106304_b.C |  8 ++++++++
 4 files changed, 25 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/modules/pr106304_a.C
 create mode 100644 gcc/testsuite/g++.dg/modules/pr106304_b.C

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 06dcd71c926..5939d2882f8 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -2108,7 +2108,7 @@ cxx_dynamic_cast_fn_p (tree fndecl)
 {
   return (cxx_dialect >= cxx20
 	  && id_equal (DECL_NAME (fndecl), "__dynamic_cast")
-	  && CP_DECL_CONTEXT (fndecl) == global_namespace);
+	  && CP_DECL_CONTEXT (fndecl) == abi_node);
 }
 
 /* Often, we have an expression in the form of address + offset, e.g.
diff --git a/gcc/cp/rtti.cc b/gcc/cp/rtti.cc
index f5b43ec0fb2..a85c7b56409 100644
--- a/gcc/cp/rtti.cc
+++ b/gcc/cp/rtti.cc
@@ -787,6 +787,10 @@ build_dynamic_cast_1 (location_t loc, tree type, tree expr,
 			       NULL_TREE));
 	      dcast_fn = (build_library_fn_ptr
 			  (fn_name, fn_type, ECF_LEAF | ECF_PURE | ECF_NOTHROW));
+	      /* As with __cxa_atexit in get_atexit_node.  */
+	      DECL_CONTEXT (dcast_fn) = FROB_CONTEXT (current_namespace);
+	      DECL_SOURCE_LOCATION (dcast_fn) = BUILTINS_LOCATION;
+	      dcast_fn = pushdecl (dcast_fn, /*hiding=*/true);
 	      pop_abi_namespace (flags);
 	      dynamic_cast_node = dcast_fn;
 	    }
diff --git a/gcc/testsuite/g++.dg/modules/pr106304_a.C b/gcc/testsuite/g++.dg/modules/pr106304_a.C
new file mode 100644
index 00000000000..b999eeccf4a
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/pr106304_a.C
@@ -0,0 +1,12 @@
+// PR c++/106304
+// { dg-additional-options -fmodules-ts }
+// { dg-module-cmi pr106304 }
+
+export module pr106304;
+
+struct A { virtual ~A() = default; };
+struct B : A { };
+
+inline const B* as_b(const A& a) {
+  return dynamic_cast<const B*>(&a);
+}
diff --git a/gcc/testsuite/g++.dg/modules/pr106304_b.C b/gcc/testsuite/g++.dg/modules/pr106304_b.C
new file mode 100644
index 00000000000..e8333909c8d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/pr106304_b.C
@@ -0,0 +1,8 @@
+// PR c++/106304
+// { dg-additional-options -fmodules-ts }
+
+module pr106304;
+
+void f(A& a) {
+  as_b(a);
+}
-- 
2.38.0.68.ge85701b4af


             reply	other threads:[~2022-10-13 19:04 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-13 19:04 Patrick Palka [this message]
2022-10-14 11:20 ` Nathan Sidwell

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=20221013190427.181432-1-ppalka@redhat.com \
    --to=ppalka@redhat.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).