]> git.lizzy.rs Git - rust.git/blob - src/test/ui/lint/lint-exceeding-bitshifts.rs
Rollup merge of #72548 - rossmacarthur:add-mcve-for-50687, r=nikomatsakis
[rust.git] / src / test / ui / lint / lint-exceeding-bitshifts.rs
1 // revisions: noopt opt opt_with_overflow_checks
2 //[noopt]compile-flags: -C opt-level=0
3 //[opt]compile-flags: -O
4 //[opt_with_overflow_checks]compile-flags: -C overflow-checks=on -O
5 // build-pass
6 // ignore-pass (test emits codegen-time warnings and verifies that they are not errors)
7
8 #![crate_type="lib"]
9 #![warn(arithmetic_overflow, const_err)]
10
11
12 pub trait Foo {
13     const N: i32;
14 }
15
16 impl<T: Foo> Foo for Vec<T> {
17     const N: i32 = T::N << 42; //~ WARN: arithmetic operation will overflow
18 }
19
20 pub fn foo(x: i32) {
21     let _ = x << 42; //~ WARN: arithmetic operation will overflow
22 }
23
24 pub fn main() {
25       let n = 1u8 << 7;
26       let n = 1u8 << 8;   //~ WARN: arithmetic operation will overflow
27       let n = 1u16 << 15;
28       let n = 1u16 << 16; //~ WARN: arithmetic operation will overflow
29       let n = 1u32 << 31;
30       let n = 1u32 << 32; //~ WARN: arithmetic operation will overflow
31       let n = 1u64 << 63;
32       let n = 1u64 << 64; //~ WARN: arithmetic operation will overflow
33       let n = 1i8 << 7;
34       let n = 1i8 << 8;   //~ WARN: arithmetic operation will overflow
35       let n = 1i16 << 15;
36       let n = 1i16 << 16; //~ WARN: arithmetic operation will overflow
37       let n = 1i32 << 31;
38       let n = 1i32 << 32; //~ WARN: arithmetic operation will overflow
39       let n = 1i64 << 63;
40       let n = 1i64 << 64; //~ WARN: arithmetic operation will overflow
41
42       let n = 1u8 >> 7;
43       let n = 1u8 >> 8;   //~ WARN: arithmetic operation will overflow
44       let n = 1u16 >> 15;
45       let n = 1u16 >> 16; //~ WARN: arithmetic operation will overflow
46       let n = 1u32 >> 31;
47       let n = 1u32 >> 32; //~ WARN: arithmetic operation will overflow
48       let n = 1u64 >> 63;
49       let n = 1u64 >> 64; //~ WARN: arithmetic operation will overflow
50       let n = 1i8 >> 7;
51       let n = 1i8 >> 8;   //~ WARN: arithmetic operation will overflow
52       let n = 1i16 >> 15;
53       let n = 1i16 >> 16; //~ WARN: arithmetic operation will overflow
54       let n = 1i32 >> 31;
55       let n = 1i32 >> 32; //~ WARN: arithmetic operation will overflow
56       let n = 1i64 >> 63;
57       let n = 1i64 >> 64; //~ WARN: arithmetic operation will overflow
58
59       let n = 1u8;
60       let n = n << 7;
61       let n = n << 8; //~ WARN: arithmetic operation will overflow
62
63       let n = 1u8 << -8; //~ WARN: arithmetic operation will overflow
64
65       let n = 1i8<<(1isize+-1);
66
67       let n = 1u8 << (4+3);
68       let n = 1u8 << (4+4); //~ WARN: arithmetic operation will overflow
69       let n = 1i64 >> [63][0];
70       let n = 1i64 >> [64][0]; //~ WARN: arithmetic operation will overflow
71
72       #[cfg(target_pointer_width = "32")]
73       const BITS: usize = 32;
74       #[cfg(target_pointer_width = "64")]
75       const BITS: usize = 64;
76       let n = 1_isize << BITS; //~ WARN: arithmetic operation will overflow
77       let n = 1_usize << BITS; //~ WARN: arithmetic operation will overflow
78 }