]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++ modules: ICE with dynamic_cast [PR106304]
authorPatrick Palka <ppalka@redhat.com>
Fri, 14 Oct 2022 13:07:01 +0000 (09:07 -0400)
committerPatrick Palka <ppalka@redhat.com>
Fri, 14 Oct 2022 13:07:01 +0000 (09:07 -0400)
The FUNCTION_DECL we build for __dynamic_cast has an empty DECL_CONTEXT
but trees_out::tree_node expects 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.  I suppose we should push it into the namespace too, like
we do for __cxa_atexit which is similarly lazily declared.

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 when building dynamic_cast_node.  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
gcc/cp/rtti.cc
gcc/testsuite/g++.dg/modules/pr106304_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/pr106304_b.C [new file with mode: 0644]

index fb171a2784fed48ee3cc2f3e00ffce15a9505684..03663961bb8bf04234f23477fe629ad7b9f36f75 100644 (file)
@@ -2088,7 +2088,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.
index f5b43ec0fb258656980cfd7d72e5d68eb36f9a4b..a85c7b56409035bbc19d7de3011a67bbae06e961 100644 (file)
@@ -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 (file)
index 0000000..b999eec
--- /dev/null
@@ -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 (file)
index 0000000..e833390
--- /dev/null
@@ -0,0 +1,8 @@
+// PR c++/106304
+// { dg-additional-options -fmodules-ts }
+
+module pr106304;
+
+void f(A& a) {
+  as_b(a);
+}