]> git.lizzy.rs Git - rust.git/commitdiff
fix exceeding_bitshift lint and test
authorRalf Jung <post@ralfj.de>
Sat, 15 Feb 2020 10:43:54 +0000 (11:43 +0100)
committerRalf Jung <post@ralfj.de>
Sat, 15 Feb 2020 10:43:54 +0000 (11:43 +0100)
src/librustc_mir/transform/const_prop.rs
src/test/ui/lint/lint-exceeding-bitshifts.default.stderr [new file with mode: 0644]
src/test/ui/lint/lint-exceeding-bitshifts.noopt.stderr [new file with mode: 0644]
src/test/ui/lint/lint-exceeding-bitshifts.opt.stderr [new file with mode: 0644]
src/test/ui/lint/lint-exceeding-bitshifts.opt_with_overflow_checks.stderr [new file with mode: 0644]
src/test/ui/lint/lint-exceeding-bitshifts.rs
src/test/ui/lint/lint-exceeding-bitshifts.stderr [deleted file]
src/test/ui/lint/lint-exceeding-bitshifts2.rs [deleted file]
src/test/ui/lint/lint-exceeding-bitshifts2.stderr [deleted file]

index 53acf1490bf58fb9a6de6d2a26536865ee62122c..05e70ed217a5f6042df17b47b225d395882cf0ca 100644 (file)
@@ -547,16 +547,18 @@ fn check_binary_op(
         left: &Operand<'tcx>,
         right: &Operand<'tcx>,
         source_info: SourceInfo,
-        place_layout: TyLayout<'tcx>,
     ) -> Option<()> {
         let r =
             self.use_ecx(|this| this.ecx.read_immediate(this.ecx.eval_operand(right, None)?))?;
         // Check for exceeding shifts *even if* we cannot evaluate the LHS.
         if op == BinOp::Shr || op == BinOp::Shl {
-            let left_bits = place_layout.size.bits();
+            // We need the type of the LHS. We cannot use `place_layout` as that is the type
+            // of the result, which for checked binops is not the same!
+            let left_ty = left.ty(&self.local_decls, self.tcx);
+            let left_size_bits = self.ecx.layout_of(left_ty).ok()?.size.bits();
             let right_size = r.layout.size;
             let r_bits = r.to_scalar().and_then(|r| r.to_bits(right_size));
-            if r_bits.map_or(false, |b| b >= left_bits as u128) {
+            if r_bits.map_or(false, |b| b >= left_size_bits as u128) {
                 self.report_assert_as_lint(
                     lint::builtin::EXCEEDING_BITSHIFTS,
                     source_info,
@@ -618,7 +620,7 @@ fn const_prop(
             }
             Rvalue::BinaryOp(op, left, right) => {
                 trace!("checking BinaryOp(op = {:?}, left = {:?}, right = {:?})", op, left, right);
-                self.check_binary_op(*op, left, right, source_info, place_layout)?;
+                self.check_binary_op(*op, left, right, source_info)?;
             }
             Rvalue::CheckedBinaryOp(op, left, right) => {
                 trace!(
@@ -627,7 +629,7 @@ fn const_prop(
                     left,
                     right
                 );
-                self.check_binary_op(*op, left, right, source_info, place_layout)?;
+                self.check_binary_op(*op, left, right, source_info)?;
             }
 
             // Do not try creating references (#67862)
diff --git a/src/test/ui/lint/lint-exceeding-bitshifts.default.stderr b/src/test/ui/lint/lint-exceeding-bitshifts.default.stderr
new file mode 100644 (file)
index 0000000..5e32466
--- /dev/null
@@ -0,0 +1,146 @@
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:22:13
+   |
+LL |     let _ = x << 42;
+   |             ^^^^^^^ attempt to shift left with overflow
+   |
+note: the lint level is defined here
+  --> $DIR/lint-exceeding-bitshifts.rs:9:9
+   |
+LL | #![deny(exceeding_bitshifts, const_err)]
+   |         ^^^^^^^^^^^^^^^^^^^
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:27:15
+   |
+LL |       let n = 1u8 << 8;
+   |               ^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:29:15
+   |
+LL |       let n = 1u16 << 16;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:31:15
+   |
+LL |       let n = 1u32 << 32;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:33:15
+   |
+LL |       let n = 1u64 << 64;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:35:15
+   |
+LL |       let n = 1i8 << 8;
+   |               ^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:37:15
+   |
+LL |       let n = 1i16 << 16;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:39:15
+   |
+LL |       let n = 1i32 << 32;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:41:15
+   |
+LL |       let n = 1i64 << 64;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:44:15
+   |
+LL |       let n = 1u8 >> 8;
+   |               ^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:46:15
+   |
+LL |       let n = 1u16 >> 16;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:48:15
+   |
+LL |       let n = 1u32 >> 32;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:50:15
+   |
+LL |       let n = 1u64 >> 64;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:52:15
+   |
+LL |       let n = 1i8 >> 8;
+   |               ^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:54:15
+   |
+LL |       let n = 1i16 >> 16;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:56:15
+   |
+LL |       let n = 1i32 >> 32;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:58:15
+   |
+LL |       let n = 1i64 >> 64;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:62:15
+   |
+LL |       let n = n << 8;
+   |               ^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:64:15
+   |
+LL |       let n = 1u8 << -8;
+   |               ^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:69:15
+   |
+LL |       let n = 1u8 << (4+4);
+   |               ^^^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:71:15
+   |
+LL |       let n = 1i64 >> [64][0];
+   |               ^^^^^^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:77:15
+   |
+LL |       let n = 1_isize << BITS;
+   |               ^^^^^^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:78:15
+   |
+LL |       let n = 1_usize << BITS;
+   |               ^^^^^^^^^^^^^^^ attempt to shift left with overflow
+
+error: aborting due to 23 previous errors
+
diff --git a/src/test/ui/lint/lint-exceeding-bitshifts.noopt.stderr b/src/test/ui/lint/lint-exceeding-bitshifts.noopt.stderr
new file mode 100644 (file)
index 0000000..5e32466
--- /dev/null
@@ -0,0 +1,146 @@
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:22:13
+   |
+LL |     let _ = x << 42;
+   |             ^^^^^^^ attempt to shift left with overflow
+   |
+note: the lint level is defined here
+  --> $DIR/lint-exceeding-bitshifts.rs:9:9
+   |
+LL | #![deny(exceeding_bitshifts, const_err)]
+   |         ^^^^^^^^^^^^^^^^^^^
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:27:15
+   |
+LL |       let n = 1u8 << 8;
+   |               ^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:29:15
+   |
+LL |       let n = 1u16 << 16;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:31:15
+   |
+LL |       let n = 1u32 << 32;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:33:15
+   |
+LL |       let n = 1u64 << 64;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:35:15
+   |
+LL |       let n = 1i8 << 8;
+   |               ^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:37:15
+   |
+LL |       let n = 1i16 << 16;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:39:15
+   |
+LL |       let n = 1i32 << 32;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:41:15
+   |
+LL |       let n = 1i64 << 64;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:44:15
+   |
+LL |       let n = 1u8 >> 8;
+   |               ^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:46:15
+   |
+LL |       let n = 1u16 >> 16;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:48:15
+   |
+LL |       let n = 1u32 >> 32;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:50:15
+   |
+LL |       let n = 1u64 >> 64;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:52:15
+   |
+LL |       let n = 1i8 >> 8;
+   |               ^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:54:15
+   |
+LL |       let n = 1i16 >> 16;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:56:15
+   |
+LL |       let n = 1i32 >> 32;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:58:15
+   |
+LL |       let n = 1i64 >> 64;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:62:15
+   |
+LL |       let n = n << 8;
+   |               ^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:64:15
+   |
+LL |       let n = 1u8 << -8;
+   |               ^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:69:15
+   |
+LL |       let n = 1u8 << (4+4);
+   |               ^^^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:71:15
+   |
+LL |       let n = 1i64 >> [64][0];
+   |               ^^^^^^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:77:15
+   |
+LL |       let n = 1_isize << BITS;
+   |               ^^^^^^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:78:15
+   |
+LL |       let n = 1_usize << BITS;
+   |               ^^^^^^^^^^^^^^^ attempt to shift left with overflow
+
+error: aborting due to 23 previous errors
+
diff --git a/src/test/ui/lint/lint-exceeding-bitshifts.opt.stderr b/src/test/ui/lint/lint-exceeding-bitshifts.opt.stderr
new file mode 100644 (file)
index 0000000..5e32466
--- /dev/null
@@ -0,0 +1,146 @@
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:22:13
+   |
+LL |     let _ = x << 42;
+   |             ^^^^^^^ attempt to shift left with overflow
+   |
+note: the lint level is defined here
+  --> $DIR/lint-exceeding-bitshifts.rs:9:9
+   |
+LL | #![deny(exceeding_bitshifts, const_err)]
+   |         ^^^^^^^^^^^^^^^^^^^
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:27:15
+   |
+LL |       let n = 1u8 << 8;
+   |               ^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:29:15
+   |
+LL |       let n = 1u16 << 16;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:31:15
+   |
+LL |       let n = 1u32 << 32;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:33:15
+   |
+LL |       let n = 1u64 << 64;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:35:15
+   |
+LL |       let n = 1i8 << 8;
+   |               ^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:37:15
+   |
+LL |       let n = 1i16 << 16;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:39:15
+   |
+LL |       let n = 1i32 << 32;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:41:15
+   |
+LL |       let n = 1i64 << 64;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:44:15
+   |
+LL |       let n = 1u8 >> 8;
+   |               ^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:46:15
+   |
+LL |       let n = 1u16 >> 16;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:48:15
+   |
+LL |       let n = 1u32 >> 32;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:50:15
+   |
+LL |       let n = 1u64 >> 64;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:52:15
+   |
+LL |       let n = 1i8 >> 8;
+   |               ^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:54:15
+   |
+LL |       let n = 1i16 >> 16;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:56:15
+   |
+LL |       let n = 1i32 >> 32;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:58:15
+   |
+LL |       let n = 1i64 >> 64;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:62:15
+   |
+LL |       let n = n << 8;
+   |               ^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:64:15
+   |
+LL |       let n = 1u8 << -8;
+   |               ^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:69:15
+   |
+LL |       let n = 1u8 << (4+4);
+   |               ^^^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:71:15
+   |
+LL |       let n = 1i64 >> [64][0];
+   |               ^^^^^^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:77:15
+   |
+LL |       let n = 1_isize << BITS;
+   |               ^^^^^^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:78:15
+   |
+LL |       let n = 1_usize << BITS;
+   |               ^^^^^^^^^^^^^^^ attempt to shift left with overflow
+
+error: aborting due to 23 previous errors
+
diff --git a/src/test/ui/lint/lint-exceeding-bitshifts.opt_with_overflow_checks.stderr b/src/test/ui/lint/lint-exceeding-bitshifts.opt_with_overflow_checks.stderr
new file mode 100644 (file)
index 0000000..5e32466
--- /dev/null
@@ -0,0 +1,146 @@
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:22:13
+   |
+LL |     let _ = x << 42;
+   |             ^^^^^^^ attempt to shift left with overflow
+   |
+note: the lint level is defined here
+  --> $DIR/lint-exceeding-bitshifts.rs:9:9
+   |
+LL | #![deny(exceeding_bitshifts, const_err)]
+   |         ^^^^^^^^^^^^^^^^^^^
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:27:15
+   |
+LL |       let n = 1u8 << 8;
+   |               ^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:29:15
+   |
+LL |       let n = 1u16 << 16;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:31:15
+   |
+LL |       let n = 1u32 << 32;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:33:15
+   |
+LL |       let n = 1u64 << 64;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:35:15
+   |
+LL |       let n = 1i8 << 8;
+   |               ^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:37:15
+   |
+LL |       let n = 1i16 << 16;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:39:15
+   |
+LL |       let n = 1i32 << 32;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:41:15
+   |
+LL |       let n = 1i64 << 64;
+   |               ^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:44:15
+   |
+LL |       let n = 1u8 >> 8;
+   |               ^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:46:15
+   |
+LL |       let n = 1u16 >> 16;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:48:15
+   |
+LL |       let n = 1u32 >> 32;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:50:15
+   |
+LL |       let n = 1u64 >> 64;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:52:15
+   |
+LL |       let n = 1i8 >> 8;
+   |               ^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:54:15
+   |
+LL |       let n = 1i16 >> 16;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:56:15
+   |
+LL |       let n = 1i32 >> 32;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:58:15
+   |
+LL |       let n = 1i64 >> 64;
+   |               ^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:62:15
+   |
+LL |       let n = n << 8;
+   |               ^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:64:15
+   |
+LL |       let n = 1u8 << -8;
+   |               ^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:69:15
+   |
+LL |       let n = 1u8 << (4+4);
+   |               ^^^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:71:15
+   |
+LL |       let n = 1i64 >> [64][0];
+   |               ^^^^^^^^^^^^^^^ attempt to shift right with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:77:15
+   |
+LL |       let n = 1_isize << BITS;
+   |               ^^^^^^^^^^^^^^^ attempt to shift left with overflow
+
+error: this arithmetic operation will overflow
+  --> $DIR/lint-exceeding-bitshifts.rs:78:15
+   |
+LL |       let n = 1_usize << BITS;
+   |               ^^^^^^^^^^^^^^^ attempt to shift left with overflow
+
+error: aborting due to 23 previous errors
+
index 121e5b796bbb714015cba9f426caac51b4602764..3c6d70e7a6ffac7df8b2e063d0b189235b4faa76 100644 (file)
@@ -1,50 +1,79 @@
+// revisions: default noopt opt opt_with_overflow_checks
+//[noopt]compile-flags: -C opt-level=0
+//[opt]compile-flags: -O
+//[opt_with_overflow_checks]compile-flags: -C overflow-checks=on -O
+
 // build-fail
-// compile-flags: -O
 
+#![crate_type="lib"]
 #![deny(exceeding_bitshifts, const_err)]
 #![allow(unused_variables)]
 #![allow(dead_code)]
 
-fn main() {
+pub trait Foo {
+    const N: i32;
+}
+
+impl<T: Foo> Foo for Vec<T> {
+    const N: i32 = T::N << 42; // FIXME this should warn
+}
+
+pub fn foo(x: i32) {
+    let _ = x << 42; //~ ERROR: arithmetic operation will overflow
+}
+
+pub fn main() {
       let n = 1u8 << 7;
-      let n = 1u8 << 8;   //~ ERROR: attempt to shift left with overflow
+      let n = 1u8 << 8;   //~ ERROR: arithmetic operation will overflow
       let n = 1u16 << 15;
-      let n = 1u16 << 16; //~ ERROR: attempt to shift left with overflow
+      let n = 1u16 << 16; //~ ERROR: arithmetic operation will overflow
       let n = 1u32 << 31;
-      let n = 1u32 << 32; //~ ERROR: attempt to shift left with overflow
+      let n = 1u32 << 32; //~ ERROR: arithmetic operation will overflow
       let n = 1u64 << 63;
-      let n = 1u64 << 64; //~ ERROR: attempt to shift left with overflow
+      let n = 1u64 << 64; //~ ERROR: arithmetic operation will overflow
       let n = 1i8 << 7;
-      let n = 1i8 << 8;   //~ ERROR: attempt to shift left with overflow
+      let n = 1i8 << 8;   //~ ERROR: arithmetic operation will overflow
       let n = 1i16 << 15;
-      let n = 1i16 << 16; //~ ERROR: attempt to shift left with overflow
+      let n = 1i16 << 16; //~ ERROR: arithmetic operation will overflow
       let n = 1i32 << 31;
-      let n = 1i32 << 32; //~ ERROR: attempt to shift left with overflow
+      let n = 1i32 << 32; //~ ERROR: arithmetic operation will overflow
       let n = 1i64 << 63;
-      let n = 1i64 << 64; //~ ERROR: attempt to shift left with overflow
+      let n = 1i64 << 64; //~ ERROR: arithmetic operation will overflow
 
       let n = 1u8 >> 7;
-      let n = 1u8 >> 8;   //~ ERROR: attempt to shift right with overflow
+      let n = 1u8 >> 8;   //~ ERROR: arithmetic operation will overflow
       let n = 1u16 >> 15;
-      let n = 1u16 >> 16; //~ ERROR: attempt to shift right with overflow
+      let n = 1u16 >> 16; //~ ERROR: arithmetic operation will overflow
       let n = 1u32 >> 31;
-      let n = 1u32 >> 32; //~ ERROR: attempt to shift right with overflow
+      let n = 1u32 >> 32; //~ ERROR: arithmetic operation will overflow
       let n = 1u64 >> 63;
-      let n = 1u64 >> 64; //~ ERROR: attempt to shift right with overflow
+      let n = 1u64 >> 64; //~ ERROR: arithmetic operation will overflow
       let n = 1i8 >> 7;
-      let n = 1i8 >> 8;   //~ ERROR: attempt to shift right with overflow
+      let n = 1i8 >> 8;   //~ ERROR: arithmetic operation will overflow
       let n = 1i16 >> 15;
-      let n = 1i16 >> 16; //~ ERROR: attempt to shift right with overflow
+      let n = 1i16 >> 16; //~ ERROR: arithmetic operation will overflow
       let n = 1i32 >> 31;
-      let n = 1i32 >> 32; //~ ERROR: attempt to shift right with overflow
+      let n = 1i32 >> 32; //~ ERROR: arithmetic operation will overflow
       let n = 1i64 >> 63;
-      let n = 1i64 >> 64; //~ ERROR: attempt to shift right with overflow
+      let n = 1i64 >> 64; //~ ERROR: arithmetic operation will overflow
 
       let n = 1u8;
       let n = n << 7;
-      let n = n << 8; //~ ERROR: attempt to shift left with overflow
+      let n = n << 8; //~ ERROR: arithmetic operation will overflow
 
-      let n = 1u8 << -8; //~ ERROR: attempt to shift left with overflow
+      let n = 1u8 << -8; //~ ERROR: arithmetic operation will overflow
 
       let n = 1i8<<(1isize+-1);
+
+      let n = 1u8 << (4+3);
+      let n = 1u8 << (4+4); //~ ERROR: arithmetic operation will overflow
+      let n = 1i64 >> [63][0];
+      let n = 1i64 >> [64][0]; //~ ERROR: arithmetic operation will overflow
+
+      #[cfg(target_pointer_width = "32")]
+      const BITS: usize = 32;
+      #[cfg(target_pointer_width = "64")]
+      const BITS: usize = 64;
+      let n = 1_isize << BITS; //~ ERROR: arithmetic operation will overflow
+      let n = 1_usize << BITS; //~ ERROR: arithmetic operation will overflow
 }
diff --git a/src/test/ui/lint/lint-exceeding-bitshifts.stderr b/src/test/ui/lint/lint-exceeding-bitshifts.stderr
deleted file mode 100644 (file)
index 6585772..0000000
+++ /dev/null
@@ -1,116 +0,0 @@
-error: attempt to shift left with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:10:15
-   |
-LL |       let n = 1u8 << 8;
-   |               ^^^^^^^^
-   |
-note: the lint level is defined here
-  --> $DIR/lint-exceeding-bitshifts.rs:4:9
-   |
-LL | #![deny(exceeding_bitshifts, const_err)]
-   |         ^^^^^^^^^^^^^^^^^^^
-
-error: attempt to shift left with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:12:15
-   |
-LL |       let n = 1u16 << 16;
-   |               ^^^^^^^^^^
-
-error: attempt to shift left with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:14:15
-   |
-LL |       let n = 1u32 << 32;
-   |               ^^^^^^^^^^
-
-error: attempt to shift left with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:16:15
-   |
-LL |       let n = 1u64 << 64;
-   |               ^^^^^^^^^^
-
-error: attempt to shift left with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:18:15
-   |
-LL |       let n = 1i8 << 8;
-   |               ^^^^^^^^
-
-error: attempt to shift left with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:20:15
-   |
-LL |       let n = 1i16 << 16;
-   |               ^^^^^^^^^^
-
-error: attempt to shift left with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:22:15
-   |
-LL |       let n = 1i32 << 32;
-   |               ^^^^^^^^^^
-
-error: attempt to shift left with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:24:15
-   |
-LL |       let n = 1i64 << 64;
-   |               ^^^^^^^^^^
-
-error: attempt to shift right with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:27:15
-   |
-LL |       let n = 1u8 >> 8;
-   |               ^^^^^^^^
-
-error: attempt to shift right with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:29:15
-   |
-LL |       let n = 1u16 >> 16;
-   |               ^^^^^^^^^^
-
-error: attempt to shift right with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:31:15
-   |
-LL |       let n = 1u32 >> 32;
-   |               ^^^^^^^^^^
-
-error: attempt to shift right with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:33:15
-   |
-LL |       let n = 1u64 >> 64;
-   |               ^^^^^^^^^^
-
-error: attempt to shift right with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:35:15
-   |
-LL |       let n = 1i8 >> 8;
-   |               ^^^^^^^^
-
-error: attempt to shift right with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:37:15
-   |
-LL |       let n = 1i16 >> 16;
-   |               ^^^^^^^^^^
-
-error: attempt to shift right with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:39:15
-   |
-LL |       let n = 1i32 >> 32;
-   |               ^^^^^^^^^^
-
-error: attempt to shift right with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:41:15
-   |
-LL |       let n = 1i64 >> 64;
-   |               ^^^^^^^^^^
-
-error: attempt to shift left with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:45:15
-   |
-LL |       let n = n << 8;
-   |               ^^^^^^
-
-error: attempt to shift left with overflow
-  --> $DIR/lint-exceeding-bitshifts.rs:47:15
-   |
-LL |       let n = 1u8 << -8;
-   |               ^^^^^^^^^
-
-error: aborting due to 18 previous errors
-
diff --git a/src/test/ui/lint/lint-exceeding-bitshifts2.rs b/src/test/ui/lint/lint-exceeding-bitshifts2.rs
deleted file mode 100644 (file)
index 2a7cbc1..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-// build-fail
-// compile-flags: -O
-
-#![deny(exceeding_bitshifts, const_err)]
-#![allow(unused_variables)]
-#![allow(dead_code)]
-
-fn main() {
-      let n = 1u8 << (4+3);
-      let n = 1u8 << (4+4); //~ ERROR: attempt to shift left with overflow
-      let n = 1i64 >> [63][0];
-      let n = 1i64 >> [64][0]; //~ ERROR: attempt to shift right with overflow
-
-      #[cfg(target_pointer_width = "32")]
-      const BITS: usize = 32;
-      #[cfg(target_pointer_width = "64")]
-      const BITS: usize = 64;
-      let n = 1_isize << BITS; //~ ERROR: attempt to shift left with overflow
-      let n = 1_usize << BITS; //~ ERROR: attempt to shift left with overflow
-}
diff --git a/src/test/ui/lint/lint-exceeding-bitshifts2.stderr b/src/test/ui/lint/lint-exceeding-bitshifts2.stderr
deleted file mode 100644 (file)
index ac9f3b1..0000000
+++ /dev/null
@@ -1,32 +0,0 @@
-error: attempt to shift left with overflow
-  --> $DIR/lint-exceeding-bitshifts2.rs:10:15
-   |
-LL |       let n = 1u8 << (4+4);
-   |               ^^^^^^^^^^^^
-   |
-note: the lint level is defined here
-  --> $DIR/lint-exceeding-bitshifts2.rs:4:9
-   |
-LL | #![deny(exceeding_bitshifts, const_err)]
-   |         ^^^^^^^^^^^^^^^^^^^
-
-error: attempt to shift right with overflow
-  --> $DIR/lint-exceeding-bitshifts2.rs:12:15
-   |
-LL |       let n = 1i64 >> [64][0];
-   |               ^^^^^^^^^^^^^^^
-
-error: attempt to shift left with overflow
-  --> $DIR/lint-exceeding-bitshifts2.rs:18:15
-   |
-LL |       let n = 1_isize << BITS;
-   |               ^^^^^^^^^^^^^^^
-
-error: attempt to shift left with overflow
-  --> $DIR/lint-exceeding-bitshifts2.rs:19:15
-   |
-LL |       let n = 1_usize << BITS;
-   |               ^^^^^^^^^^^^^^^
-
-error: aborting due to 4 previous errors
-