Skip to content

Commit 3a6aada

Browse files
committed
Add opt-level check
1 parent 35c9e5f commit 3a6aada

File tree

8 files changed

+67
-147
lines changed

8 files changed

+67
-147
lines changed

src/librustc_mir/transform/const_prop.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::cell::Cell;
66
use rustc::hir::def::DefKind;
77
use rustc::mir::{
88
AggregateKind, Constant, Location, Place, PlaceBase, Body, Operand, Rvalue,
9-
Local, NullOp, StatementKind, Statement, LocalKind, Static, StaticKind,
9+
Local, NullOp, UnOp, StatementKind, Statement, LocalKind, Static, StaticKind,
1010
TerminatorKind, Terminator, ClearCrossCrate, SourceInfo, BinOp, ProjectionElem,
1111
SourceScope, SourceScopeLocalData, LocalDecl,
1212
};
@@ -405,8 +405,20 @@ 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.opts.debugging_opts.mir_opt_level >= 2;
408409
let val = self.use_ecx(source_info, |this| {
409410
let prim = this.ecx.read_immediate(arg)?;
411+
match op {
412+
UnOp::Neg => {
413+
if is_release_mode
414+
&& prim.to_bits()? == (1 << (prim.layout.size.bits() - 1)) {
415+
throw_panic!(OverflowNeg)
416+
}
417+
}
418+
UnOp::Not => {
419+
// Cannot overflow
420+
}
421+
}
410422
// Now run the actual operation.
411423
this.ecx.unary_op(op, prim)
412424
})?;
@@ -473,7 +485,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
473485
Scalar::from_bool(overflow).into(),
474486
)
475487
} else {
476-
if overflow {
488+
if self.tcx.sess.opts.debugging_opts.mir_opt_level >= 2 && overflow {
477489
let err = err_panic!(Overflow(op)).into();
478490
let _: Option<()> = self.use_ecx(source_info, |_| Err(err));
479491
return None;

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,8 @@ fn black_box<T>(_: T) {
1414
fn main() {
1515
let a = -std::i8::MIN;
1616
let b = 200u8 + 200u8 + 200u8;
17-
//~^ ERROR const_err
1817
let c = 200u8 * 4;
19-
//~^ ERROR const_err
2018
let d = 42u8 - (42u8 + 1);
21-
//~^ ERROR const_err
2219
let _e = [5u8][1];
2320
//~^ ERROR const_err
2421
black_box(a);

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

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,14 @@
1-
error: this expression will panic at runtime
2-
--> $DIR/const-err2.rs:16:13
1+
error: index out of bounds: the len is 1 but the index is 1
2+
--> $DIR/const-err2.rs:19:14
33
|
4-
LL | let b = 200u8 + 200u8 + 200u8;
5-
| ^^^^^^^^^^^^^ attempt to add with overflow
4+
LL | let _e = [5u8][1];
5+
| ^^^^^^^^
66
|
77
note: lint level defined here
88
--> $DIR/const-err2.rs:8:9
99
|
1010
LL | #![deny(const_err)]
1111
| ^^^^^^^^^
1212

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

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
fn main() {
66
println!("{}", 0u32 - 1);
77
let _x = 0u32 - 1;
8-
//~^ ERROR this expression will panic at runtime [const_err]
98
println!("{}", 1/(1-1));
109
//~^ ERROR attempt to divide by zero [const_err]
1110
//~| ERROR reaching this expression at runtime will panic or abort [const_err]
Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,56 @@
1-
error: this expression will panic at runtime
2-
--> $DIR/promoted_errors.rs:7:14
1+
error: attempt to divide by zero
2+
--> $DIR/promoted_errors.rs:8:20
33
|
4-
LL | let _x = 0u32 - 1;
5-
| ^^^^^^^^ attempt to subtract with overflow
4+
LL | println!("{}", 1/(1-1));
5+
| ^^^^^^^
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-
1913
error: reaching this expression at runtime will panic or abort
20-
--> $DIR/promoted_errors.rs:9:20
14+
--> $DIR/promoted_errors.rs:8:20
2115
|
2216
LL | println!("{}", 1/(1-1));
2317
| ^^^^^^^ attempt to divide by zero
2418

2519
error: attempt to divide by zero
26-
--> $DIR/promoted_errors.rs:12:14
20+
--> $DIR/promoted_errors.rs:11:14
2721
|
2822
LL | let _x = 1/(1-1);
2923
| ^^^^^^^
3024

3125
error: this expression will panic at runtime
32-
--> $DIR/promoted_errors.rs:12:14
26+
--> $DIR/promoted_errors.rs:11:14
3327
|
3428
LL | let _x = 1/(1-1);
3529
| ^^^^^^^ attempt to divide by zero
3630

3731
error: attempt to divide by zero
38-
--> $DIR/promoted_errors.rs:15:20
32+
--> $DIR/promoted_errors.rs:14:20
3933
|
4034
LL | println!("{}", 1/(false as u32));
4135
| ^^^^^^^^^^^^^^^^
4236

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

4943
error: attempt to divide by zero
50-
--> $DIR/promoted_errors.rs:18:14
44+
--> $DIR/promoted_errors.rs:17:14
5145
|
5246
LL | let _x = 1/(false as u32);
5347
| ^^^^^^^^^^^^^^^^
5448

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
error: failed to remove $TEST_BUILD_DIR/issues/issue-8171-default-method-self-inherit-builtin-trait/issue-8171-default-method-self-inherit-builtin-trait.issue_8171_default_method_self_inherit_builtin_trait.7rcbfp3g-cgu.0.rcgu.o: 指定されたパスが見つかりません。 (os error 3)
2+
3+
error: failed to remove $TEST_BUILD_DIR/issues/issue-8171-default-method-self-inherit-builtin-trait/issue-8171-default-method-self-inherit-builtin-trait.issue_8171_default_method_self_inherit_builtin_trait.7rcbfp3g-cgu.1.rcgu.o: 指定されたパスが見つかりません。 (os error 3)
4+
5+
error: aborting due to 2 previous errors
6+

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

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,14 @@ 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
109
assert!(thread::spawn(move|| { i8::MIN / -1; }).join().is_err());
1110
//~^ ERROR attempt to divide with overflow
12-
//~| ERROR this expression will panic at runtime
1311
assert!(thread::spawn(move|| { i16::MIN / -1; }).join().is_err());
1412
//~^ ERROR attempt to divide with overflow
15-
//~| ERROR this expression will panic at runtime
1613
assert!(thread::spawn(move|| { i32::MIN / -1; }).join().is_err());
1714
//~^ ERROR attempt to divide with overflow
18-
//~| ERROR this expression will panic at runtime
1915
assert!(thread::spawn(move|| { i64::MIN / -1; }).join().is_err());
2016
//~^ ERROR attempt to divide with overflow
21-
//~| ERROR this expression will panic at runtime
2217
assert!(thread::spawn(move|| { 1isize / 0; }).join().is_err());
2318
//~^ ERROR attempt to divide by zero
2419
//~| ERROR this expression will panic at runtime
@@ -36,19 +31,14 @@ fn main() {
3631
//~| ERROR this expression will panic at runtime
3732
assert!(thread::spawn(move|| { isize::MIN % -1; }).join().is_err());
3833
//~^ ERROR attempt to calculate the remainder with overflow
39-
//~| ERROR this expression will panic at runtime
4034
assert!(thread::spawn(move|| { i8::MIN % -1; }).join().is_err());
4135
//~^ ERROR attempt to calculate the remainder with overflow
42-
//~| ERROR this expression will panic at runtime
4336
assert!(thread::spawn(move|| { i16::MIN % -1; }).join().is_err());
4437
//~^ ERROR attempt to calculate the remainder with overflow
45-
//~| ERROR this expression will panic at runtime
4638
assert!(thread::spawn(move|| { i32::MIN % -1; }).join().is_err());
4739
//~^ ERROR attempt to calculate the remainder with overflow
48-
//~| ERROR this expression will panic at runtime
4940
assert!(thread::spawn(move|| { i64::MIN % -1; }).join().is_err());
5041
//~^ ERROR attempt to calculate the remainder with overflow
51-
//~| ERROR this expression will panic at runtime
5242
assert!(thread::spawn(move|| { 1isize % 0; }).join().is_err());
5343
//~^ ERROR attempt to calculate the remainder with a divisor of zero
5444
//~| ERROR this expression will panic at runtime

0 commit comments

Comments
 (0)