2020-02-19 04:25:41 -06:00
|
|
|
// revisions: noopt opt opt_with_overflow_checks
|
2020-02-15 04:43:54 -06:00
|
|
|
//[noopt]compile-flags: -C opt-level=0
|
|
|
|
//[opt]compile-flags: -O
|
|
|
|
//[opt_with_overflow_checks]compile-flags: -C overflow-checks=on -O
|
2020-04-22 15:50:31 -05:00
|
|
|
|
|
|
|
// build-fail
|
2019-09-27 19:09:52 -05:00
|
|
|
|
2020-02-15 04:43:54 -06:00
|
|
|
#![crate_type="lib"]
|
2020-04-22 15:50:31 -05:00
|
|
|
#![deny(arithmetic_overflow, const_err)]
|
2014-11-01 03:10:02 -05:00
|
|
|
#![allow(unused_variables)]
|
2018-03-06 05:43:02 -06:00
|
|
|
#![allow(dead_code)]
|
2014-11-01 03:10:02 -05:00
|
|
|
|
2020-02-15 04:43:54 -06:00
|
|
|
pub trait Foo {
|
|
|
|
const N: i32;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Foo> Foo for Vec<T> {
|
2020-04-22 15:50:31 -05:00
|
|
|
const N: i32 = T::N << 42; // FIXME this should warn
|
2020-02-15 04:43:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn foo(x: i32) {
|
2020-04-22 15:50:31 -05:00
|
|
|
let _ = x << 42; //~ ERROR: arithmetic operation will overflow
|
2020-02-15 04:43:54 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn main() {
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1u8 << 7;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1u8 << 8; //~ ERROR: arithmetic operation will overflow
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1u16 << 15;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1u16 << 16; //~ ERROR: arithmetic operation will overflow
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1u32 << 31;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1u32 << 32; //~ ERROR: arithmetic operation will overflow
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1u64 << 63;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1u64 << 64; //~ ERROR: arithmetic operation will overflow
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1i8 << 7;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1i8 << 8; //~ ERROR: arithmetic operation will overflow
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1i16 << 15;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1i16 << 16; //~ ERROR: arithmetic operation will overflow
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1i32 << 31;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1i32 << 32; //~ ERROR: arithmetic operation will overflow
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1i64 << 63;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1i64 << 64; //~ ERROR: arithmetic operation will overflow
|
2014-11-01 03:10:02 -05:00
|
|
|
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1u8 >> 7;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1u8 >> 8; //~ ERROR: arithmetic operation will overflow
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1u16 >> 15;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1u16 >> 16; //~ ERROR: arithmetic operation will overflow
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1u32 >> 31;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1u32 >> 32; //~ ERROR: arithmetic operation will overflow
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1u64 >> 63;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1u64 >> 64; //~ ERROR: arithmetic operation will overflow
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1i8 >> 7;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1i8 >> 8; //~ ERROR: arithmetic operation will overflow
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1i16 >> 15;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1i16 >> 16; //~ ERROR: arithmetic operation will overflow
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1i32 >> 31;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1i32 >> 32; //~ ERROR: arithmetic operation will overflow
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = 1i64 >> 63;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1i64 >> 64; //~ ERROR: arithmetic operation will overflow
|
2014-11-01 03:10:02 -05:00
|
|
|
|
|
|
|
let n = 1u8;
|
2014-11-03 13:08:11 -06:00
|
|
|
let n = n << 7;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = n << 8; //~ ERROR: arithmetic operation will overflow
|
2014-11-01 03:10:02 -05:00
|
|
|
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1u8 << -8; //~ ERROR: arithmetic operation will overflow
|
2018-01-16 02:28:27 -06:00
|
|
|
|
2015-06-30 10:53:50 -05:00
|
|
|
let n = 1i8<<(1isize+-1);
|
2020-02-15 04:43:54 -06:00
|
|
|
|
|
|
|
let n = 1u8 << (4+3);
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1u8 << (4+4); //~ ERROR: arithmetic operation will overflow
|
2020-02-15 04:43:54 -06:00
|
|
|
let n = 1i64 >> [63][0];
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1i64 >> [64][0]; //~ ERROR: arithmetic operation will overflow
|
2020-02-15 04:43:54 -06:00
|
|
|
|
|
|
|
#[cfg(target_pointer_width = "32")]
|
|
|
|
const BITS: usize = 32;
|
|
|
|
#[cfg(target_pointer_width = "64")]
|
|
|
|
const BITS: usize = 64;
|
2020-04-22 15:50:31 -05:00
|
|
|
let n = 1_isize << BITS; //~ ERROR: arithmetic operation will overflow
|
|
|
|
let n = 1_usize << BITS; //~ ERROR: arithmetic operation will overflow
|
2014-11-01 03:10:02 -05:00
|
|
|
}
|