diff --git a/src/test/ui/consts/const-int-unchecked.rs b/src/test/ui/consts/const-int-unchecked.rs index aeac6c37dcb..8ee029b6cc3 100644 --- a/src/test/ui/consts/const-int-unchecked.rs +++ b/src/test/ui/consts/const-int-unchecked.rs @@ -2,10 +2,119 @@ use std::intrinsics; -const SHR: u8 = unsafe { intrinsics::unchecked_shr(5_u8, 8) }; +// The documentation of `unchecked_shl` states that it: +// +// Performs an unchecked left shift, resulting in undefined behavior when +// y < 0 or y >= N, where N is the width of T in bits. +// +// So we check this for a few `y`. + +// unsigned types: + +const SHL_U8: u8 = unsafe { intrinsics::unchecked_shl(5_u8, 8) }; //~^ ERROR any use of this value will cause an error -const SHL: u8 = unsafe { intrinsics::unchecked_shl(5_u8, 8) }; +const SHL_U16: u16 = unsafe { intrinsics::unchecked_shl(5_u16, 16) }; +//~^ ERROR any use of this value will cause an error +const SHL_U32: u32 = unsafe { intrinsics::unchecked_shl(5_u32, 32) }; +//~^ ERROR any use of this value will cause an error +const SHL_U64: u64 = unsafe { intrinsics::unchecked_shl(5_u64, 64) }; +//~^ ERROR any use of this value will cause an error +const SHL_U128: u128 = unsafe { intrinsics::unchecked_shl(5_u128, 128) }; //~^ ERROR any use of this value will cause an error -fn main() { -} +// signed types: + +const SHL_I8: i8 = unsafe { intrinsics::unchecked_shl(5_i8, 8) }; +//~^ ERROR any use of this value will cause an error +const SHL_I16: i16 = unsafe { intrinsics::unchecked_shl(5_16, 16) }; +//~^ ERROR any use of this value will cause an error +const SHL_I32: i32 = unsafe { intrinsics::unchecked_shl(5_i32, 32) }; +//~^ ERROR any use of this value will cause an error +const SHL_I64: i64 = unsafe { intrinsics::unchecked_shl(5_i64, 64) }; +//~^ ERROR any use of this value will cause an error +const SHL_I128: i128 = unsafe { intrinsics::unchecked_shl(5_i128, 128) }; +//~^ ERROR any use of this value will cause an error + +// and make sure we capture y < 0: + +const SHL_I8_NEG: i8 = unsafe { intrinsics::unchecked_shl(5_i8, -1) }; +//~^ ERROR any use of this value will cause an error +const SHL_I16_NEG: i16 = unsafe { intrinsics::unchecked_shl(5_16, -1) }; +//~^ ERROR any use of this value will cause an error +const SHL_I32_NEG: i32 = unsafe { intrinsics::unchecked_shl(5_i32, -1) }; +//~^ ERROR any use of this value will cause an error +const SHL_I64_NEG: i64 = unsafe { intrinsics::unchecked_shl(5_i64, -1) }; +//~^ ERROR any use of this value will cause an error +const SHL_I128_NEG: i128 = unsafe { intrinsics::unchecked_shl(5_i128, -1) }; +//~^ ERROR any use of this value will cause an error + +// and that there's no special relation to the value -1 by picking some +// negative values at random: + +const SHL_I8_NEG_RANDOM: i8 = unsafe { intrinsics::unchecked_shl(5_i8, -6) }; +//~^ ERROR any use of this value will cause an error +const SHL_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shl(5_16, -13) }; +//~^ ERROR any use of this value will cause an error +const SHL_I32_NEG_RANDOM: i32 = unsafe { intrinsics::unchecked_shl(5_i32, -25) }; +//~^ ERROR any use of this value will cause an error +const SHL_I64_NEG_RANDOM: i64 = unsafe { intrinsics::unchecked_shl(5_i64, -30) }; +//~^ ERROR any use of this value will cause an error +const SHL_I128_NEG_RANDOM: i128 = unsafe { intrinsics::unchecked_shl(5_i128, -93) }; +//~^ ERROR any use of this value will cause an error + +// Repeat it all over for `unchecked_shr` + +// unsigned types: + +const SHR_U8: u8 = unsafe { intrinsics::unchecked_shr(5_u8, 8) }; +//~^ ERROR any use of this value will cause an error +const SHR_U16: u16 = unsafe { intrinsics::unchecked_shr(5_u16, 16) }; +//~^ ERROR any use of this value will cause an error +const SHR_U32: u32 = unsafe { intrinsics::unchecked_shr(5_u32, 32) }; +//~^ ERROR any use of this value will cause an error +const SHR_U64: u64 = unsafe { intrinsics::unchecked_shr(5_u64, 64) }; +//~^ ERROR any use of this value will cause an error +const SHR_U128: u128 = unsafe { intrinsics::unchecked_shr(5_u128, 128) }; +//~^ ERROR any use of this value will cause an error + +// signed types: + +const SHR_I8: i8 = unsafe { intrinsics::unchecked_shr(5_i8, 8) }; +//~^ ERROR any use of this value will cause an error +const SHR_I16: i16 = unsafe { intrinsics::unchecked_shr(5_16, 16) }; +//~^ ERROR any use of this value will cause an error +const SHR_I32: i32 = unsafe { intrinsics::unchecked_shr(5_i32, 32) }; +//~^ ERROR any use of this value will cause an error +const SHR_I64: i64 = unsafe { intrinsics::unchecked_shr(5_i64, 64) }; +//~^ ERROR any use of this value will cause an error +const SHR_I128: i128 = unsafe { intrinsics::unchecked_shr(5_i128, 128) }; +//~^ ERROR any use of this value will cause an error + +// and make sure we capture y < 0: + +const SHR_I8_NEG: i8 = unsafe { intrinsics::unchecked_shr(5_i8, -1) }; +//~^ ERROR any use of this value will cause an error +const SHR_I16_NEG: i16 = unsafe { intrinsics::unchecked_shr(5_16, -1) }; +//~^ ERROR any use of this value will cause an error +const SHR_I32_NEG: i32 = unsafe { intrinsics::unchecked_shr(5_i32, -1) }; +//~^ ERROR any use of this value will cause an error +const SHR_I64_NEG: i64 = unsafe { intrinsics::unchecked_shr(5_i64, -1) }; +//~^ ERROR any use of this value will cause an error +const SHR_I128_NEG: i128 = unsafe { intrinsics::unchecked_shr(5_i128, -1) }; +//~^ ERROR any use of this value will cause an error + +// and that there's no special relation to the value -1 by picking some +// negative values at random: + +const SHR_I8_NEG_RANDOM: i8 = unsafe { intrinsics::unchecked_shr(5_i8, -6) }; +//~^ ERROR any use of this value will cause an error +const SHR_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shr(5_16, -13) }; +//~^ ERROR any use of this value will cause an error +const SHR_I32_NEG_RANDOM: i32 = unsafe { intrinsics::unchecked_shr(5_i32, -25) }; +//~^ ERROR any use of this value will cause an error +const SHR_I64_NEG_RANDOM: i64 = unsafe { intrinsics::unchecked_shr(5_i64, -30) }; +//~^ ERROR any use of this value will cause an error +const SHR_I128_NEG_RANDOM: i128 = unsafe { intrinsics::unchecked_shr(5_i128, -93) }; +//~^ ERROR any use of this value will cause an error + +fn main() {} diff --git a/src/test/ui/consts/const-int-unchecked.stderr b/src/test/ui/consts/const-int-unchecked.stderr index dd28cc4d533..4382d9174b7 100644 --- a/src/test/ui/consts/const-int-unchecked.stderr +++ b/src/test/ui/consts/const-int-unchecked.stderr @@ -1,20 +1,324 @@ error: any use of this value will cause an error - --> $DIR/const-int-unchecked.rs:5:1 + --> $DIR/const-int-unchecked.rs:14:1 | -LL | const SHR: u8 = unsafe { intrinsics::unchecked_shr(5_u8, 8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^ - | | - | Overflowing shift by 8 in unchecked_shr +LL | const SHL_U8: u8 = unsafe { intrinsics::unchecked_shl(5_u8, 8) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^ + | | + | Overflowing shift by 8 in unchecked_shl | = note: #[deny(const_err)] on by default error: any use of this value will cause an error - --> $DIR/const-int-unchecked.rs:7:1 + --> $DIR/const-int-unchecked.rs:16:1 | -LL | const SHL: u8 = unsafe { intrinsics::unchecked_shl(5_u8, 8) }; - | ^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^ - | | - | Overflowing shift by 8 in unchecked_shl +LL | const SHL_U16: u16 = unsafe { intrinsics::unchecked_shl(5_u16, 16) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 16 in unchecked_shl -error: aborting due to 2 previous errors +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:18:1 + | +LL | const SHL_U32: u32 = unsafe { intrinsics::unchecked_shl(5_u32, 32) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 32 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:20:1 + | +LL | const SHL_U64: u64 = unsafe { intrinsics::unchecked_shl(5_u64, 64) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 64 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:22:1 + | +LL | const SHL_U128: u128 = unsafe { intrinsics::unchecked_shl(5_u128, 128) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------------------------------------^^^ + | | + | Overflowing shift by 128 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:27:1 + | +LL | const SHL_I8: i8 = unsafe { intrinsics::unchecked_shl(5_i8, 8) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^ + | | + | Overflowing shift by 8 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:29:1 + | +LL | const SHL_I16: i16 = unsafe { intrinsics::unchecked_shl(5_16, 16) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------^^^ + | | + | Overflowing shift by 16 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:31:1 + | +LL | const SHL_I32: i32 = unsafe { intrinsics::unchecked_shl(5_i32, 32) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 32 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:33:1 + | +LL | const SHL_I64: i64 = unsafe { intrinsics::unchecked_shl(5_i64, 64) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 64 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:35:1 + | +LL | const SHL_I128: i128 = unsafe { intrinsics::unchecked_shl(5_i128, 128) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------------------------------------^^^ + | | + | Overflowing shift by 128 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:40:1 + | +LL | const SHL_I8_NEG: i8 = unsafe { intrinsics::unchecked_shl(5_i8, -1) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------^^^ + | | + | Overflowing shift by 255 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:42:1 + | +LL | const SHL_I16_NEG: i16 = unsafe { intrinsics::unchecked_shl(5_16, -1) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------^^^ + | | + | Overflowing shift by 65535 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:44:1 + | +LL | const SHL_I32_NEG: i32 = unsafe { intrinsics::unchecked_shl(5_i32, -1) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 4294967295 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:46:1 + | +LL | const SHL_I64_NEG: i64 = unsafe { intrinsics::unchecked_shl(5_i64, -1) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 18446744073709551615 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:48:1 + | +LL | const SHL_I128_NEG: i128 = unsafe { intrinsics::unchecked_shl(5_i128, -1) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------------------------^^^ + | | + | Overflowing shift by 340282366920938463463374607431768211455 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:54:1 + | +LL | const SHL_I8_NEG_RANDOM: i8 = unsafe { intrinsics::unchecked_shl(5_i8, -6) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------^^^ + | | + | Overflowing shift by 250 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:56:1 + | +LL | const SHL_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shl(5_16, -13) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 65523 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:58:1 + | +LL | const SHL_I32_NEG_RANDOM: i32 = unsafe { intrinsics::unchecked_shl(5_i32, -25) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------------------------^^^ + | | + | Overflowing shift by 4294967271 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:60:1 + | +LL | const SHL_I64_NEG_RANDOM: i64 = unsafe { intrinsics::unchecked_shl(5_i64, -30) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------------------------^^^ + | | + | Overflowing shift by 18446744073709551586 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:62:1 + | +LL | const SHL_I128_NEG_RANDOM: i128 = unsafe { intrinsics::unchecked_shl(5_i128, -93) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------------------------------------^^^ + | | + | Overflowing shift by 340282366920938463463374607431768211363 in unchecked_shl + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:69:1 + | +LL | const SHR_U8: u8 = unsafe { intrinsics::unchecked_shr(5_u8, 8) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^ + | | + | Overflowing shift by 8 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:71:1 + | +LL | const SHR_U16: u16 = unsafe { intrinsics::unchecked_shr(5_u16, 16) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 16 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:73:1 + | +LL | const SHR_U32: u32 = unsafe { intrinsics::unchecked_shr(5_u32, 32) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 32 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:75:1 + | +LL | const SHR_U64: u64 = unsafe { intrinsics::unchecked_shr(5_u64, 64) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 64 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:77:1 + | +LL | const SHR_U128: u128 = unsafe { intrinsics::unchecked_shr(5_u128, 128) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------------------------------------^^^ + | | + | Overflowing shift by 128 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:82:1 + | +LL | const SHR_I8: i8 = unsafe { intrinsics::unchecked_shr(5_i8, 8) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------------^^^ + | | + | Overflowing shift by 8 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:84:1 + | +LL | const SHR_I16: i16 = unsafe { intrinsics::unchecked_shr(5_16, 16) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------^^^ + | | + | Overflowing shift by 16 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:86:1 + | +LL | const SHR_I32: i32 = unsafe { intrinsics::unchecked_shr(5_i32, 32) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 32 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:88:1 + | +LL | const SHR_I64: i64 = unsafe { intrinsics::unchecked_shr(5_i64, 64) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 64 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:90:1 + | +LL | const SHR_I128: i128 = unsafe { intrinsics::unchecked_shr(5_i128, 128) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------------------------------------^^^ + | | + | Overflowing shift by 128 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:95:1 + | +LL | const SHR_I8_NEG: i8 = unsafe { intrinsics::unchecked_shr(5_i8, -1) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------^^^ + | | + | Overflowing shift by 255 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:97:1 + | +LL | const SHR_I16_NEG: i16 = unsafe { intrinsics::unchecked_shr(5_16, -1) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------^^^ + | | + | Overflowing shift by 65535 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:99:1 + | +LL | const SHR_I32_NEG: i32 = unsafe { intrinsics::unchecked_shr(5_i32, -1) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 4294967295 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:101:1 + | +LL | const SHR_I64_NEG: i64 = unsafe { intrinsics::unchecked_shr(5_i64, -1) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 18446744073709551615 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:103:1 + | +LL | const SHR_I128_NEG: i128 = unsafe { intrinsics::unchecked_shr(5_i128, -1) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------------------------^^^ + | | + | Overflowing shift by 340282366920938463463374607431768211455 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:109:1 + | +LL | const SHR_I8_NEG_RANDOM: i8 = unsafe { intrinsics::unchecked_shr(5_i8, -6) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-----------------------------------^^^ + | | + | Overflowing shift by 250 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:111:1 + | +LL | const SHR_I16_NEG_RANDOM: i16 = unsafe { intrinsics::unchecked_shr(5_16, -13) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^------------------------------------^^^ + | | + | Overflowing shift by 65523 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:113:1 + | +LL | const SHR_I32_NEG_RANDOM: i32 = unsafe { intrinsics::unchecked_shr(5_i32, -25) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------------------------^^^ + | | + | Overflowing shift by 4294967271 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:115:1 + | +LL | const SHR_I64_NEG_RANDOM: i64 = unsafe { intrinsics::unchecked_shr(5_i64, -30) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^-------------------------------------^^^ + | | + | Overflowing shift by 18446744073709551586 in unchecked_shr + +error: any use of this value will cause an error + --> $DIR/const-int-unchecked.rs:117:1 + | +LL | const SHR_I128_NEG_RANDOM: i128 = unsafe { intrinsics::unchecked_shr(5_i128, -93) }; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^--------------------------------------^^^ + | | + | Overflowing shift by 340282366920938463463374607431768211363 in unchecked_shr + +error: aborting due to 40 previous errors