Skip to content

Commit 95946e1

Browse files
committedApr 9, 2018
Only warn on erroneous promoted constants
1 parent 7327d9d commit 95946e1

13 files changed

+129
-30
lines changed
 

‎src/librustc_mir/interpret/const_eval.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub fn mk_eval_cx<'a, 'tcx>(
5757
Ok(ecx)
5858
}
5959

60-
pub fn eval_body_with_mir<'a, 'mir, 'tcx>(
60+
pub fn eval_promoted<'a, 'mir, 'tcx>(
6161
tcx: TyCtxt<'a, 'tcx, 'tcx>,
6262
cid: GlobalId<'tcx>,
6363
mir: &'mir mir::Mir<'tcx>,
@@ -67,7 +67,7 @@ pub fn eval_body_with_mir<'a, 'mir, 'tcx>(
6767
match res {
6868
Ok(val) => Some(val),
6969
Err(mut err) => {
70-
ecx.report(&mut err, true, None);
70+
ecx.report(&mut err, false, None);
7171
None
7272
}
7373
}

‎src/librustc_mir/interpret/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub use self::place::{Place, PlaceExtra};
1919
pub use self::memory::{Memory, MemoryKind, HasMemory};
2020

2121
pub use self::const_eval::{
22-
eval_body_with_mir,
22+
eval_promoted,
2323
mk_borrowck_eval_cx,
2424
eval_body,
2525
CompileTimeEvaluator,

‎src/librustc_mir/monomorphize/collector.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -1177,17 +1177,15 @@ fn collect_neighbours<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
11771177
param_substs: instance.substs,
11781178
}.visit_mir(&mir);
11791179
let param_env = ty::ParamEnv::reveal_all();
1180-
for (i, promoted) in mir.promoted.iter().enumerate() {
1180+
for i in 0..mir.promoted.len() {
11811181
use rustc_data_structures::indexed_vec::Idx;
11821182
let cid = GlobalId {
11831183
instance,
11841184
promoted: Some(Promoted::new(i)),
11851185
};
11861186
match tcx.const_eval(param_env.and(cid)) {
11871187
Ok(val) => collect_const(tcx, val, instance.substs, output),
1188-
Err(err) => {
1189-
err.report(tcx, promoted.span, "promoted");
1190-
}
1188+
Err(_) => {},
11911189
}
11921190
}
11931191
}

‎src/librustc_mir/transform/const_prop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc::mir::visit::{Visitor, PlaceContext};
2020
use rustc::middle::const_val::ConstVal;
2121
use rustc::ty::{TyCtxt, self, Instance};
2222
use rustc::mir::interpret::{Value, PrimVal, GlobalId};
23-
use interpret::{eval_body_with_mir, mk_borrowck_eval_cx, ValTy};
23+
use interpret::{eval_promoted, mk_borrowck_eval_cx, ValTy};
2424
use transform::{MirPass, MirSource};
2525
use syntax::codemap::Span;
2626
use rustc::ty::subst::Substs;
@@ -161,7 +161,7 @@ impl<'b, 'a, 'tcx:'b> ConstPropagator<'b, 'a, 'tcx> {
161161
};
162162
// cannot use `const_eval` here, because that would require having the MIR
163163
// for the current function available, but we're producing said MIR right now
164-
let (value, _, ty) = eval_body_with_mir(self.tcx, cid, self.mir, self.param_env)?;
164+
let (value, _, ty) = eval_promoted(self.tcx, cid, self.mir, self.param_env)?;
165165
let val = (value, ty, c.span);
166166
trace!("evaluated {:?} to {:?}", c, val);
167167
Some(val)

