c++: ICE with __dynamic_cast redecl [PR115501]

Message ID 20240618145824.447538-1-polacek@redhat.com
State Committed
Commit 71f484d02b2b3e8616cd7af27a0d4c72e4c7e977
Headers
Series c++: ICE with __dynamic_cast redecl [PR115501] |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_gcc_check--master-arm success Test passed

Commit Message

Marek Polacek June 18, 2024, 2:58 p.m. UTC
  Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

-- >8 --
Since r13-3299, build_dynamic_cast_1 calls pushdecl which calls
duplicate_decls and that in this testcase emits the "conflicting
declaration" error and returns error_mark_node, so the subsequent
build_cxx_call crashes on the error_mark_node.

	PR c++/115501

gcc/cp/ChangeLog:

	* rtti.cc (build_dynamic_cast_1): Return if dcast_fn is erroneous.

gcc/testsuite/ChangeLog:

	* g++.dg/rtti/dyncast8.C: New test.
---
 gcc/cp/rtti.cc                       |  2 ++
 gcc/testsuite/g++.dg/rtti/dyncast8.C | 15 +++++++++++++++
 2 files changed, 17 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/rtti/dyncast8.C


base-commit: e4f938936867d8799775d1455e67bd3fb8711afd
  

Comments

Jason Merrill June 25, 2024, 2:05 a.m. UTC | #1
On 6/18/24 10:58, Marek Polacek wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

> -- >8 --
> Since r13-3299, build_dynamic_cast_1 calls pushdecl which calls
> duplicate_decls and that in this testcase emits the "conflicting
> declaration" error and returns error_mark_node, so the subsequent
> build_cxx_call crashes on the error_mark_node.
> 
> 	PR c++/115501
> 
> gcc/cp/ChangeLog:
> 
> 	* rtti.cc (build_dynamic_cast_1): Return if dcast_fn is erroneous.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/rtti/dyncast8.C: New test.
> ---
>   gcc/cp/rtti.cc                       |  2 ++
>   gcc/testsuite/g++.dg/rtti/dyncast8.C | 15 +++++++++++++++
>   2 files changed, 17 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/rtti/dyncast8.C
> 
> diff --git a/gcc/cp/rtti.cc b/gcc/cp/rtti.cc
> index ed69606f4dd..cc006ea927f 100644
> --- a/gcc/cp/rtti.cc
> +++ b/gcc/cp/rtti.cc
> @@ -794,6 +794,8 @@ build_dynamic_cast_1 (location_t loc, tree type, tree expr,
>   	      pop_abi_namespace (flags);
>   	      dynamic_cast_node = dcast_fn;
>   	    }
> +	  if (dcast_fn == error_mark_node)
> +	    return error_mark_node;
>   	  result = build_cxx_call (dcast_fn, 4, elems, complain);
>   	  SET_EXPR_LOCATION (result, loc);
>   
> diff --git a/gcc/testsuite/g++.dg/rtti/dyncast8.C b/gcc/testsuite/g++.dg/rtti/dyncast8.C
> new file mode 100644
> index 00000000000..de23433dd9b
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/rtti/dyncast8.C
> @@ -0,0 +1,15 @@
> +// PR c++/115501
> +// { dg-do compile }
> +
> +struct s{virtual void f();};
> +struct s1 : s{};
> +namespace __cxxabiv1
> +{
> +  extern "C" void __dynamic_cast(); // { dg-message "previous declaration" }
> +}
> +void diagnostic_information_impl(s const *se)
> +{
> +  dynamic_cast<s1 const *>(se);
> +}
> +
> +// { dg-error "conflicting declaration" "" { target *-*-* } 0 }
> 
> base-commit: e4f938936867d8799775d1455e67bd3fb8711afd
  

Patch

diff --git a/gcc/cp/rtti.cc b/gcc/cp/rtti.cc
index ed69606f4dd..cc006ea927f 100644
--- a/gcc/cp/rtti.cc
+++ b/gcc/cp/rtti.cc
@@ -794,6 +794,8 @@  build_dynamic_cast_1 (location_t loc, tree type, tree expr,
 	      pop_abi_namespace (flags);
 	      dynamic_cast_node = dcast_fn;
 	    }
+	  if (dcast_fn == error_mark_node)
+	    return error_mark_node;
 	  result = build_cxx_call (dcast_fn, 4, elems, complain);
 	  SET_EXPR_LOCATION (result, loc);
 
diff --git a/gcc/testsuite/g++.dg/rtti/dyncast8.C b/gcc/testsuite/g++.dg/rtti/dyncast8.C
new file mode 100644
index 00000000000..de23433dd9b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/rtti/dyncast8.C
@@ -0,0 +1,15 @@ 
+// PR c++/115501
+// { dg-do compile }
+
+struct s{virtual void f();};
+struct s1 : s{};
+namespace __cxxabiv1
+{
+  extern "C" void __dynamic_cast(); // { dg-message "previous declaration" }
+}
+void diagnostic_information_impl(s const *se)
+{
+  dynamic_cast<s1 const *>(se);
+}
+
+// { dg-error "conflicting declaration" "" { target *-*-* } 0 }