Release mode overflows should not cause const eval to error

This commit is contained in:
Oliver Schneider 2018-05-17 12:07:00 +02:00
parent bdace29de0
commit d81651e8e9
6 changed files with 62 additions and 32 deletions

View file

@ -522,21 +522,13 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
BinaryOp(bin_op, ref left, ref right) => {
let left = self.eval_operand(left)?;
let right = self.eval_operand(right)?;
if self.intrinsic_overflowing(
self.intrinsic_overflowing(
bin_op,
left,
right,
dest,
dest_ty,
)?
{
// There was an overflow in an unchecked binop. Right now, we consider this an error and bail out.
// The rationale is that the reason rustc emits unchecked binops in release mode (vs. the checked binops
// it emits in debug mode) is performance, but it doesn't cost us any performance in miri.
// If, however, the compiler ever starts transforming unchecked intrinsics into unchecked binops,
// we have to go back to just ignoring the overflow here.
return err!(Overflow(bin_op));
}
)?;
}
CheckedBinaryOp(bin_op, ref left, ref right) => {

View file

@ -0,0 +1,17 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![allow(const_err)]
// error-pattern: attempt to divide by zero
fn main() {
let x = &(1 / (1 - 1));
}

View file

@ -0,0 +1,17 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![allow(const_err)]
// error-pattern: overflow
fn main() {
let x: &'static u32 = &(0u32 - 1);
}

View file

@ -0,0 +1,18 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
#![allow(const_err)]
// compile-flags: -O
fn main() {
let x = &(0u32 - 1);
assert_eq!(*x, u32::max_value())
}

View file

@ -14,8 +14,6 @@
// compile-flags: -O
fn main() {
println!("{}", 0u32 - 1);
//~^ WARN const_err
//~| WARN const_err
let _x = 0u32 - 1;
//~^ WARN const_err
println!("{}", 1/(1-1));

View file

@ -1,8 +1,8 @@
warning: constant evaluation error
--> $DIR/promoted_errors.rs:16:20
--> $DIR/promoted_errors.rs:17:14
|
LL | println!("{}", 0u32 - 1);
| ^^^^^^^^ attempt to subtract with overflow
LL | let _x = 0u32 - 1;
| ^^^^^^^^ attempt to subtract with overflow
|
note: lint level defined here
--> $DIR/promoted_errors.rs:11:9
@ -10,44 +10,32 @@ note: lint level defined here
LL | #![warn(const_err)]
| ^^^^^^^^^
warning: constant evaluation error
--> $DIR/promoted_errors.rs:16:20
|
LL | println!("{}", 0u32 - 1);
| ^^^^^^^^ attempt to subtract with overflow
warning: constant evaluation error
--> $DIR/promoted_errors.rs:19:14
|
LL | let _x = 0u32 - 1;
| ^^^^^^^^ attempt to subtract with overflow
warning: attempt to divide by zero
--> $DIR/promoted_errors.rs:21:20
--> $DIR/promoted_errors.rs:19:20
|
LL | println!("{}", 1/(1-1));
| ^^^^^^^
warning: constant evaluation error
--> $DIR/promoted_errors.rs:21:20
--> $DIR/promoted_errors.rs:19:20
|
LL | println!("{}", 1/(1-1));
| ^^^^^^^ attempt to divide by zero
warning: attempt to divide by zero
--> $DIR/promoted_errors.rs:24:14
--> $DIR/promoted_errors.rs:22:14
|
LL | let _x = 1/(1-1);
| ^^^^^^^
warning: constant evaluation error
--> $DIR/promoted_errors.rs:24:14
--> $DIR/promoted_errors.rs:22:14
|
LL | let _x = 1/(1-1);
| ^^^^^^^ attempt to divide by zero
warning: constant evaluation error
--> $DIR/promoted_errors.rs:27:20
--> $DIR/promoted_errors.rs:25:20
|
LL | println!("{}", 1/(false as u32));
| ^^^^^^^^^^^^^^^^ attempt to divide by zero