On Mon, Aug 31, 2020 at 10:33:20AM +0200, Richard Biener wrote:
> In any case type mismatches here are of course unfortunate
> and both more verification and documentation would be
> nice.  verify_gimple_switch only verifies all case labels
> have the same type, the type of the switch argument is
> not verified in any way against that type.

When looking at the verification, I have noticed a bug in it.

The verification that CASE_HIGH (if present) has the same type as CASE_LOW
is only performed for the case label 2 and higher, case label 1 (the first
one after the default label) isn't checked.

The following patch fixes that, it will uselessly also compare
TREE_TYPE (CASE_LOW (elt)) != elt_type for the case label 1, but I think
that isn't that expensive and helps readability of the code.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2020-08-31  Jakub Jelinek  <ja...@redhat.com>

        * tree-cfg.c (verify_gimple_switch): If the first non-default case
        label has CASE_HIGH, verify it has the same type as CASE_LOW.

--- gcc/tree-cfg.c.jj   2020-08-27 18:42:35.660711349 +0200
+++ gcc/tree-cfg.c      2020-08-27 22:46:34.574154370 +0200
@@ -4809,17 +4809,7 @@ verify_gimple_switch (gswitch *stmt)
          return true;
        }
 
-      if (elt_type)
-       {
-         if (TREE_TYPE (CASE_LOW (elt)) != elt_type
-             || (CASE_HIGH (elt) && TREE_TYPE (CASE_HIGH (elt)) != elt_type))
-           {
-             error ("type mismatch for case label in switch statement");
-             debug_generic_expr (elt);
-             return true;
-           }
-       }
-      else
+      if (! elt_type)
        {
          elt_type = TREE_TYPE (CASE_LOW (elt));
          if (TYPE_PRECISION (index_type) < TYPE_PRECISION (elt_type))
@@ -4828,6 +4818,13 @@ verify_gimple_switch (gswitch *stmt)
              return true;
            }
        }
+      if (TREE_TYPE (CASE_LOW (elt)) != elt_type
+          || (CASE_HIGH (elt) && TREE_TYPE (CASE_HIGH (elt)) != elt_type))
+       {
+         error ("type mismatch for case label in switch statement");
+         debug_generic_expr (elt);
+         return true;
+       }
 
       if (prev_upper_bound)
        {


        Jakub

Reply via email to