public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: ICE with reinterpret_cast and switch [PR113545]
@ 2024-02-10 22:42 Marek Polacek
  2024-02-12 19:13 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Marek Polacek @ 2024-02-10 22:42 UTC (permalink / raw)
  To: Jason Merrill, GCC Patches

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

-- >8 --
Jason, this is the patch you proposed for PR113545.  It looks very safe
so I'm posting it here so that it's not forgotten.

	PR c++/113545

gcc/cp/ChangeLog:

	* constexpr.cc (cxx_eval_switch_expr): If the condition doesn't reduce
	to an INTEGER_CST, consider it non-constant.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp1y/constexpr-reinterpret3.C: Remove dg-ice.
---
 gcc/cp/constexpr.cc                                 | 10 ++++++++++
 gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C |  4 +---
 2 files changed, 11 insertions(+), 3 deletions(-)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 2ebb1470dd5..fa346fe01c9 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -7106,6 +7106,16 @@ cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
   cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
 				       non_constant_p, overflow_p);
   VERIFY_CONSTANT (cond);
+  if (TREE_CODE (cond) != INTEGER_CST)
+    {
+      /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
+	 switch condition even if it's constant enough for other things
+	 (c++/113545).  */
+      gcc_checking_assert (ctx->quiet);
+      *non_constant_p = true;
+      return t;
+    }
+
   *jump_target = cond;
 
   tree body
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C
index 51feb2e558e..adc0b418df9 100644
--- a/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C
@@ -1,7 +1,5 @@
 // PR c++/113545
-// { dg-do compile { target c++14 } }
-// Please change the above "dg-do compile" to "dg-do run" when the ICE is resolved.
-// { dg-ice "PR112545 - constexpr function with switch called for reinterpret_cast" }
+// { dg-do run { target c++14 } }
 
 char foo;
 

base-commit: 93e1559bea434a681208e5e7a21513d7da2844d6
prerequisite-patch-id: 9dda211bd29817f18d2d46c3c5a7bbe3708cf9f2
-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] c++: ICE with reinterpret_cast and switch [PR113545]
  2024-02-10 22:42 [PATCH] c++: ICE with reinterpret_cast and switch [PR113545] Marek Polacek
@ 2024-02-12 19:13 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2024-02-12 19:13 UTC (permalink / raw)
  To: Marek Polacek, GCC Patches

On 2/10/24 17:42, Marek Polacek wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK, thanks.

> -- >8 --
> Jason, this is the patch you proposed for PR113545.  It looks very safe
> so I'm posting it here so that it's not forgotten.
> 
> 	PR c++/113545
> 
> gcc/cp/ChangeLog:
> 
> 	* constexpr.cc (cxx_eval_switch_expr): If the condition doesn't reduce
> 	to an INTEGER_CST, consider it non-constant.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp1y/constexpr-reinterpret3.C: Remove dg-ice.
> ---
>   gcc/cp/constexpr.cc                                 | 10 ++++++++++
>   gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C |  4 +---
>   2 files changed, 11 insertions(+), 3 deletions(-)
> 
> diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
> index 2ebb1470dd5..fa346fe01c9 100644
> --- a/gcc/cp/constexpr.cc
> +++ b/gcc/cp/constexpr.cc
> @@ -7106,6 +7106,16 @@ cxx_eval_switch_expr (const constexpr_ctx *ctx, tree t,
>     cond = cxx_eval_constant_expression (ctx, cond, vc_prvalue,
>   				       non_constant_p, overflow_p);
>     VERIFY_CONSTANT (cond);
> +  if (TREE_CODE (cond) != INTEGER_CST)
> +    {
> +      /* If the condition doesn't reduce to an INTEGER_CST it isn't a usable
> +	 switch condition even if it's constant enough for other things
> +	 (c++/113545).  */
> +      gcc_checking_assert (ctx->quiet);
> +      *non_constant_p = true;
> +      return t;
> +    }
> +
>     *jump_target = cond;
>   
>     tree body
> diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C
> index 51feb2e558e..adc0b418df9 100644
> --- a/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C
> +++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C
> @@ -1,7 +1,5 @@
>   // PR c++/113545
> -// { dg-do compile { target c++14 } }
> -// Please change the above "dg-do compile" to "dg-do run" when the ICE is resolved.
> -// { dg-ice "PR112545 - constexpr function with switch called for reinterpret_cast" }
> +// { dg-do run { target c++14 } }
>   
>   char foo;
>   
> 
> base-commit: 93e1559bea434a681208e5e7a21513d7da2844d6
> prerequisite-patch-id: 9dda211bd29817f18d2d46c3c5a7bbe3708cf9f2


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-02-12 19:13 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-10 22:42 [PATCH] c++: ICE with reinterpret_cast and switch [PR113545] Marek Polacek
2024-02-12 19:13 ` Jason Merrill

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).