]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: ICE with reinterpret_cast and switch [PR113545]
authorMarek Polacek <polacek@redhat.com>
Mon, 12 Feb 2024 19:53:24 +0000 (14:53 -0500)
committerMarek Polacek <polacek@redhat.com>
Mon, 12 Feb 2024 19:53:24 +0000 (14:53 -0500)
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
gcc/testsuite/g++.dg/cpp1y/constexpr-reinterpret3.C

index 2ebb1470dd5e644e72a1c3303898f0b8f268d00b..fa346fe01c9c67afa655394fcf151eb65e90cfaa 100644 (file)
@@ -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
index 51feb2e558e76b51dbf03505a742610c13efadd5..adc0b418df910c211a72f3d789c3bfc147861608 100644 (file)
@@ -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;