diff mbox

C++ PATCH for c++/70285 (verify_gimple ICE with ?: and bit-field)

Message ID 56F063C7.5090107@redhat.com
State New
Headers show

Commit Message

Jason Merrill March 21, 2016, 9:12 p.m. UTC
The C++ front end builds COND_EXPR where the arms can have a different 
type from the combined expression, if they are bit-fields. 
cp_genericize_r already fixes this up, but now cp_fold needs to handle 
it too.

Tested x86_64-pc-linux-gnu, applying to trunk.
diff mbox

Patch

commit c1b91b53f85c8a00c5067dbfc15f5fb0da7da4f6
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Mar 21 15:54:51 2016 -0400

    	PR c++/70285
    	* cp-gimplify.c (cp_fold) [COND_EXPR]: Handle bit-fields.

diff --git a/gcc/cp/cp-gimplify.c b/gcc/cp/cp-gimplify.c
index 6a767fa..9bf2482 100644
--- a/gcc/cp/cp-gimplify.c
+++ b/gcc/cp/cp-gimplify.c
@@ -2130,6 +2130,12 @@  cp_fold (tree x)
       else
 	x = fold (x);
 
+      /* A COND_EXPR might have incompatible types in branches if one or both
+	 arms are bitfields.  If folding exposed such a branch, fix it up.  */
+      if (TREE_CODE (x) != code)
+	if (tree type = is_bitfield_expr_with_lowered_type (x))
+	  x = fold_convert (type, x);
+
       break;
 
     case CALL_EXPR:
diff --git a/gcc/testsuite/g++.dg/other/bitfield5.C b/gcc/testsuite/g++.dg/other/bitfield5.C
new file mode 100644
index 0000000..b8cd4dd
--- /dev/null
+++ b/gcc/testsuite/g++.dg/other/bitfield5.C
@@ -0,0 +1,15 @@ 
+// PR c++/70285
+
+int a;
+
+struct S
+{
+  int i:8;
+} b;
+
+int
+fn1 (bool x)
+{
+  (&fn1 ? b.i : a) = 42;
+  return (&fn1 ? b.i : a);
+}