From: Ralf Jung Date: Sat, 15 Feb 2020 10:43:54 +0000 (+0100) Subject: fix exceeding_bitshift lint and test X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=2107e73d2f0605e916d7b17b1cc0dbb5a1353765;p=rust.git fix exceeding_bitshift lint and test --- diff --git a/src/librustc_mir/transform/const_prop.rs b/src/librustc_mir/transform/const_prop.rs index 53acf1490bf..05e70ed217a 100644 --- a/src/librustc_mir/transform/const_prop.rs +++ b/src/librustc_mir/transform/const_prop.rs @@ -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 index 00000000000..5e32466c4e0 --- /dev/null +++ b/src/test/ui/lint/lint-exceeding-bitshifts.default.stderr @@ -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 index 00000000000..5e32466c4e0 --- /dev/null +++ b/src/test/ui/lint/lint-exceeding-bitshifts.noopt.stderr @@ -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 index 00000000000..5e32466c4e0 --- /dev/null +++ b/src/test/ui/lint/lint-exceeding-bitshifts.opt.stderr @@ -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 index 00000000000..5e32466c4e0 --- /dev/null +++ b/src/test/ui/lint/lint-exceeding-bitshifts.opt_with_overflow_checks.stderr @@ -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.rs b/src/test/ui/lint/lint-exceeding-bitshifts.rs index 121e5b796bb..3c6d70e7a6f 100644 --- a/src/test/ui/lint/lint-exceeding-bitshifts.rs +++ b/src/test/ui/lint/lint-exceeding-bitshifts.rs @@ -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 Foo for Vec { + 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 index 658577213b3..00000000000 --- a/src/test/ui/lint/lint-exceeding-bitshifts.stderr +++ /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 index 2a7cbc10f77..00000000000 --- a/src/test/ui/lint/lint-exceeding-bitshifts2.rs +++ /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 index ac9f3b1e56b..00000000000 --- a/src/test/ui/lint/lint-exceeding-bitshifts2.stderr +++ /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 -