c++: ICE when shortening right shift [PR94955]
authorMarek Polacek <polacek@redhat.com>
Wed, 6 May 2020 19:53:33 +0000 (15:53 -0400)
committerMarek Polacek <polacek@redhat.com>
Mon, 18 May 2020 23:01:54 +0000 (19:01 -0400)
Since r10-6527 fold_for_warn calls maybe_constant_value, which means it
can fold more than it previously could.  In this testcase it means that
cp_build_binary_op/RSHIFT_EXPR set short_shift because now we were able
to fold op1 to an INTEGER_CST.  But then when actually performing the
shortening we crashed because cp_fold_rvalue wasn't able to fold as much
as f_f_w and so tree_int_cst_sgn crashed on a NOP_EXPR.  Therefore the
calls should probably match.

PR c++/94955
* typeck.c (cp_build_binary_op): Use fold_for_warn instead of
cp_fold_rvalue.

* g++.dg/cpp0x/constexpr-shift2.C: New test.

gcc/cp/ChangeLog
gcc/cp/typeck.c
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/cpp0x/constexpr-shift2.C [new file with mode: 0644]

index 605cdf33ae4e2545bba6ffce09e965649b677b78..4e446585aa42f4104057155e4c06a4f9573ea746 100644 (file)
@@ -1,3 +1,9 @@
+2020-05-18  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/94955
+       * typeck.c (cp_build_binary_op): Use fold_for_warn instead of
+       cp_fold_rvalue.
+
 2020-05-18  Marek Polacek  <polacek@redhat.com>
 
        PR c++/94937
index 768c62281f486ee2b34e3e2ce252056cb88434f5..d2e6c90762206644ea5f996c3a028450c131151c 100644 (file)
@@ -5633,7 +5633,9 @@ cp_build_binary_op (const op_location_t &location,
        {
          int unsigned_arg;
          tree arg0 = get_narrower (op0, &unsigned_arg);
-         tree const_op1 = cp_fold_rvalue (op1);
+         /* We're not really warning here but when we set short_shift we
+            used fold_for_warn to fold the operand.  */
+         tree const_op1 = fold_for_warn (op1);
 
          final_type = result_type;
 
index 67f2c550ff0ce029e1847f6f66847ad7b9e0701f..02de8028552991f2c2572deff25f99e661c54ce6 100644 (file)
@@ -1,3 +1,8 @@
+2020-05-18  Marek Polacek  <polacek@redhat.com>
+
+       PR c++/94955
+       * g++.dg/cpp0x/constexpr-shift2.C: New test.
+
 2020-05-18  Marek Polacek  <polacek@redhat.com>
 
        PR c++/94937
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-shift2.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-shift2.C
new file mode 100644 (file)
index 0000000..9b3490a
--- /dev/null
@@ -0,0 +1,12 @@
+// PR c++/94955
+// { dg-do compile { target c++11 } }
+
+struct S {
+  static constexpr char foo() { return 10; }
+};
+
+short int
+fn (short int e)
+{
+  return e >> S::foo();
+}