|
| 1 | +// Tests that overflowing or bound-exceeding operations |
| 2 | +// for compile-time consts raise errors |
| 3 | + |
| 4 | +//@ revisions: noopt opt opt_with_overflow_checks |
| 5 | +//@ [noopt]compile-flags: -C opt-level=0 |
| 6 | +//@ [opt]compile-flags: -O |
| 7 | +//@ [opt_with_overflow_checks]compile-flags: -C overflow-checks=on -O |
| 8 | +//@ ignore-pass (test tests codegen-time behaviour) |
| 9 | +//@ normalize-stderr-test "shift left by `(64|32)_usize`, which" -> "shift left by `%BITS%`, which" |
| 10 | +//@ normalize-stderr-test "shift right by `(64|32)_usize`, which" -> "shift right by `%BITS%`, which" |
| 11 | + |
| 12 | + |
| 13 | +#[cfg(target_pointer_width = "32")] |
| 14 | +const BITS: usize = 32; |
| 15 | +#[cfg(target_pointer_width = "64")] |
| 16 | +const BITS: usize = 64; |
| 17 | + |
| 18 | +// Shift left |
| 19 | +const _NI8_SHL: i8 = 1i8 << 8; //~ ERROR: evaluation of constant value failed |
| 20 | +const _NI8_SHL_P: &i8 = &(1i8 << 8); //~ ERROR: evaluation of constant value failed |
| 21 | + |
| 22 | +const _NI16_SHL: i16 = 1i16 << 16; //~ ERROR: evaluation of constant value failed |
| 23 | +const _NI16_SHL_P: &i16 = &(1i16 << 16); //~ ERROR: evaluation of constant value failed |
| 24 | + |
| 25 | +const _NI32_SHL: i32 = 1i32 << 32; //~ ERROR: evaluation of constant value failed |
| 26 | +const _NI32_SHL_P: &i32 = &(1i32 << 32); //~ ERROR: evaluation of constant value failed |
| 27 | + |
| 28 | +const _NI64_SHL: i64 = 1i64 << 64; //~ ERROR: evaluation of constant value failed |
| 29 | +const _NI64_SHL_P: &i64 = &(1i64 << 64); //~ ERROR: evaluation of constant value failed |
| 30 | + |
| 31 | +const _NI128_SHL: i128 = 1i128 << 128; //~ ERROR: evaluation of constant value failed |
| 32 | +const _NI128_SHL_P: &i128 = &(1i128 << 128); //~ ERROR: evaluation of constant value failed |
| 33 | + |
| 34 | +const _NU8_SHL: u8 = 1u8 << 8; //~ ERROR: evaluation of constant value failed |
| 35 | +const _NU8_SHL_P: &u8 = &(1u8 << 8); //~ ERROR: evaluation of constant value failed |
| 36 | + |
| 37 | +const _NU16_SHL: u16 = 1u16 << 16; //~ ERROR: evaluation of constant value failed |
| 38 | +const _NU16_SHL_P: &u16 = &(1u16 << 16); //~ ERROR: evaluation of constant value failed |
| 39 | + |
| 40 | +const _NU32_SHL: u32 = 1u32 << 32; //~ ERROR: evaluation of constant value failed |
| 41 | +const _NU32_SHL_P: &u32 = &(1u32 << 32); //~ ERROR: evaluation of constant value failed |
| 42 | + |
| 43 | +const _NU64_SHL: u64 = 1u64 << 64; //~ ERROR: evaluation of constant value failed |
| 44 | +const _NU64_SHL_P: &u64 = &(1u64 << 64); //~ ERROR: evaluation of constant value failed |
| 45 | + |
| 46 | +const _NU128_SHL: u128 = 1u128 << 128; //~ ERROR: evaluation of constant value failed |
| 47 | +const _NU128_SHL_P: &u128 = &(1u128 << 128); //~ ERROR: evaluation of constant value failed |
| 48 | + |
| 49 | +const _NISIZE_SHL: isize = 1isize << BITS; //~ ERROR: evaluation of constant value failed |
| 50 | +const _NISIZE_SHL_P: &isize = &(1isize << BITS); //~ ERROR: evaluation of constant value failed |
| 51 | + |
| 52 | +const _NUSIZE_SHL: usize = 1usize << BITS; //~ ERROR: evaluation of constant value failed |
| 53 | +const _NUSIZE_SHL_P: &usize = &(1usize << BITS); //~ ERROR: evaluation of constant value failed |
| 54 | + |
| 55 | + |
| 56 | +// Shift right |
| 57 | +const _NI8_SHR: i8 = 1i8 >> 8; //~ ERROR: evaluation of constant value failed |
| 58 | +const _NI8_SHR_P: &i8 = &(1i8 >> 8); //~ ERROR: evaluation of constant value failed |
| 59 | + |
| 60 | +const _NI16_SHR: i16 = 1i16 >> 16; //~ ERROR: evaluation of constant value failed |
| 61 | +const _NI16_SHR_P: &i16 = &(1i16 >> 16); //~ ERROR: evaluation of constant value failed |
| 62 | + |
| 63 | +const _NI32_SHR: i32 = 1i32 >> 32; //~ ERROR: evaluation of constant value failed |
| 64 | +const _NI32_SHR_P: &i32 = &(1i32 >> 32); //~ ERROR: evaluation of constant value failed |
| 65 | + |
| 66 | +const _NI64_SHR: i64 = 1i64 >> 64; //~ ERROR: evaluation of constant value failed |
| 67 | +const _NI64_SHR_P: &i64 = &(1i64 >> 64); //~ ERROR: evaluation of constant value failed |
| 68 | + |
| 69 | +const _NI128_SHR: i128 = 1i128 >> 128; //~ ERROR: evaluation of constant value failed |
| 70 | +const _NI128_SHR_P: &i128 = &(1i128 >> 128); //~ ERROR: evaluation of constant value failed |
| 71 | + |
| 72 | +const _NU8_SHR: u8 = 1u8 >> 8; //~ ERROR: evaluation of constant value failed |
| 73 | +const _NU8_SHR_P: &u8 = &(1u8 >> 8); //~ ERROR: evaluation of constant value failed |
| 74 | + |
| 75 | +const _NU16_SHR: u16 = 1u16 >> 16; //~ ERROR: evaluation of constant value failed |
| 76 | +const _NU16_SHR_P: &u16 = &(1u16 >> 16); //~ ERROR: evaluation of constant value failed |
| 77 | + |
| 78 | +const _NU32_SHR: u32 = 1u32 >> 32; //~ ERROR: evaluation of constant value failed |
| 79 | +const _NU32_SHR_P: &u32 = &(1u32 >> 32); //~ ERROR: evaluation of constant value failed |
| 80 | + |
| 81 | +const _NU64_SHR: u64 = 1u64 >> 64; //~ ERROR: evaluation of constant value failed |
| 82 | +const _NU64_SHR_P: &u64 = &(1u64 >> 64); //~ ERROR: evaluation of constant value failed |
| 83 | + |
| 84 | +const _NU128_SHR: u128 = 1u128 >> 128; //~ ERROR: evaluation of constant value failed |
| 85 | +const _NU128_SHR_P: &u128 = &(1u128 >> 128); //~ ERROR: evaluation of constant value failed |
| 86 | + |
| 87 | +const _NISIZE_SHR: isize = 1isize >> BITS; //~ ERROR: evaluation of constant value failed |
| 88 | +const _NISIZE_SHR_P: &isize = &(1isize >> BITS); //~ ERROR: evaluation of constant value failed |
| 89 | + |
| 90 | +const _NUSIZE_SHR: usize = 1usize >> BITS; //~ ERROR: evaluation of constant value failed |
| 91 | +const _NUSIZE_SHR_P: &usize = &(1usize >> BITS); //~ ERROR: evaluation of constant value failed |
| 92 | + |
| 93 | + |
| 94 | +// Addition |
| 95 | +const _NI8_ADD: i8 = 1i8 + i8::MAX; //~ ERROR: evaluation of constant value failed |
| 96 | +const _NI8_ADD_P: &i8 = &(1i8 + i8::MAX); //~ ERROR: evaluation of constant value failed |
| 97 | + |
| 98 | +const _NI16_ADD: i16 = 1i16 + i16::MAX; //~ ERROR: evaluation of constant value failed |
| 99 | +const _NI16_ADD_P: &i16 = &(1i16 + i16::MAX); //~ ERROR: evaluation of constant value failed |
| 100 | + |
| 101 | +const _NI32_ADD: i32 = 1i32 + i32::MAX; //~ ERROR: evaluation of constant value failed |
| 102 | +const _NI32_ADD_P: &i32 = &(1i32 + i32::MAX); //~ ERROR: evaluation of constant value failed |
| 103 | + |
| 104 | +const _NI64_ADD: i64 = 1i64 + i64::MAX; //~ ERROR: evaluation of constant value failed |
| 105 | +const _NI64_ADD_P: &i64 = &(1i64 + i64::MAX); //~ ERROR: evaluation of constant value failed |
| 106 | + |
| 107 | +const _NI128_ADD: i128 = 1i128 + i128::MAX; //~ ERROR: evaluation of constant value failed |
| 108 | +const _NI128_ADD_P: &i128 = &(1i128 + i128::MAX); //~ ERROR: evaluation of constant value failed |
| 109 | + |
| 110 | +const _NU8_ADD: u8 = 1u8 + u8::MAX; //~ ERROR: evaluation of constant value failed |
| 111 | +const _NU8_ADD_P: &u8 = &(1u8 + u8::MAX); //~ ERROR: evaluation of constant value failed |
| 112 | + |
| 113 | +const _NU16_ADD: u16 = 1u16 + u16::MAX; //~ ERROR: evaluation of constant value failed |
| 114 | +const _NU16_ADD_P: &u16 = &(1u16 + u16::MAX); //~ ERROR: evaluation of constant value failed |
| 115 | + |
| 116 | +const _NU32_ADD: u32 = 1u32 + u32::MAX; //~ ERROR: evaluation of constant value failed |
| 117 | +const _NU32_ADD_P: &u32 = &(1u32 + u32::MAX); //~ ERROR: evaluation of constant value failed |
| 118 | + |
| 119 | +const _NU64_ADD: u64 = 1u64 + u64::MAX; //~ ERROR: evaluation of constant value failed |
| 120 | +const _NU64_ADD_P: &u64 = &(1u64 + u64::MAX); //~ ERROR: evaluation of constant value failed |
| 121 | + |
| 122 | +const _NU128_ADD: u128 = 1u128 + u128::MAX; //~ ERROR: evaluation of constant value failed |
| 123 | +const _NU128_ADD_P: &u128 = &(1u128 + u128::MAX); //~ ERROR: evaluation of constant value failed |
| 124 | + |
| 125 | +const _NISIZE_ADD: isize = 1isize + isize::MAX; //~ ERROR: evaluation of constant value failed |
| 126 | +const _NISIZE_ADD_P: &isize = &(1isize + isize::MAX); //~ ERROR: evaluation of constant value failed |
| 127 | + |
| 128 | +const _NUSIZE_ADD: usize = 1usize + usize::MAX; //~ ERROR: evaluation of constant value failed |
| 129 | +const _NUSIZE_ADD_P: &usize = &(1usize + usize::MAX); //~ ERROR: evaluation of constant value failed |
| 130 | + |
| 131 | + |
| 132 | +// Subtraction |
| 133 | +const _NI8_SUB: i8 = -5i8 - i8::MAX; //~ ERROR: evaluation of constant value failed |
| 134 | +const _NI8_SUB_P: &i8 = &(-5i8 - i8::MAX); //~ ERROR: evaluation of constant value failed |
| 135 | + |
| 136 | +const _NI16_SUB: i16 = -5i16 - i16::MAX; //~ ERROR: evaluation of constant value failed |
| 137 | +const _NI16_SUB_P: &i16 = &(-5i16 - i16::MAX); //~ ERROR: evaluation of constant value failed |
| 138 | + |
| 139 | +const _NI32_SUB: i32 = -5i32 - i32::MAX; //~ ERROR: evaluation of constant value failed |
| 140 | +const _NI32_SUB_P: &i32 = &(-5i32 - i32::MAX); //~ ERROR: evaluation of constant value failed |
| 141 | + |
| 142 | +const _NI64_SUB: i64 = -5i64 - i64::MAX; //~ ERROR: evaluation of constant value failed |
| 143 | +const _NI64_SUB_P: &i64 = &(-5i64 - i64::MAX); //~ ERROR: evaluation of constant value failed |
| 144 | + |
| 145 | +const _NI128_SUB: i128 = -5i128 - i128::MAX; //~ ERROR: evaluation of constant value failed |
| 146 | +const _NI128_SUB_P: &i128 = &(-5i128 - i128::MAX); //~ ERROR: evaluation of constant value failed |
| 147 | + |
| 148 | +const _NU8_SUB: u8 = 1u8 - 5; //~ ERROR: evaluation of constant value failed |
| 149 | +const _NU8_SUB_P: &u8 = &(1u8 - 5); //~ ERROR: evaluation of constant value failed |
| 150 | + |
| 151 | +const _NU16_SUB: u16 = 1u16 - 5; //~ ERROR: evaluation of constant value failed |
| 152 | +const _NU16_SUB_P: &u16 = &(1u16 - 5); //~ ERROR: evaluation of constant value failed |
| 153 | + |
| 154 | +const _NU32_SUB: u32 = 1u32 - 5; //~ ERROR: evaluation of constant value failed |
| 155 | +const _NU32_SUB_P: &u32 = &(1u32 - 5); //~ ERROR: evaluation of constant value failed |
| 156 | + |
| 157 | +const _NU64_SUB: u64 = 1u64 - 5; //~ ERROR: evaluation of constant value failed |
| 158 | +const _NU64_SUB_P: &u64 = &(1u64 - 5); //~ ERROR: evaluation of constant value failed |
| 159 | + |
| 160 | +const _NU128_SUB: u128 = 1u128 - 5; //~ ERROR: evaluation of constant value failed |
| 161 | +const _NU128_SUB_P: &u128 = &(1u128 - 5); //~ ERROR: evaluation of constant value failed |
| 162 | + |
| 163 | +const _NISIZE_SUB: isize = -5isize - isize::MAX; //~ ERROR: evaluation of constant value failed |
| 164 | +const _NISIZE_SUB_P: &isize = &(-5isize - isize::MAX); //~ ERROR: evaluation of constant value failed |
| 165 | + |
| 166 | +const _NUSIZE_SUB: usize = 1usize - 5 ; //~ ERROR: evaluation of constant value failed |
| 167 | +const _NUSIZE_SUB_P: &usize = &(1usize - 5 ); //~ ERROR: evaluation of constant value failed |
| 168 | + |
| 169 | + |
| 170 | +// Multiplication |
| 171 | +const _NI8_MUL: i8 = i8::MAX * 5; //~ ERROR: evaluation of constant value failed |
| 172 | +const _NI8_MUL_P: &i8 = &(i8::MAX * 5); //~ ERROR: evaluation of constant value failed |
| 173 | + |
| 174 | +const _NI16_MUL: i16 = i16::MAX * 5; //~ ERROR: evaluation of constant value failed |
| 175 | +const _NI16_MUL_P: &i16 = &(i16::MAX * 5); //~ ERROR: evaluation of constant value failed |
| 176 | + |
| 177 | +const _NI32_MUL: i32 = i32::MAX * 5; //~ ERROR: evaluation of constant value failed |
| 178 | +const _NI32_MUL_P: &i32 = &(i32::MAX * 5); //~ ERROR: evaluation of constant value failed |
| 179 | + |
| 180 | +const _NI64_MUL: i64 = i64::MAX * 5; //~ ERROR: evaluation of constant value failed |
| 181 | +const _NI64_MUL_P: &i64 = &(i64::MAX * 5); //~ ERROR: evaluation of constant value failed |
| 182 | + |
| 183 | +const _NI128_MUL: i128 = i128::MAX * 5; //~ ERROR: evaluation of constant value failed |
| 184 | +const _NI128_MUL_P: &i128 = &(i128::MAX * 5); //~ ERROR: evaluation of constant value failed |
| 185 | + |
| 186 | +const _NU8_MUL: u8 = u8::MAX * 5; //~ ERROR: evaluation of constant value failed |
| 187 | +const _NU8_MUL_P: &u8 = &(u8::MAX * 5); //~ ERROR: evaluation of constant value failed |
| 188 | + |
| 189 | +const _NU16_MUL: u16 = u16::MAX * 5; //~ ERROR: evaluation of constant value failed |
| 190 | +const _NU16_MUL_P: &u16 = &(u16::MAX * 5); //~ ERROR: evaluation of constant value failed |
| 191 | + |
| 192 | +const _NU32_MUL: u32 = u32::MAX * 5; //~ ERROR: evaluation of constant value failed |
| 193 | +const _NU32_MUL_P: &u32 = &(u32::MAX * 5); //~ ERROR: evaluation of constant value failed |
| 194 | + |
| 195 | +const _NU64_MUL: u64 = u64::MAX * 5; //~ ERROR: evaluation of constant value failed |
| 196 | +const _NU64_MUL_P: &u64 = &(u64::MAX * 5); //~ ERROR: evaluation of constant value failed |
| 197 | + |
| 198 | +const _NU128_MUL: u128 = u128::MAX * 5; //~ ERROR: evaluation of constant value failed |
| 199 | +const _NU128_MUL_P: &u128 = &(u128::MAX * 5); //~ ERROR: evaluation of constant value failed |
| 200 | + |
| 201 | +const _NISIZE_MUL: isize = isize::MAX * 5; //~ ERROR: evaluation of constant value failed |
| 202 | +const _NISIZE_MUL_P: &isize = &(isize::MAX * 5); //~ ERROR: evaluation of constant value failed |
| 203 | + |
| 204 | +const _NUSIZE_MUL: usize = usize::MAX * 5; //~ ERROR: evaluation of constant value failed |
| 205 | +const _NUSIZE_MUL_P: &usize = &(usize::MAX * 5); //~ ERROR: evaluation of constant value failed |
| 206 | + |
| 207 | + |
| 208 | +// Division |
| 209 | +const _NI8_DIV: i8 = 1i8 / 0; //~ ERROR: evaluation of constant value failed |
| 210 | +const _NI8_DIV_P: &i8 = &(1i8 / 0); //~ ERROR: evaluation of constant value failed |
| 211 | + |
| 212 | +const _NI16_DIV: i16 = 1i16 / 0; //~ ERROR: evaluation of constant value failed |
| 213 | +const _NI16_DIV_P: &i16 = &(1i16 / 0); //~ ERROR: evaluation of constant value failed |
| 214 | + |
| 215 | +const _NI32_DIV: i32 = 1i32 / 0; //~ ERROR: evaluation of constant value failed |
| 216 | +const _NI32_DIV_P: &i32 = &(1i32 / 0); //~ ERROR: evaluation of constant value failed |
| 217 | + |
| 218 | +const _NI64_DIV: i64 = 1i64 / 0; //~ ERROR: evaluation of constant value failed |
| 219 | +const _NI64_DIV_P: &i64 = &(1i64 / 0); //~ ERROR: evaluation of constant value failed |
| 220 | + |
| 221 | +const _NI128_DIV: i128 = 1i128 / 0; //~ ERROR: evaluation of constant value failed |
| 222 | +const _NI128_DIV_P: &i128 = &(1i128 / 0); //~ ERROR: evaluation of constant value failed |
| 223 | + |
| 224 | +const _NU8_DIV: u8 = 1u8 / 0; //~ ERROR: evaluation of constant value failed |
| 225 | +const _NU8_DIV_P: &u8 = &(1u8 / 0); //~ ERROR: evaluation of constant value failed |
| 226 | + |
| 227 | +const _NU16_DIV: u16 = 1u16 / 0; //~ ERROR: evaluation of constant value failed |
| 228 | +const _NU16_DIV_P: &u16 = &(1u16 / 0); //~ ERROR: evaluation of constant value failed |
| 229 | + |
| 230 | +const _NU32_DIV: u32 = 1u32 / 0; //~ ERROR: evaluation of constant value failed |
| 231 | +const _NU32_DIV_P: &u32 = &(1u32 / 0); //~ ERROR: evaluation of constant value failed |
| 232 | + |
| 233 | +const _NU64_DIV: u64 = 1u64 / 0; //~ ERROR: evaluation of constant value failed |
| 234 | +const _NU64_DIV_P: &u64 = &(1u64 / 0); //~ ERROR: evaluation of constant value failed |
| 235 | + |
| 236 | +const _NU128_DIV: u128 = 1u128 / 0; //~ ERROR: evaluation of constant value failed |
| 237 | +const _NU128_DIV_P: &u128 = &(1u128 / 0); //~ ERROR: evaluation of constant value failed |
| 238 | + |
| 239 | +const _NISIZE_DIV: isize = 1isize / 0; //~ ERROR: evaluation of constant value failed |
| 240 | +const _NISIZE_DIV_P: &isize = &(1isize / 0); //~ ERROR: evaluation of constant value failed |
| 241 | + |
| 242 | +const _NUSIZE_DIV: usize = 1usize / 0; //~ ERROR: evaluation of constant value failed |
| 243 | +const _NUSIZE_DIV_P: &usize = &(1usize / 0); //~ ERROR: evaluation of constant value failed |
| 244 | + |
| 245 | +// Modulus |
| 246 | +const _NI8_MOD: i8 = 1i8 % 0; //~ ERROR: evaluation of constant value failed |
| 247 | +const _NI8_MOD_P: &i8 = &(1i8 % 0); //~ ERROR: evaluation of constant value failed |
| 248 | + |
| 249 | +const _NI16_MOD: i16 = 1i16 % 0; //~ ERROR: evaluation of constant value failed |
| 250 | +const _NI16_MOD_P: &i16 = &(1i16 % 0); //~ ERROR: evaluation of constant value failed |
| 251 | + |
| 252 | +const _NI32_MOD: i32 = 1i32 % 0; //~ ERROR: evaluation of constant value failed |
| 253 | +const _NI32_MOD_P: &i32 = &(1i32 % 0); //~ ERROR: evaluation of constant value failed |
| 254 | + |
| 255 | +const _NI64_MOD: i64 = 1i64 % 0; //~ ERROR: evaluation of constant value failed |
| 256 | +const _NI64_MOD_P: &i64 = &(1i64 % 0); //~ ERROR: evaluation of constant value failed |
| 257 | + |
| 258 | +const _NI128_MOD: i128 = 1i128 % 0; //~ ERROR: evaluation of constant value failed |
| 259 | +const _NI128_MOD_P: &i128 = &(1i128 % 0); //~ ERROR: evaluation of constant value failed |
| 260 | + |
| 261 | +const _NU8_MOD: u8 = 1u8 % 0; //~ ERROR: evaluation of constant value failed |
| 262 | +const _NU8_MOD_P: &u8 = &(1u8 % 0); //~ ERROR: evaluation of constant value failed |
| 263 | + |
| 264 | +const _NU16_MOD: u16 = 1u16 % 0; //~ ERROR: evaluation of constant value failed |
| 265 | +const _NU16_MOD_P: &u16 = &(1u16 % 0); //~ ERROR: evaluation of constant value failed |
| 266 | + |
| 267 | +const _NU32_MOD: u32 = 1u32 % 0; //~ ERROR: evaluation of constant value failed |
| 268 | +const _NU32_MOD_P: &u32 = &(1u32 % 0); //~ ERROR: evaluation of constant value failed |
| 269 | + |
| 270 | +const _NU64_MOD: u64 = 1u64 % 0; //~ ERROR: evaluation of constant value failed |
| 271 | +const _NU64_MOD_P: &u64 = &(1u64 % 0); //~ ERROR: evaluation of constant value failed |
| 272 | + |
| 273 | +const _NU128_MOD: u128 = 1u128 % 0; //~ ERROR: evaluation of constant value failed |
| 274 | +const _NU128_MOD_P: &u128 = &(1u128 % 0); //~ ERROR: evaluation of constant value failed |
| 275 | + |
| 276 | +const _NISIZE_MOD: isize = 1isize % 0; //~ ERROR: evaluation of constant value failed |
| 277 | +const _NISIZE_MOD_P: &isize = &(1isize % 0); //~ ERROR: evaluation of constant value failed |
| 278 | + |
| 279 | +const _NUSIZE_MOD: usize = 1usize % 0; //~ ERROR: evaluation of constant value failed |
| 280 | +const _NUSIZE_MOD_P: &usize = &(1usize % 0); //~ ERROR: evaluation of constant value failed |
| 281 | + |
| 282 | + |
| 283 | +// Out of bounds access |
| 284 | +const _NI32_OOB: i32 = [1, 2, 3][4]; //~ ERROR: evaluation of constant value failed |
| 285 | +const _NI32_OOB_P: &i32 = &([1, 2, 3][4]); //~ ERROR: evaluation of constant value failed |
| 286 | + |
| 287 | + |
| 288 | +pub fn main() {} |
0 commit comments