From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id EF57B3858429; Fri, 14 Oct 2022 13:07:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EF57B3858429 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665752836; bh=P4xa9brYmpIMMRPa6hcnRlZ+pZEfjXbQyiEvl3Y55vI=; h=From:To:Subject:Date:From; b=X7U9+Ztyp+qiM9Q8GmlPn6eO8f7YDjdFOk358Di/P1ubi+aGR1HGEfZMin5AaKghD GxincBwS+sMZMc0C/Ck3ROnAL8meIUrXg4oD1Jzcvhj07l/EghfHUmIQq+BpKqr9/d yXbBtnVUkEoqALa71nXIgtzG0S+0wtJgsbVeEMxY= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Patrick Palka To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-3299] c++ modules: ICE with dynamic_cast [PR106304] X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: a75e9bee7c23403d3cc7085a249f230de50c4c3e X-Git-Newrev: d6a488f243acfac4bdbbb0eefbee3ae916159cf5 Message-Id: <20221014130716.EF57B3858429@sourceware.org> Date: Fri, 14 Oct 2022 13:07:16 +0000 (GMT) List-Id: https://gcc.gnu.org/g:d6a488f243acfac4bdbbb0eefbee3ae916159cf5 commit r13-3299-gd6a488f243acfac4bdbbb0eefbee3ae916159cf5 Author: Patrick Palka Date: Fri Oct 14 09:07:01 2022 -0400 c++ modules: ICE with dynamic_cast [PR106304] 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. Diff: --- 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(-) diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index fb171a2784f..03663961bb8 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -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. 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(&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); +}