Skip to content

Commit 8e9825a

Browse files
committed
Fix overflow_check
1 parent 4a0872b commit 8e9825a

File tree

9 files changed

+170
-56
lines changed

9 files changed

+170
-56
lines changed

src/librustc_mir/transform/const_prop.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -405,14 +405,14 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
405405
}
406406

407407
let arg = self.eval_operand(arg, source_info)?;
408-
let is_release_mode = self.tcx.sess.overflow_checks();
408+
let oflo_check = self.tcx.sess.overflow_checks();
409409
let val = self.use_ecx(source_info, |this| {
410410
let prim = this.ecx.read_immediate(arg)?;
411411
match op {
412412
UnOp::Neg => {
413-
// We don't have to check overflow here when we already
414-
// check it in release mode.
415-
if is_release_mode
413+
// We check overflow in debug mode already
414+
// so should only check in release mode.
415+
if !oflo_check
416416
&& prim.to_bits()? == (1 << (prim.layout.size.bits() - 1)) {
417417
throw_panic!(OverflowNeg)
418418
}
@@ -487,9 +487,9 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
487487
Scalar::from_bool(overflow).into(),
488488
)
489489
} else {
490-
// We don't have to check overflow here when we already
491-
// check it in release mode.
492-
if self.tcx.sess.overflow_checks() && overflow {
490+
// We check overflow in debug mode already
491+
// so should only check in release mode.
492+
if !self.tcx.sess.overflow_checks() && overflow {
493493
let err = err_panic!(Overflow(op)).into();
494494
let _: Option<()> = self.use_ecx(source_info, |_| Err(err));
495495
return None;

src/test/ui/consts/const-err2.rs

+4
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ fn black_box<T>(_: T) {
1313

1414
fn main() {
1515
let a = -std::i8::MIN;
16+
//~^ ERROR const_err
1617
let b = 200u8 + 200u8 + 200u8;
18+
//~^ ERROR const_err
1719
let c = 200u8 * 4;
20+
//~^ ERROR const_err
1821
let d = 42u8 - (42u8 + 1);
22+
//~^ ERROR const_err
1923
let _e = [5u8][1];
2024
//~^ ERROR const_err
2125
black_box(a);

src/test/ui/consts/const-err2.stderr

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,38 @@
1-
error: index out of bounds: the len is 1 but the index is 1
2-
--> $DIR/const-err2.rs:19:14
1+
error: this expression will panic at runtime
2+
--> $DIR/const-err2.rs:15:13
33
|
4-
LL | let _e = [5u8][1];
5-
| ^^^^^^^^
4+
LL | let a = -std::i8::MIN;
5+
| ^^^^^^^^^^^^^ attempt to negate with overflow
66
|
77
note: lint level defined here
88
--> $DIR/const-err2.rs:8:9
99
|
1010
LL | #![deny(const_err)]
1111
| ^^^^^^^^^
1212

13-
error: aborting due to previous error
13+
error: this expression will panic at runtime
14+
--> $DIR/const-err2.rs:17:13
15+
|
16+
LL | let b = 200u8 + 200u8 + 200u8;
17+
| ^^^^^^^^^^^^^ attempt to add with overflow
18+
19+
error: this expression will panic at runtime
20+
--> $DIR/const-err2.rs:19:13
21+
|
22+
LL | let c = 200u8 * 4;
23+
| ^^^^^^^^^ attempt to multiply with overflow
24+
25+
error: this expression will panic at runtime
26+
--> $DIR/const-err2.rs:21:13
27+
|
28+
LL | let d = 42u8 - (42u8 + 1);
29+
| ^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
30+
31+
error: index out of bounds: the len is 1 but the index is 1
32+
--> $DIR/const-err2.rs:23:14
33+
|
34+
LL | let _e = [5u8][1];
35+
| ^^^^^^^^
36+
37+
error: aborting due to 5 previous errors
1438

src/test/ui/consts/const-eval/promoted_errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
fn main() {
66
println!("{}", 0u32 - 1);
77
let _x = 0u32 - 1;
8+
//~^ ERROR this expression will panic at runtime [const_err]
89
println!("{}", 1/(1-1));
910
//~^ ERROR attempt to divide by zero [const_err]
1011
//~| ERROR reaching this expression at runtime will panic or abort [const_err]
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,62 @@
1-
error: attempt to divide by zero
2-
--> $DIR/promoted_errors.rs:8:20
1+
error: this expression will panic at runtime
2+
--> $DIR/promoted_errors.rs:7:14
33
|
4-
LL | println!("{}", 1/(1-1));
5-
| ^^^^^^^
4+
LL | let _x = 0u32 - 1;
5+
| ^^^^^^^^ attempt to subtract with overflow
66
|
77
note: lint level defined here
88
--> $DIR/promoted_errors.rs:3:9
99
|
1010
LL | #![deny(const_err)]
1111
| ^^^^^^^^^
1212

13+
error: attempt to divide by zero
14+
--> $DIR/promoted_errors.rs:9:20
15+
|
16+
LL | println!("{}", 1/(1-1));
17+
| ^^^^^^^
18+
1319
error: reaching this expression at runtime will panic or abort
14-
--> $DIR/promoted_errors.rs:8:20
20+
--> $DIR/promoted_errors.rs:9:20
1521
|
1622
LL | println!("{}", 1/(1-1));
1723
| ^^^^^^^ attempt to divide by zero
1824

1925
error: attempt to divide by zero
20-
--> $DIR/promoted_errors.rs:11:14
26+
--> $DIR/promoted_errors.rs:12:14
2127
|
2228
LL | let _x = 1/(1-1);
2329
| ^^^^^^^
2430

2531
error: this expression will panic at runtime
26-
--> $DIR/promoted_errors.rs:11:14
32+
--> $DIR/promoted_errors.rs:12:14
2733
|
2834
LL | let _x = 1/(1-1);
2935
| ^^^^^^^ attempt to divide by zero
3036

3137
error: attempt to divide by zero
32-
--> $DIR/promoted_errors.rs:14:20
38+
--> $DIR/promoted_errors.rs:15:20
3339
|
3440
LL | println!("{}", 1/(false as u32));
3541
| ^^^^^^^^^^^^^^^^
3642

3743
error: reaching this expression at runtime will panic or abort
38-
--> $DIR/promoted_errors.rs:14:20
44+
--> $DIR/promoted_errors.rs:15:20
3945
|
4046
LL | println!("{}", 1/(false as u32));
4147
| ^^^^^^^^^^^^^^^^ attempt to divide by zero
4248

4349
error: attempt to divide by zero
44-
--> $DIR/promoted_errors.rs:17:14
50+
--> $DIR/promoted_errors.rs:18:14
4551
|
4652
LL | let _x = 1/(false as u32);
4753
| ^^^^^^^^^^^^^^^^
4854

4955
error: this expression will panic at runtime
50-
--> $DIR/promoted_errors.rs:17:14
56+
--> $DIR/promoted_errors.rs:18:14
5157
|
5258
LL | let _x = 1/(false as u32);
5359
| ^^^^^^^^^^^^^^^^ attempt to divide by zero
5460

55-
error: aborting due to 8 previous errors
61+
error: aborting due to 9 previous errors
5662

src/test/ui/consts/issue-64059.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
// run-pass
2-
31
fn main() {
42
let _ = -(-0.0);
3+
//~^ ERROR: this expression will panic at runtime
54
}

src/test/ui/consts/issue-64059.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: this expression will panic at runtime
2+
--> $DIR/issue-64059.rs:2:13
3+
|
4+
LL | let _ = -(-0.0);
5+
| ^^^^^^^ attempt to negate with overflow
6+
|
7+
= note: `#[deny(const_err)]` on by default
8+
9+
error: aborting due to previous error
10+

src/test/ui/issues/issue-8460-const.rs

+10
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,19 @@ use std::thread;
66
fn main() {
77
assert!(thread::spawn(move|| { isize::MIN / -1; }).join().is_err());
88
//~^ ERROR attempt to divide with overflow
9+
//~| ERROR this expression will panic at runtime
910
assert!(thread::spawn(move|| { i8::MIN / -1; }).join().is_err());
1011
//~^ ERROR attempt to divide with overflow
12+
//~| ERROR this expression will panic at runtime
1113
assert!(thread::spawn(move|| { i16::MIN / -1; }).join().is_err());
1214
//~^ ERROR attempt to divide with overflow
15+
//~| ERROR this expression will panic at runtime
1316
assert!(thread::spawn(move|| { i32::MIN / -1; }).join().is_err());
1417
//~^ ERROR attempt to divide with overflow
18+
//~| ERROR this expression will panic at runtime
1519
assert!(thread::spawn(move|| { i64::MIN / -1; }).join().is_err());
1620
//~^ ERROR attempt to divide with overflow
21+
//~| ERROR this expression will panic at runtime
1722
assert!(thread::spawn(move|| { 1isize / 0; }).join().is_err());
1823
//~^ ERROR attempt to divide by zero
1924
//~| ERROR this expression will panic at runtime
@@ -31,14 +36,19 @@ fn main() {
3136
//~| ERROR this expression will panic at runtime
3237
assert!(thread::spawn(move|| { isize::MIN % -1; }).join().is_err());
3338
//~^ ERROR attempt to calculate the remainder with overflow
39+
//~| ERROR this expression will panic at runtime
3440
assert!(thread::spawn(move|| { i8::MIN % -1; }).join().is_err());
3541
//~^ ERROR attempt to calculate the remainder with overflow
42+
//~| ERROR this expression will panic at runtime
3643
assert!(thread::spawn(move|| { i16::MIN % -1; }).join().is_err());
3744
//~^ ERROR attempt to calculate the remainder with overflow
45+
//~| ERROR this expression will panic at runtime
3846
assert!(thread::spawn(move|| { i32::MIN % -1; }).join().is_err());
3947
//~^ ERROR attempt to calculate the remainder with overflow
48+
//~| ERROR this expression will panic at runtime
4049
assert!(thread::spawn(move|| { i64::MIN % -1; }).join().is_err());
4150
//~^ ERROR attempt to calculate the remainder with overflow
51+
//~| ERROR this expression will panic at runtime
4252
assert!(thread::spawn(move|| { 1isize % 0; }).join().is_err());
4353
//~^ ERROR attempt to calculate the remainder with a divisor of zero
4454
//~| ERROR this expression will panic at runtime

0 commit comments

Comments
 (0)