]> git.proxmox.com Git - rustc.git/blame - src/test/ui/lint/lint-exceeding-bitshifts.rs
New upstream version 1.43.0+dfsg1
[rustc.git] / src / test / ui / lint / lint-exceeding-bitshifts.rs
CommitLineData
74b04a01
XL
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
dfeec247 6// build-fail
e74abb32 7
74b04a01
XL
8#![crate_type="lib"]
9#![deny(arithmetic_overflow, const_err)]
1a4d82fc
JJ
10#![allow(unused_variables)]
11#![allow(dead_code)]
12
74b04a01
XL
13pub trait Foo {
14 const N: i32;
15}
16
17impl<T: Foo> Foo for Vec<T> {
18 const N: i32 = T::N << 42; // FIXME this should warn
19}
20
21pub fn foo(x: i32) {
22 let _ = x << 42; //~ ERROR: arithmetic operation will overflow
23}
24
25pub fn main() {
1a4d82fc 26 let n = 1u8 << 7;
74b04a01 27 let n = 1u8 << 8; //~ ERROR: arithmetic operation will overflow
1a4d82fc 28 let n = 1u16 << 15;
74b04a01 29 let n = 1u16 << 16; //~ ERROR: arithmetic operation will overflow
1a4d82fc 30 let n = 1u32 << 31;
74b04a01 31 let n = 1u32 << 32; //~ ERROR: arithmetic operation will overflow
1a4d82fc 32 let n = 1u64 << 63;
74b04a01 33 let n = 1u64 << 64; //~ ERROR: arithmetic operation will overflow
1a4d82fc 34 let n = 1i8 << 7;
74b04a01 35 let n = 1i8 << 8; //~ ERROR: arithmetic operation will overflow
1a4d82fc 36 let n = 1i16 << 15;
74b04a01 37 let n = 1i16 << 16; //~ ERROR: arithmetic operation will overflow
1a4d82fc 38 let n = 1i32 << 31;
74b04a01 39 let n = 1i32 << 32; //~ ERROR: arithmetic operation will overflow
1a4d82fc 40 let n = 1i64 << 63;
74b04a01 41 let n = 1i64 << 64; //~ ERROR: arithmetic operation will overflow
1a4d82fc
JJ
42
43 let n = 1u8 >> 7;
74b04a01 44 let n = 1u8 >> 8; //~ ERROR: arithmetic operation will overflow
1a4d82fc 45 let n = 1u16 >> 15;
74b04a01 46 let n = 1u16 >> 16; //~ ERROR: arithmetic operation will overflow
1a4d82fc 47 let n = 1u32 >> 31;
74b04a01 48 let n = 1u32 >> 32; //~ ERROR: arithmetic operation will overflow
1a4d82fc 49 let n = 1u64 >> 63;
74b04a01 50 let n = 1u64 >> 64; //~ ERROR: arithmetic operation will overflow
1a4d82fc 51 let n = 1i8 >> 7;
74b04a01 52 let n = 1i8 >> 8; //~ ERROR: arithmetic operation will overflow
1a4d82fc 53 let n = 1i16 >> 15;
74b04a01 54 let n = 1i16 >> 16; //~ ERROR: arithmetic operation will overflow
1a4d82fc 55 let n = 1i32 >> 31;
74b04a01 56 let n = 1i32 >> 32; //~ ERROR: arithmetic operation will overflow
1a4d82fc 57 let n = 1i64 >> 63;
74b04a01 58 let n = 1i64 >> 64; //~ ERROR: arithmetic operation will overflow
1a4d82fc
JJ
59
60 let n = 1u8;
61 let n = n << 7;
74b04a01 62 let n = n << 8; //~ ERROR: arithmetic operation will overflow
c1a9b12d 63
74b04a01 64 let n = 1u8 << -8; //~ ERROR: arithmetic operation will overflow
c1a9b12d
SL
65
66 let n = 1i8<<(1isize+-1);
74b04a01
XL
67
68 let n = 1u8 << (4+3);
69 let n = 1u8 << (4+4); //~ ERROR: arithmetic operation will overflow
70 let n = 1i64 >> [63][0];
71 let n = 1i64 >> [64][0]; //~ ERROR: arithmetic operation will overflow
72
73 #[cfg(target_pointer_width = "32")]
74 const BITS: usize = 32;
75 #[cfg(target_pointer_width = "64")]
76 const BITS: usize = 64;
77 let n = 1_isize << BITS; //~ ERROR: arithmetic operation will overflow
78 let n = 1_usize << BITS; //~ ERROR: arithmetic operation will overflow
1a4d82fc 79}