‎src/librustc_trans/mir/operand.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,14 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
399399
self.mir_constant_to_miri_value(bx, constant)
400400
.and_then(|c| OperandRef::from_const(bx, c, ty))
401401
.unwrap_or_else(|err| {
402-
err.report(bx.tcx(), constant.span, "const operand");
402+
match constant.literal {
403+
mir::Literal::Promoted { .. } => {
404+
// don't report errors inside promoteds, just warnings.
405+
},
406+
mir::Literal::Value { .. } => {
407+
err.report(bx.tcx(), constant.span, "const operand")
408+
},
409+
}
403410
// We've errored, so we don't have to produce working code.
404411
let layout = bx.cx.layout_of(ty);
405412
PlaceRef::new_sized(

‎src/test/ui/const-eval/conditional_array_execution.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// must-compile-successfully
12+
1113
const X: u32 = 5;
1214
const Y: u32 = 6;
1315
const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
1416
//~^ WARN attempt to subtract with overflow
1517

1618
fn main() {
17-
println!("{}", FOO); //~ E0080
19+
println!("{}", FOO);
20+
//~^ WARN constant evaluation error
1821
}
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
warning: attempt to subtract with overflow
2-
--> $DIR/conditional_array_execution.rs:13:19
2+
--> $DIR/conditional_array_execution.rs:15:19
33
|
44
LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize];
55
| ^^^^^
66
|
77
= note: #[warn(const_err)] on by default
88

9-
error[E0080]: constant evaluation error
10-
--> $DIR/conditional_array_execution.rs:17:20
9+
warning: constant evaluation error
10+
--> $DIR/conditional_array_execution.rs:19:20
1111
|
12-
LL | println!("{}", FOO); //~ E0080
12+
LL | println!("{}", FOO);
1313
| ^^^ referenced constant has errors
1414

15-
error: aborting due to previous error
16-
17-
For more information about this error, try `rustc --explain E0080`.

‎src/test/ui/const-eval/issue-43197.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// must-compile-successfully
12+
1113
#![feature(const_fn)]
1214

1315
const fn foo(x: u32) -> u32 {
@@ -20,6 +22,6 @@ fn main() {
2022
const Y: u32 = foo(0-1);
2123
//~^ WARN attempt to subtract with overflow
2224
println!("{} {}", X, Y);
23-
//~^ ERROR constant evaluation error
24-
//~| ERROR constant evaluation error
25+
//~^ WARN constant evaluation error
26+
//~| WARN constant evaluation error
2527
}
+6-9
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
11
warning: attempt to subtract with overflow
2-
--> $DIR/issue-43197.rs:18:20
2+
--> $DIR/issue-43197.rs:20:20
33
|
44
LL | const X: u32 = 0-1;
55
| ^^^
66
|
77
= note: #[warn(const_err)] on by default
88

9-
error[E0080]: constant evaluation error
10-
--> $DIR/issue-43197.rs:22:23
9+
warning: constant evaluation error
10+
--> $DIR/issue-43197.rs:24:23
1111
|
1212
LL | println!("{} {}", X, Y);
1313
| ^ referenced constant has errors
1414

1515
warning: attempt to subtract with overflow
16-
--> $DIR/issue-43197.rs:20:24
16+
--> $DIR/issue-43197.rs:22:24
1717
|
1818
LL | const Y: u32 = foo(0-1);
1919
| ^^^
2020

21-
error[E0080]: constant evaluation error
22-
--> $DIR/issue-43197.rs:22:26
21+
warning: constant evaluation error
22+
--> $DIR/issue-43197.rs:24:26
2323
|
2424
LL | println!("{} {}", X, Y);
2525
| ^ referenced constant has errors
2626

27-
error: aborting due to 2 previous errors
28-
29-
For more information about this error, try `rustc --explain E0080`.

‎src/test/compile-fail/issue-44578.rs renamed to ‎src/test/ui/const-eval/issue-44578.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// must-compile-successfully
12+
1113
trait Foo {
1214
const AMT: usize;
1315
}
@@ -30,5 +32,6 @@ impl Foo for u16 {
3032
}
3133

3234
fn main() {
33-
println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ E0080
35+
println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err
36+
//~^ WARN const_err
3437
}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: constant evaluation error
2+
--> $DIR/issue-44578.rs:35:20
3+
|
4+
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
6+
|
7+
= note: #[warn(const_err)] on by default
8+
9+
warning: constant evaluation error
10+
--> $DIR/issue-44578.rs:35:20
11+
|
12+
LL | println!("{}", <Bar<u16, u8> as Foo>::AMT); //~ WARN const_err
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ referenced constant has errors
14+
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
// must-compile-successfully
12+
13+
fn main() {
14+
println!("{}", 0u32 - 1);
15+
//~^ WARN const_err
16+
//~| WARN const_err
17+
let _x = 0u32 - 1;
18+
//~^ WARN const_err
19+
println!("{}", 1/(1-1));
20+
//~^ WARN const_err
21+
//~| WARN const_err
22+
let _x = 1/(1-1);
23+
//~^ WARN const_err
24+
//~| WARN const_err
25+
println!("{}", 1/(false as u32));
26+
//~^ WARN const_err
27+
let _x = 1/(false as u32);
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
warning: constant evaluation error
2+
--> $DIR/promoted_errors.rs:14:20
3+
|
4+
LL | println!("{}", 0u32 - 1);
5+
| ^^^^^^^^ attempted to do overflowing math
6+
|
7+
= note: #[warn(const_err)] on by default
8+
9+
warning: constant evaluation error
10+
--> $DIR/promoted_errors.rs:14:20
11+
|
12+
LL | println!("{}", 0u32 - 1);
13+
| ^^^^^^^^ attempted to do overflowing math
14+
15+
warning: constant evaluation error
16+
--> $DIR/promoted_errors.rs:17:14
17+
|
18+
LL | let _x = 0u32 - 1;
19+
| ^^^^^^^^ attempted to do overflowing math
20+
21+
warning: attempt to divide by zero
22+
--> $DIR/promoted_errors.rs:19:20
23+
|
24+
LL | println!("{}", 1/(1-1));
25+
| ^^^^^^^
26+
27+
warning: constant evaluation error
28+
--> $DIR/promoted_errors.rs:19:20
29+
|
30+
LL | println!("{}", 1/(1-1));
31+
| ^^^^^^^ attempted to do overflowing math
32+
33+
warning: attempt to divide by zero
34+
--> $DIR/promoted_errors.rs:22:14
35+
|
36+
LL | let _x = 1/(1-1);
37+
| ^^^^^^^
38+
39+
warning: constant evaluation error
40+
--> $DIR/promoted_errors.rs:22:14
41+
|
42+
LL | let _x = 1/(1-1);
43+
| ^^^^^^^ attempted to do overflowing math
44+
45+
warning: constant evaluation error
46+
--> $DIR/promoted_errors.rs:25:20
47+
|
48+
LL | println!("{}", 1/(false as u32));
49+
| ^^^^^^^^^^^^^^^^ attempted to do overflowing math
50+

0 commit comments

Comments
 (0)