Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit dfdbe30

Browse files
committedFeb 17, 2024
Auto merge of #119432 - gurry:117949-make-lint-run-on-promoteds, r=oli-obk
Make `ConstPropLint` lint run on promoteds Fixes #117949 wherein the lint didn't fire for the following promoteds: - SHL or SHR operators in a non-optimized build - any arithmetic operator in an optimized build What I have done here is simply enabled `ConstPropLint` to run on promoted bodies by removing the relevant `if` check. After this change _all_ promoted arithmetic operators will lint _in both non-optimized and optimized builds_. On the flip side programs containing the above mentioned overflowing promoteds that were accepted earlier will now be rejected. Hope that is okay from a backward compatibility standpoint. I have added tests covering all overflowing promoted & non-promoted ops for both compile-time and runtime operations and for optimized as well as non-optimized builds. I had to amend some existing tests to make them pass and had to delete a couple that were set to pass despite overflows. This PR increases the number of duplicate diagnostics emitted (because the same operator might get linted in both the promoted MIR and the main MIR). I hope that is an acceptable trade-off given that we now lint overflows much more comprehensively than earlier.
2 parents 4316d0c + 5010ca0 commit dfdbe30

29 files changed

+6967
-738
lines changed
 

‎compiler/rustc_mir_transform/src/const_prop_lint.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ impl<'tcx> MirLint<'tcx> for ConstPropLint {
3232
return;
3333
}
3434

35-
// will be evaluated by miri and produce its errors there
36-
if body.source.promoted.is_some() {
37-
return;
38-
}
39-
4035
let def_id = body.source.def_id().expect_local();
4136
let def_kind = tcx.def_kind(def_id);
4237
let is_fn_like = def_kind.is_fn_like();

‎src/tools/miri/tests/pass/overflow_checks_off.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
//@compile-flags: -C overflow-checks=off
22

33
// Check that we correctly implement the intended behavior of these operators
4-
// when they are not being overflow-checked.
4+
// when they are not being overflow-checked at runtime.
55

66
// FIXME: if we call the functions in `std::ops`, we still get the panics.
77
// Miri does not implement the codegen-time hack that backs `#[rustc_inherit_overflow_checks]`.
88
// use std::ops::*;
99

10+
11+
// Disable _compile-time_ overflow linting
12+
// so that we can test runtime overflow checks
13+
#![allow(arithmetic_overflow)]
14+
1015
fn main() {
1116
assert_eq!(-{ -0x80i8 }, -0x80);
1217

‎tests/ui/associated-consts/defaults-cyclic-fail.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ note: ...which requires const-evaluating + checking `Tr::B`...
2020
LL | const B: u8 = Self::A;
2121
| ^^^^^^^
2222
= note: ...which again requires simplifying constant for the type system `Tr::A`, completing the cycle
23-
note: cycle used when const-evaluating + checking `main::promoted[1]`
23+
note: cycle used when optimizing promoted MIR for `main`
2424
--> $DIR/defaults-cyclic-fail.rs:16:16
2525
|
2626
LL | assert_eq!(<() as Tr>::A, 0);

‎tests/ui/associated-consts/defaults-not-assumed-fail.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ note: erroneous constant encountered
1010
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above
1111
| ^^^^^^^^^^^^^
1212

13+
note: erroneous constant encountered
14+
--> $DIR/defaults-not-assumed-fail.rs:33:16
15+
|
16+
LL | assert_eq!(<() as Tr>::B, 0); // causes the error above
17+
| ^^^^^^^^^^^^^
18+
|
19+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20+
1321
note: erroneous constant encountered
1422
--> $DIR/defaults-not-assumed-fail.rs:33:5
1523
|

‎tests/ui/consts/const-err2.noopt.stderr

Lines changed: 0 additions & 48 deletions
This file was deleted.

‎tests/ui/consts/const-err2.opt.stderr

Lines changed: 0 additions & 48 deletions
This file was deleted.

‎tests/ui/consts/const-err2.opt_with_overflow_checks.stderr

Lines changed: 0 additions & 48 deletions
This file was deleted.

‎tests/ui/consts/const-err2.rs

Lines changed: 0 additions & 39 deletions
This file was deleted.

‎tests/ui/consts/const-eval/issue-44578.stderr

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ note: erroneous constant encountered
1010
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13+
note: erroneous constant encountered
14+
--> $DIR/issue-44578.rs:25:20
15+
|
16+
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT);
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
|
19+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20+
1321
note: erroneous constant encountered
1422
--> $DIR/issue-44578.rs:25:20
1523
|

