Skip to content

Commit d81651e

Browse files
committed
Release mode overflows should not cause const eval to error
1 parent bdace29 commit d81651e

File tree

6 files changed

+62
-32
lines changed

6 files changed

+62
-32
lines changed

src/librustc_mir/interpret/eval_context.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -522,21 +522,13 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
522522
BinaryOp(bin_op, ref left, ref right) => {
523523
let left = self.eval_operand(left)?;
524524
let right = self.eval_operand(right)?;
525-
if self.intrinsic_overflowing(
525+
self.intrinsic_overflowing(
526526
bin_op,
527527
left,
528528
right,
529529
dest,
530530
dest_ty,
531-
)?
532-
{
533-
// There was an overflow in an unchecked binop. Right now, we consider this an error and bail out.
534-
// The rationale is that the reason rustc emits unchecked binops in release mode (vs. the checked binops
535-
// it emits in debug mode) is performance, but it doesn't cost us any performance in miri.
536-
// If, however, the compiler ever starts transforming unchecked intrinsics into unchecked binops,
537-
// we have to go back to just ignoring the overflow here.
538-
return err!(Overflow(bin_op));
539-
}
531+
)?;
540532
}
541533

542534
CheckedBinaryOp(bin_op, ref left, ref right) => {
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(const_err)]
12+
13+
// error-pattern: attempt to divide by zero
14+
15+
fn main() {
16+
let x = &(1 / (1 - 1));
17+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(const_err)]
12+
13+
// error-pattern: overflow
14+
15+
fn main() {
16+
let x: &'static u32 = &(0u32 - 1);
17+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![allow(const_err)]
12+
13+
// compile-flags: -O
14+
15+
fn main() {
16+
let x = &(0u32 - 1);
17+
assert_eq!(*x, u32::max_value())
18+
}

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

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
// compile-flags: -O
1515
fn main() {
1616
println!("{}", 0u32 - 1);
17-
//~^ WARN const_err
18-
//~| WARN const_err
1917
let _x = 0u32 - 1;
2018
//~^ WARN const_err
2119
println!("{}", 1/(1-1));

src/test/ui/const-eval/promoted_errors.stderr

+8-20
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,41 @@
11
warning: constant evaluation error
2-
--> $DIR/promoted_errors.rs:16:20
2+
--> $DIR/promoted_errors.rs:17:14
33
|
4-
LL | println!("{}", 0u32 - 1);
5-
| ^^^^^^^^ attempt to subtract with overflow
4+
LL | let _x = 0u32 - 1;
5+
| ^^^^^^^^ attempt to subtract with overflow
66
|
77
note: lint level defined here
88
--> $DIR/promoted_errors.rs:11:9
99
|
1010
LL | #![warn(const_err)]
1111
| ^^^^^^^^^
1212

13-
warning: constant evaluation error
14-
--> $DIR/promoted_errors.rs:16:20
15-
|
16-
LL | println!("{}", 0u32 - 1);
17-
| ^^^^^^^^ attempt to subtract with overflow
18-
19-
warning: constant evaluation error
20-
--> $DIR/promoted_errors.rs:19:14
21-
|
22-
LL | let _x = 0u32 - 1;
23-
| ^^^^^^^^ attempt to subtract with overflow
24-
2513
warning: attempt to divide by zero
26-
--> $DIR/promoted_errors.rs:21:20
14+
--> $DIR/promoted_errors.rs:19:20
2715
|
2816
LL | println!("{}", 1/(1-1));
2917
| ^^^^^^^
3018

3119
warning: constant evaluation error
32-
--> $DIR/promoted_errors.rs:21:20
20+
--> $DIR/promoted_errors.rs:19:20
3321
|
3422
LL | println!("{}", 1/(1-1));
3523
| ^^^^^^^ attempt to divide by zero
3624

3725
warning: attempt to divide by zero
38-
--> $DIR/promoted_errors.rs:24:14
26+
--> $DIR/promoted_errors.rs:22:14
3927
|
4028
LL | let _x = 1/(1-1);
4129
| ^^^^^^^
4230

4331
warning: constant evaluation error
44-
--> $DIR/promoted_errors.rs:24:14
32+
--> $DIR/promoted_errors.rs:22:14
4533
|
4634
LL | let _x = 1/(1-1);
4735
| ^^^^^^^ attempt to divide by zero
4836

4937
warning: constant evaluation error
50-
--> $DIR/promoted_errors.rs:27:20
38+
--> $DIR/promoted_errors.rs:25:20
5139
|
5240
LL | println!("{}", 1/(false as u32));
5341
| ^^^^^^^^^^^^^^^^ attempt to divide by zero

0 commit comments

Comments
 (0)