1 // build-fail
2 // compile-flags: -C debug-assertions
3
4 // This function is checking that our automatic truncation does not
5 // sidestep the overflow checking.
6
7 #![deny(arithmetic_overflow, const_err)]
8
main()9 fn main() {
10 // this signals overflow when checking is on
11 let x = 1_i8 << 17;
12 //~^ ERROR: this arithmetic operation will overflow
13
14 // ... but when checking is off, the fallback will truncate the
15 // input to its lower three bits (= 1). Note that this is *not*
16 // the behavior of the x86 processor for 8- and 16-bit types,
17 // but it is necessary to avoid undefined behavior from LLVM.
18 //
19 // We check that here, by ensuring the result has only been
20 // shifted by one place; if overflow checking is turned off, then
21 // this assertion will pass (and the compiletest driver will
22 // report that the test did not produce the error expected above).
23 assert_eq!(x, 2_i8);
24 }
25