‎tests/ui/consts/const-eval/issue-50814.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ struct Sum<A, B>(A, B);
1414
impl<A: Unsigned, B: Unsigned> Unsigned for Sum<A, B> {
1515
const MAX: u8 = A::MAX + B::MAX;
1616
//~^ ERROR evaluation of `<Sum<U8, U8> as Unsigned>::MAX` failed
17+
//~| ERROR evaluation of `<Sum<U8, U8> as Unsigned>::MAX` failed
1718
}
1819

1920
fn foo<T>(_: T) -> &'static u8 {

‎tests/ui/consts/const-eval/issue-50814.stderr

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,33 @@ LL | const MAX: u8 = A::MAX + B::MAX;
55
| ^^^^^^^^^^^^^^^ attempt to compute `u8::MAX + u8::MAX`, which would overflow
66

77
note: erroneous constant encountered
8-
--> $DIR/issue-50814.rs:20:6
8+
--> $DIR/issue-50814.rs:21:6
99
|
1010
LL | &Sum::<U8, U8>::MAX
1111
| ^^^^^^^^^^^^^^^^^^
1212

13+
error[E0080]: evaluation of `<Sum<U8, U8> as Unsigned>::MAX` failed
14+
--> $DIR/issue-50814.rs:15:21
15+
|
16+
LL | const MAX: u8 = A::MAX + B::MAX;
17+
| ^^^^^^^^^^^^^^^ attempt to compute `u8::MAX + u8::MAX`, which would overflow
18+
|
19+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20+
21+
note: erroneous constant encountered
22+
--> $DIR/issue-50814.rs:21:6
23+
|
24+
LL | &Sum::<U8, U8>::MAX
25+
| ^^^^^^^^^^^^^^^^^^
26+
|
27+
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
28+
1329
note: the above error was encountered while instantiating `fn foo::<i32>`
14-
--> $DIR/issue-50814.rs:25:5
30+
--> $DIR/issue-50814.rs:26:5
1531
|
1632
LL | foo(0);
1733
| ^^^^^^
1834

19-
error: aborting due to 1 previous error
35+
error: aborting due to 2 previous errors
2036

2137
For more information about this error, try `rustc --explain E0080`.

‎tests/ui/consts/overflowing-consts.noopt.stderr

Lines changed: 1023 additions & 0 deletions
Large diffs are not rendered by default.

‎tests/ui/consts/overflowing-consts.opt.stderr

Lines changed: 1023 additions & 0 deletions
Large diffs are not rendered by default.

‎tests/ui/consts/overflowing-consts.opt_with_overflow_checks.stderr

Lines changed: 1023 additions & 0 deletions
Large diffs are not rendered by default.

‎tests/ui/consts/overflowing-consts.rs

Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
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() {}

