Skip to content

Commit 22c25dd

Browse files
committed
Auto merge of #50841 - oli-obk:promote_errors_to_panics, r=eddyb
Don't lint numeric overflows in promoteds in release mode r? @eddyb mitigates #50814
2 parents 4c26e2e + 8753d0f commit 22c25dd

File tree

9 files changed

+134
-34
lines changed

9 files changed

+134
-34
lines changed

src/librustc_codegen_llvm/mir/operand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -413,10 +413,10 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
413413
.unwrap_or_else(|err| {
414414
match constant.literal {
415415
mir::Literal::Promoted { .. } => {
416-
// don't report errors inside promoteds, just warnings.
416+
// FIXME: generate a panic here
417417
},
418418
mir::Literal::Value { .. } => {
419-
err.report(bx.tcx(), constant.span, "const operand")
419+
err.report(bx.tcx(), constant.span, "const operand");
420420
},
421421
}
422422
// We've errored, so we don't have to produce working code.

src/librustc_mir/interpret/eval_context.rs

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

541533
CheckedBinaryOp(bin_op, ref left, ref right) => {
Lines changed: 17 additions & 0 deletions
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+
}
Lines changed: 18 additions & 0 deletions
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+
// error-pattern: overflow
14+
// compile-flags: -C overflow-checks=yes
15+
16+
fn main() {
17+
let x: &'static u32 = &(0u32 - 1);
18+
}
Lines changed: 18 additions & 0 deletions
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+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
#![feature(const_fn)]
12+
13+
#![deny(const_err)]
14+
15+
union Bar {
16+
a: &'static u8,
17+
b: usize,
18+
}
19+
20+
const fn bar() -> u8 {
21+
unsafe {
22+
// this will error as long as this test
23+
// is run on a system whose pointers need more
24+
// than 8 bits
25+
Bar { a: &42 }.b as u8
26+
//~^ constant evaluation error
27+
//~| constant evaluation error
28+
}
29+
}
30+
31+
fn main() {
32+
// FIXME(oli-obk): this should compile but panic at runtime
33+
// if we change the `const_err` lint to allow this will actually compile, but then
34+
// continue with undefined values.
35+
let x: &'static u8 = &(bar() + 1);
36+
let y = *x;
37+
unreachable!();
38+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error: constant evaluation error
2+
--> $DIR/promoted_const_fn_fail.rs:25:9
3+
|
4+
LL | Bar { a: &42 }.b as u8
5+
| ^^^^^^^^^^^^^^^^^^^^^^ a raw memory access tried to access part of a pointer value as raw bytes
6+
|
7+
note: lint level defined here
8+
--> $DIR/promoted_const_fn_fail.rs:13:9
9+
|
10+
LL | #![deny(const_err)]
11+
| ^^^^^^^^^
12+
note: inside call to `bar`
13+
--> $DIR/promoted_const_fn_fail.rs:35:28
14+
|
15+
LL | let x: &'static u8 = &(bar() + 1);
16+
| ^^^^^
17+
18+
error: constant evaluation error
19+
--> $DIR/promoted_const_fn_fail.rs:25:9
20+
|
21+
LL | Bar { a: &42 }.b as u8
22+
| ^^^^^^^^^^^^^^^^^^^^^^ a raw memory access tried to access part of a pointer value as raw bytes
23+
|
24+
note: inside call to `bar`
25+
--> $DIR/promoted_const_fn_fail.rs:35:28
26+
|
27+
LL | let x: &'static u8 = &(bar() + 1);
28+
| ^^^^^
29+
30+
error: aborting due to 2 previous errors
31+

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

Lines changed: 0 additions & 2 deletions
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

Lines changed: 8 additions & 20 deletions
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)