‎tests/ui/consts/promotion.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ fn main() {
2525
assert_static(&["d", "e", "f"]);
2626
assert_eq!(C, 42);
2727

28-
// make sure that these do not cause trouble despite overflowing
28+
// make sure that this does not cause trouble despite overflowing
2929
assert_static(&(0-1));
30-
assert_static(&-i32::MIN);
3130

3231
// div-by-non-0 is okay
3332
assert_static(&(1/1));
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
error: this arithmetic operation will overflow
2+
--> $DIR/issue-117949.rs:16:24
3+
|
4+
LL | format_args!("{}", 5 * i32::MAX);
5+
| ^^^^^^^^^^^^ attempt to compute `5_i32 * i32::MAX`, which would overflow
6+
|
7+
= note: `#[deny(arithmetic_overflow)]` on by default
8+
9+
error: this arithmetic operation will overflow
10+
--> $DIR/issue-117949.rs:15:24
11+
|
12+
LL | format_args!("{}", -5 - i32::MAX);
13+
| ^^^^^^^^^^^^^ attempt to compute `-5_i32 - i32::MAX`, which would overflow
14+
15+
error: this arithmetic operation will overflow
16+
--> $DIR/issue-117949.rs:14:24
17+
|
18+
LL | format_args!("{}", 1 + i32::MAX);
19+
| ^^^^^^^^^^^^ attempt to compute `1_i32 + i32::MAX`, which would overflow
20+
21+
error: this arithmetic operation will overflow
22+
--> $DIR/issue-117949.rs:13:24
23+
|
24+
LL | format_args!("{}", 1 >> 32);
25+
| ^^^^^^^ attempt to shift right by `32_i32`, which would overflow
26+
27+
error: this arithmetic operation will overflow
28+
--> $DIR/issue-117949.rs:12:24
29+
|
30+
LL | format_args!("{}", 1 << 32);
31+
| ^^^^^^^ attempt to shift left by `32_i32`, which would overflow
32+
33+
error: this operation will panic at runtime
34+
--> $DIR/issue-117949.rs:17:24
35+
|
36+
LL | format_args!("{}", 1 / 0);
37+
| ^^^^^ attempt to divide `1_i32` by zero
38+
|
39+
= note: `#[deny(unconditional_panic)]` on by default
40+
41+
error: this operation will panic at runtime
42+
--> $DIR/issue-117949.rs:18:24
43+
|
44+
LL | format_args!("{}", 1 % 0);
45+
| ^^^^^ attempt to calculate the remainder of `1_i32` with a divisor of zero
46+
47+
error: this operation will panic at runtime
48+
--> $DIR/issue-117949.rs:19:24
49+
|
50+
LL | format_args!("{}", [1, 2, 3][4]);
51+
| ^^^^^^^^^^^^ index out of bounds: the length is 3 but the index is 4
52+
53+
error: aborting due to 8 previous errors
54+

‎tests/ui/lint/issue-117949.opt.stderr

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
error: this arithmetic operation will overflow
2+
--> $DIR/issue-117949.rs:16:24
3+
|
4+
LL | format_args!("{}", 5 * i32::MAX);
5+
| ^^^^^^^^^^^^ attempt to compute `5_i32 * i32::MAX`, which would overflow
6+
|
7+
= note: `#[deny(arithmetic_overflow)]` on by default
8+
9+
error: this arithmetic operation will overflow
10+
--> $DIR/issue-117949.rs:15:24
11+
|
12+
LL | format_args!("{}", -5 - i32::MAX);
13+
| ^^^^^^^^^^^^^ attempt to compute `-5_i32 - i32::MAX`, which would overflow
14+
15+
error: this arithmetic operation will overflow
16+
--> $DIR/issue-117949.rs:14:24
17+
|
18+
LL | format_args!("{}", 1 + i32::MAX);
19+
| ^^^^^^^^^^^^ attempt to compute `1_i32 + i32::MAX`, which would overflow
20+
21+
error: this arithmetic operation will overflow
22+
--> $DIR/issue-117949.rs:13:24
23+
|
24+
LL | format_args!("{}", 1 >> 32);
25+
| ^^^^^^^ attempt to shift right by `32_i32`, which would overflow
26+
27+
error: this arithmetic operation will overflow
28+
--> $DIR/issue-117949.rs:12:24
29+
|
30+
LL | format_args!("{}", 1 << 32);
31+
| ^^^^^^^ attempt to shift left by `32_i32`, which would overflow
32+
33+
error: this operation will panic at runtime
34+
--> $DIR/issue-117949.rs:17:24
35+
|
36+
LL | format_args!("{}", 1 / 0);
37+
| ^^^^^ attempt to divide `1_i32` by zero
38+
|
39+
= note: `#[deny(unconditional_panic)]` on by default
40+
41+
error: this operation will panic at runtime
42+
--> $DIR/issue-117949.rs:18:24
43+
|
44+
LL | format_args!("{}", 1 % 0);
45+
| ^^^^^ attempt to calculate the remainder of `1_i32` with a divisor of zero
46+
47+
error: this operation will panic at runtime
48+
--> $DIR/issue-117949.rs:19:24
49+
|
50+
LL | format_args!("{}", [1, 2, 3][4]);
51+
| ^^^^^^^^^^^^ index out of bounds: the length is 3 but the index is 4
52+
53+
error: aborting due to 8 previous errors
54+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
error: this arithmetic operation will overflow
2+
--> $DIR/issue-117949.rs:16:24
3+
|
4+
LL | format_args!("{}", 5 * i32::MAX);
5+
| ^^^^^^^^^^^^ attempt to compute `5_i32 * i32::MAX`, which would overflow
6+
|
7+
= note: `#[deny(arithmetic_overflow)]` on by default
8+
9+
error: this arithmetic operation will overflow
10+
--> $DIR/issue-117949.rs:15:24
11+
|
12+
LL | format_args!("{}", -5 - i32::MAX);
13+
| ^^^^^^^^^^^^^ attempt to compute `-5_i32 - i32::MAX`, which would overflow
14+
15+
error: this arithmetic operation will overflow
16+
--> $DIR/issue-117949.rs:14:24
17+
|
18+
LL | format_args!("{}", 1 + i32::MAX);
19+
| ^^^^^^^^^^^^ attempt to compute `1_i32 + i32::MAX`, which would overflow
20+
21+
error: this arithmetic operation will overflow
22+
--> $DIR/issue-117949.rs:13:24
23+
|
24+
LL | format_args!("{}", 1 >> 32);
25+
| ^^^^^^^ attempt to shift right by `32_i32`, which would overflow
26+
27+
error: this arithmetic operation will overflow
28+
--> $DIR/issue-117949.rs:12:24
29+
|
30+
LL | format_args!("{}", 1 << 32);
31+
| ^^^^^^^ attempt to shift left by `32_i32`, which would overflow
32+
33+
error: this operation will panic at runtime
34+
--> $DIR/issue-117949.rs:17:24
35+
|
36+
LL | format_args!("{}", 1 / 0);
37+
| ^^^^^ attempt to divide `1_i32` by zero
38+
|
39+
= note: `#[deny(unconditional_panic)]` on by default
40+
41+
error: this operation will panic at runtime
42+
--> $DIR/issue-117949.rs:18:24
43+
|
44+
LL | format_args!("{}", 1 % 0);
45+
| ^^^^^ attempt to calculate the remainder of `1_i32` with a divisor of zero
46+
47+
error: this operation will panic at runtime
48+
--> $DIR/issue-117949.rs:19:24
49+
|
50+
LL | format_args!("{}", [1, 2, 3][4]);
51+
| ^^^^^^^^^^^^ index out of bounds: the length is 3 but the index is 4
52+
53+
error: aborting due to 8 previous errors
54+

‎tests/ui/lint/issue-117949.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Regression test for issue #117949
2+
3+
//@ revisions: noopt opt opt_with_overflow_checks
4+
//@ [noopt]compile-flags: -C opt-level=0 -Z deduplicate-diagnostics=yes
5+
//@ [opt]compile-flags: -O
6+
//@ [opt_with_overflow_checks]compile-flags: -C overflow-checks=on -O -Z deduplicate-diagnostics=yes
7+
//@ build-fail
8+
//@ ignore-pass (test tests codegen-time behaviour)
9+
10+
11+
fn main() {
12+
format_args!("{}", 1 << 32); //~ ERROR: arithmetic operation will overflow
13+
format_args!("{}", 1 >> 32); //~ ERROR: arithmetic operation will overflow
14+
format_args!("{}", 1 + i32::MAX); //~ ERROR: arithmetic operation will overflow
15+
format_args!("{}", -5 - i32::MAX); //~ ERROR: arithmetic operation will overflow
16+
format_args!("{}", 5 * i32::MAX); //~ ERROR: arithmetic operation will overflow
17+
format_args!("{}", 1 / 0); //~ ERROR: this operation will panic at runtime
18+
format_args!("{}", 1 % 0); //~ ERROR: this operation will panic at runtime
19+
format_args!("{}", [1, 2, 3][4]); //~ ERROR: this operation will panic at runtime
20+
}

‎tests/ui/lint/lint-exceeding-bitshifts.noopt.stderr

Lines changed: 0 additions & 152 deletions
This file was deleted.

‎tests/ui/lint/lint-exceeding-bitshifts.opt.stderr

Lines changed: 0 additions & 152 deletions
This file was deleted.

‎tests/ui/lint/lint-exceeding-bitshifts.opt_with_overflow_checks.stderr

Lines changed: 0 additions & 152 deletions
This file was deleted.

‎tests/ui/lint/lint-exceeding-bitshifts.rs

Lines changed: 0 additions & 79 deletions
This file was deleted.

‎tests/ui/lint/lint-overflowing-ops.noopt.stderr

Lines changed: 1030 additions & 0 deletions
Large diffs are not rendered by default.

‎tests/ui/lint/lint-overflowing-ops.opt.stderr

Lines changed: 1030 additions & 0 deletions
Large diffs are not rendered by default.

‎tests/ui/lint/lint-overflowing-ops.opt_with_overflow_checks.stderr

Lines changed: 1030 additions & 0 deletions
Large diffs are not rendered by default.

‎tests/ui/lint/lint-overflowing-ops.rs

Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
// Tests that overflowing or bound-exceeding operations
2+
// are correclty linted including when they are const promoted
3+
4+
// We are using "-Z deduplicate-diagnostics=yes" because different
5+
// build configurations emit different number of duplicate diagnostics
6+
// and this flag lets us test them all with a single .rs file like this
7+
8+
//@ revisions: noopt opt opt_with_overflow_checks
9+
//@ [noopt]compile-flags: -C opt-level=0 -Z deduplicate-diagnostics=yes
10+
//@ [opt]compile-flags: -O
11+
//@ [opt_with_overflow_checks]compile-flags: -C overflow-checks=on -O -Z deduplicate-diagnostics=yes
12+
//@ build-fail
13+
//@ ignore-pass (test tests codegen-time behaviour)
14+
//@ normalize-stderr-test "shift left by `(64|32)_usize`, which" -> "shift left by `%BITS%`, which"
15+
//@ normalize-stderr-test "shift right by `(64|32)_usize`, which" -> "shift right by `%BITS%`, which"
16+
17+
#![deny(arithmetic_overflow)]
18+
19+
#[cfg(target_pointer_width = "32")]
20+
const BITS: usize = 32;
21+
#[cfg(target_pointer_width = "64")]
22+
const BITS: usize = 64;
23+
24+
fn main() {
25+
// Shift left
26+
let _n = 1u8 << 8; //~ ERROR: arithmetic operation will overflow
27+
let _n = &(1u8 << 8); //~ ERROR: arithmetic operation will overflow
28+
29+
let _n = 1u16 << 16; //~ ERROR: arithmetic operation will overflow
30+
let _n = &(1u16 << 16); //~ ERROR: arithmetic operation will overflow
31+
32+
let _n = 1u32 << 32; //~ ERROR: arithmetic operation will overflow
33+
let _n = &(1u32 << 32); //~ ERROR: arithmetic operation will overflow
34+
35+
let _n = 1u64 << 64; //~ ERROR: arithmetic operation will overflow
36+
let _n = &(1u64 << 64); //~ ERROR: arithmetic operation will overflow
37+
38+
let _n = 1u128 << 128; //~ ERROR: arithmetic operation will overflow
39+
let _n = &(1u128 << 128); //~ ERROR: arithmetic operation will overflow
40+
41+
let _n = 1i8 << 8; //~ ERROR: arithmetic operation will overflow
42+
let _n = &(1i8 << 8); //~ ERROR: arithmetic operation will overflow
43+
44+
let _n = 1i16 << 16; //~ ERROR: arithmetic operation will overflow
45+
let _n = &(1i16 << 16); //~ ERROR: arithmetic operation will overflow
46+
47+
let _n = 1i32 << 32; //~ ERROR: arithmetic operation will overflow
48+
let _n = &(1i32 << 32); //~ ERROR: arithmetic operation will overflow
49+
50+
let _n = 1i64 << 64; //~ ERROR: arithmetic operation will overflow
51+
let _n = &(1i64 << 64); //~ ERROR: arithmetic operation will overflow
52+
53+
let _n = 1i128 << 128; //~ ERROR: arithmetic operation will overflow
54+
let _n = &(1i128 << 128); //~ ERROR: arithmetic operation will overflow
55+
56+
let _n = 1_isize << BITS; //~ ERROR: arithmetic operation will overflow
57+
let _n = &(1_isize << BITS); //~ ERROR: arithmetic operation will overflow
58+
59+
let _n = 1_usize << BITS; //~ ERROR: arithmetic operation will overflow
60+
let _n = &(1_usize << BITS); //~ ERROR: arithmetic operation will overflow
61+
62+
63+
// Shift right
64+
let _n = 1u8 >> 8; //~ ERROR: arithmetic operation will overflow
65+
let _n = &(1u8 >> 8); //~ ERROR: arithmetic operation will overflow
66+
67+
let _n = 1u16 >> 16; //~ ERROR: arithmetic operation will overflow
68+
let _n = &(1u16 >> 16); //~ ERROR: arithmetic operation will overflow
69+
70+
let _n = 1u32 >> 32; //~ ERROR: arithmetic operation will overflow
71+
let _n = &(1u32 >> 32); //~ ERROR: arithmetic operation will overflow
72+
73+
let _n = 1u64 >> 64; //~ ERROR: arithmetic operation will overflow
74+
let _n = &(1u64 >> 64); //~ ERROR: arithmetic operation will overflow
75+
76+
let _n = 1u128 >> 128; //~ ERROR: arithmetic operation will overflow
77+
let _n = &(1u128 >> 128); //~ ERROR: arithmetic operation will overflow
78+
79+
let _n = 1i8 >> 8; //~ ERROR: arithmetic operation will overflow
80+
let _n = &(1i8 >> 8); //~ ERROR: arithmetic operation will overflow
81+
82+
let _n = 1i16 >> 16; //~ ERROR: arithmetic operation will overflow
83+
let _n = &(1i16 >> 16); //~ ERROR: arithmetic operation will overflow
84+
85+
let _n = 1i32 >> 32; //~ ERROR: arithmetic operation will overflow
86+
let _n = &(1i32 >> 32); //~ ERROR: arithmetic operation will overflow
87+
88+
let _n = 1i64 >> 64; //~ ERROR: arithmetic operation will overflow
89+
let _n = &(1i64 >> 64); //~ ERROR: arithmetic operation will overflow
90+
91+
let _n = 1i128 >> 128; //~ ERROR: arithmetic operation will overflow
92+
let _n = &(1i128 >> 128); //~ ERROR: arithmetic operation will overflow
93+
94+
let _n = 1_isize >> BITS; //~ ERROR: arithmetic operation will overflow
95+
let _n = &(1_isize >> BITS); //~ ERROR: arithmetic operation will overflow
96+
97+
let _n = 1_usize >> BITS; //~ ERROR: arithmetic operation will overflow
98+
let _n = &(1_usize >> BITS); //~ ERROR: arithmetic operation will overflow
99+
100+
101+
// Addition
102+
let _n = 1u8 + u8::MAX; //~ ERROR: arithmetic operation will overflow
103+
let _n = &(1u8 + u8::MAX); //~ ERROR: arithmetic operation will overflow
104+
105+
let _n = 1u16 + u16::MAX; //~ ERROR: arithmetic operation will overflow
106+
let _n = &(1u16 + u16::MAX); //~ ERROR: arithmetic operation will overflow
107+
108+
let _n = 1u32 + u32::MAX; //~ ERROR: arithmetic operation will overflow
109+
let _n = &(1u32 + u32::MAX); //~ ERROR: arithmetic operation will overflow
110+
111+
let _n = 1u64 + u64::MAX; //~ ERROR: arithmetic operation will overflow
112+
let _n = &(1u64 + u64::MAX); //~ ERROR: arithmetic operation will overflow
113+
114+
let _n = 1u128 + u128::MAX; //~ ERROR: arithmetic operation will overflow
115+
let _n = &(1u128 + u128::MAX); //~ ERROR: arithmetic operation will overflow
116+
117+
let _n = 1i8 + i8::MAX; //~ ERROR: arithmetic operation will overflow
118+
let _n = &(1i8 + i8::MAX); //~ ERROR: arithmetic operation will overflow
119+
120+
let _n = 1i16 + i16::MAX; //~ ERROR: arithmetic operation will overflow
121+
let _n = &(1i16 + i16::MAX); //~ ERROR: arithmetic operation will overflow
122+
123+
let _n = 1i32 + i32::MAX; //~ ERROR: arithmetic operation will overflow
124+
let _n = &(1i32 + i32::MAX); //~ ERROR: arithmetic operation will overflow
125+
126+
let _n = 1i64 + i64::MAX; //~ ERROR: arithmetic operation will overflow
127+
let _n = &(1i64 + i64::MAX); //~ ERROR: arithmetic operation will overflow
128+
129+
let _n = 1i128 + i128::MAX; //~ ERROR: arithmetic operation will overflow
130+
let _n = &(1i128 + i128::MAX); //~ ERROR: arithmetic operation will overflow
131+
132+
let _n = 1isize + isize::MAX; //~ ERROR: arithmetic operation will overflow
133+
let _n = &(1isize + isize::MAX); //~ ERROR: arithmetic operation will overflow
134+
135+
let _n = 1usize + usize::MAX; //~ ERROR: arithmetic operation will overflow
136+
let _n = &(1usize + usize::MAX); //~ ERROR: arithmetic operation will overflow
137+
138+
139+
// Subtraction
140+
let _n = 1u8 - 5; //~ ERROR: arithmetic operation will overflow
141+
let _n = &(1u8 - 5); //~ ERROR: arithmetic operation will overflow
142+
143+
let _n = 1u16 - 5; //~ ERROR: arithmetic operation will overflow
144+
let _n = &(1u16 - 5); //~ ERROR: arithmetic operation will overflow
145+
146+
let _n = 1u32 - 5; //~ ERROR: arithmetic operation will overflow
147+
let _n = &(1u32 - 5); //~ ERROR: arithmetic operation will overflow
148+
149+
let _n = 1u64 - 5 ; //~ ERROR: arithmetic operation will overflow
150+
let _n = &(1u64 - 5); //~ ERROR: arithmetic operation will overflow
151+
152+
let _n = 1u128 - 5 ; //~ ERROR: arithmetic operation will overflow
153+
let _n = &(1u128 - 5); //~ ERROR: arithmetic operation will overflow
154+
155+
let _n = -5i8 - i8::MAX; //~ ERROR: arithmetic operation will overflow
156+
let _n = &(-5i8 - i8::MAX); //~ ERROR: arithmetic operation will overflow
157+
158+
let _n = -5i16 - i16::MAX; //~ ERROR: arithmetic operation will overflow
159+
let _n = &(-5i16 - i16::MAX); //~ ERROR: arithmetic operation will overflow
160+
161+
let _n = -5i32 - i32::MAX; //~ ERROR: arithmetic operation will overflow
162+
let _n = &(-5i32 - i32::MAX); //~ ERROR: arithmetic operation will overflow
163+
164+
let _n = -5i64 - i64::MAX; //~ ERROR: arithmetic operation will overflow
165+
let _n = &(-5i64 - i64::MAX); //~ ERROR: arithmetic operation will overflow
166+
167+
let _n = -5i128 - i128::MAX; //~ ERROR: arithmetic operation will overflow
168+
let _n = &(-5i128 - i128::MAX); //~ ERROR: arithmetic operation will overflow
169+
170+
let _n = -5isize - isize::MAX; //~ ERROR: arithmetic operation will overflow
171+
let _n = &(-5isize - isize::MAX); //~ ERROR: arithmetic operation will overflow
172+
173+
let _n = 1usize - 5; //~ ERROR: arithmetic operation will overflow
174+
let _n = &(1usize - 5); //~ ERROR: arithmetic operation will overflow
175+
176+
177+
// Multiplication
178+
let _n = u8::MAX * 5; //~ ERROR: arithmetic operation will overflow
179+
let _n = &(u8::MAX * 5); //~ ERROR: arithmetic operation will overflow
180+
181+
let _n = u16::MAX * 5; //~ ERROR: arithmetic operation will overflow
182+
let _n = &(u16::MAX * 5); //~ ERROR: arithmetic operation will overflow
183+
184+
let _n = u32::MAX * 5; //~ ERROR: arithmetic operation will overflow
185+
let _n = &(u32::MAX * 5); //~ ERROR: arithmetic operation will overflow
186+
187+
let _n = u64::MAX * 5; //~ ERROR: arithmetic operation will overflow
188+
let _n = &(u64::MAX * 5); //~ ERROR: arithmetic operation will overflow
189+
190+
let _n = u128::MAX * 5; //~ ERROR: arithmetic operation will overflow
191+
let _n = &(u128::MAX * 5); //~ ERROR: arithmetic operation will overflow
192+
193+
let _n = i8::MAX * i8::MAX; //~ ERROR: arithmetic operation will overflow
194+
let _n = &(i8::MAX * i8::MAX); //~ ERROR: arithmetic operation will overflow
195+
196+
let _n = i16::MAX * 5; //~ ERROR: arithmetic operation will overflow
197+
let _n = &(i16::MAX * 5); //~ ERROR: arithmetic operation will overflow
198+
199+
let _n = i32::MAX * 5; //~ ERROR: arithmetic operation will overflow
200+
let _n = &(i32::MAX * 5); //~ ERROR: arithmetic operation will overflow
201+
202+
let _n = i64::MAX * 5; //~ ERROR: arithmetic operation will overflow
203+
let _n = &(i64::MAX * 5); //~ ERROR: arithmetic operation will overflow
204+
205+
let _n = i128::MAX * 5; //~ ERROR: arithmetic operation will overflow
206+
let _n = &(i128::MAX * 5); //~ ERROR: arithmetic operation will overflow
207+
208+
let _n = isize::MAX * 5; //~ ERROR: arithmetic operation will overflow
209+
let _n = &(isize::MAX * 5); //~ ERROR: arithmetic operation will overflow
210+
211+
let _n = usize::MAX * 5; //~ ERROR: arithmetic operation will overflow
212+
let _n = &(usize::MAX * 5); //~ ERROR: arithmetic operation will overflow
213+
214+
215+
// Division
216+
let _n = 1u8 / 0; //~ ERROR: this operation will panic at runtime
217+
let _n = &(1u8 / 0); //~ ERROR: this operation will panic at runtime
218+
219+
let _n = 1u16 / 0; //~ ERROR: this operation will panic at runtime
220+
let _n = &(1u16 / 0); //~ ERROR: this operation will panic at runtime
221+
222+
let _n = 1u32 / 0; //~ ERROR: this operation will panic at runtime
223+
let _n = &(1u32 / 0); //~ ERROR: this operation will panic at runtime
224+
225+
let _n = 1u64 / 0; //~ ERROR: this operation will panic at runtime
226+
let _n = &(1u64 / 0); //~ ERROR: this operation will panic at runtime
227+
228+
let _n = 1u128 / 0; //~ ERROR: this operation will panic at runtime
229+
let _n = &(1u128 / 0); //~ ERROR: this operation will panic at runtime
230+
231+
let _n = 1i8 / 0; //~ ERROR: this operation will panic at runtime
232+
let _n = &(1i8 / 0); //~ ERROR: this operation will panic at runtime
233+
234+
let _n = 1i16 / 0; //~ ERROR: this operation will panic at runtime
235+
let _n = &(1i16 / 0); //~ ERROR: this operation will panic at runtime
236+
237+
let _n = 1i32 / 0; //~ ERROR: this operation will panic at runtime
238+
let _n = &(1i32 / 0); //~ ERROR: this operation will panic at runtime
239+
240+
let _n = 1i64 / 0; //~ ERROR: this operation will panic at runtime
241+
let _n = &(1i64 / 0); //~ ERROR: this operation will panic at runtime
242+
243+
let _n = 1i128 / 0; //~ ERROR: this operation will panic at runtime
244+
let _n = &(1i128 / 0); //~ ERROR: this operation will panic at runtime
245+
246+
let _n = 1isize / 0; //~ ERROR: this operation will panic at runtime
247+
let _n = &(1isize / 0); //~ ERROR: this operation will panic at runtime
248+
249+
let _n = 1usize / 0; //~ ERROR: this operation will panic at runtime
250+
let _n = &(1usize / 0); //~ ERROR: this operation will panic at runtime
251+
252+
253+
// Modulus
254+
let _n = 1u8 % 0; //~ ERROR: this operation will panic at runtime
255+
let _n = &(1u8 % 0); //~ ERROR: this operation will panic at runtime
256+
257+
let _n = 1u16 % 0; //~ ERROR: this operation will panic at runtime
258+
let _n = &(1u16 % 0); //~ ERROR: this operation will panic at runtime
259+
260+
let _n = 1u32 % 0; //~ ERROR: this operation will panic at runtime
261+
let _n = &(1u32 % 0); //~ ERROR: this operation will panic at runtime
262+
263+
let _n = 1u64 % 0; //~ ERROR: this operation will panic at runtime
264+
let _n = &(1u64 % 0); //~ ERROR: this operation will panic at runtime
265+
266+
let _n = 1u128 % 0; //~ ERROR: this operation will panic at runtime
267+
let _n = &(1u128 % 0); //~ ERROR: this operation will panic at runtime
268+
269+
let _n = 1i8 % 0; //~ ERROR: this operation will panic at runtime
270+
let _n = &(1i8 % 0); //~ ERROR: this operation will panic at runtime
271+
272+
let _n = 1i16 % 0; //~ ERROR: this operation will panic at runtime
273+
let _n = &(1i16 % 0); //~ ERROR: this operation will panic at runtime
274+
275+
let _n = 1i32 % 0; //~ ERROR: this operation will panic at runtime
276+
let _n = &(1i32 % 0); //~ ERROR: this operation will panic at runtime
277+
278+
let _n = 1i64 % 0; //~ ERROR: this operation will panic at runtime
279+
let _n = &(1i64 % 0); //~ ERROR: this operation will panic at runtime
280+
281+
let _n = 1i128 % 0; //~ ERROR: this operation will panic at runtime
282+
let _n = &(1i128 % 0); //~ ERROR: this operation will panic at runtime
283+
284+
let _n = 1isize % 0; //~ ERROR: this operation will panic at runtime
285+
let _n = &(1isize % 0); //~ ERROR: this operation will panic at runtime
286+
287+
let _n = 1usize % 0; //~ ERROR: this operation will panic at runtime
288+
let _n = &(1usize % 0); //~ ERROR: this operation will panic at runtime
289+
290+
291+
// Out of bounds access
292+
let _n = [1, 2, 3][4]; //~ ERROR: this operation will panic at runtime
293+
let _n = &([1, 2, 3][4]); //~ ERROR: this operation will panic at runtime
294+
}

‎tests/ui/numbers-arithmetic/promoted_overflow_opt.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.