Skip to content

Commit 12255d6

Browse files
committed
Unregress error spans in constant errors
1 parent d9277b9 commit 12255d6

17 files changed

+112
-126
lines changed

src/librustc/middle/const_val.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl<'a, 'gcx, 'tcx> ConstEvalErr<'tcx> {
155155
ConstEvalErrDescription::Backtrace(miri, frames) => {
156156
diag.span_label(self.span, format!("{}", miri));
157157
for frame in frames {
158-
diag.span_label(frame.span, format!("inside call to {}", frame.location));
158+
diag.span_label(frame.span, format!("inside call to `{}`", frame.location));
159159
}
160160
}
161161
}

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
788788
let field = Field::new(i);
789789
let val = match cv.val {
790790
ConstVal::Value(miri) => const_val_field(
791-
self.tcx, self.param_env, instance, span,
791+
self.tcx, self.param_env, instance,
792792
variant_opt, field, miri, cv.ty,
793793
).unwrap(),
794794
_ => bug!("{:#?} is not a valid adt", cv),

src/librustc_mir/interpret/const_eval.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn eval_body_with_mir<'a, 'mir, 'tcx>(
6161
mir: &'mir mir::Mir<'tcx>,
6262
param_env: ty::ParamEnv<'tcx>,
6363
) -> Option<(Value, Pointer, Ty<'tcx>)> {
64-
let (res, ecx, _) = eval_body_and_ecx(tcx, cid, Some(mir), param_env);
64+
let (res, ecx) = eval_body_and_ecx(tcx, cid, Some(mir), param_env);
6565
match res {
6666
Ok(val) => Some(val),
6767
Err(mut err) => {
@@ -76,7 +76,7 @@ pub fn eval_body<'a, 'tcx>(
7676
cid: GlobalId<'tcx>,
7777
param_env: ty::ParamEnv<'tcx>,
7878
) -> Option<(Value, Pointer, Ty<'tcx>)> {
79-
let (res, ecx, _) = eval_body_and_ecx(tcx, cid, None, param_env);
79+
let (res, ecx) = eval_body_and_ecx(tcx, cid, None, param_env);
8080
match res {
8181
Ok(val) => Some(val),
8282
Err(mut err) => {
@@ -91,7 +91,7 @@ fn eval_body_and_ecx<'a, 'mir, 'tcx>(
9191
cid: GlobalId<'tcx>,
9292
mir: Option<&'mir mir::Mir<'tcx>>,
9393
param_env: ty::ParamEnv<'tcx>,
94-
) -> (EvalResult<'tcx, (Value, Pointer, Ty<'tcx>)>, EvalContext<'a, 'mir, 'tcx, CompileTimeEvaluator>, Span) {
94+
) -> (EvalResult<'tcx, (Value, Pointer, Ty<'tcx>)>, EvalContext<'a, 'mir, 'tcx, CompileTimeEvaluator>) {
9595
debug!("eval_body: {:?}, {:?}", cid, param_env);
9696
let mut ecx = EvalContext::new(tcx, param_env, CompileTimeEvaluator, ());
9797
// we start out with the best span we have
@@ -155,7 +155,7 @@ fn eval_body_and_ecx<'a, 'mir, 'tcx>(
155155
};
156156
Ok((value, ptr, layout.ty))
157157
})();
158-
(res, ecx, span)
158+
(res, ecx)
159159
}
160160

161161
pub struct CompileTimeEvaluator;
@@ -367,7 +367,6 @@ pub fn const_val_field<'a, 'tcx>(
367367
tcx: TyCtxt<'a, 'tcx, 'tcx>,
368368
param_env: ty::ParamEnv<'tcx>,
369369
instance: ty::Instance<'tcx>,
370-
span: Span,
371370
variant: Option<usize>,
372371
field: mir::Field,
373372
value: Value,
@@ -403,7 +402,7 @@ pub fn const_val_field<'a, 'tcx>(
403402
ty,
404403
})),
405404
Err(err) => {
406-
let trace = ecx.generate_stacktrace(None);
405+
let (trace, span) = ecx.generate_stacktrace(None);
407406
let err = ErrKind::Miri(err, trace);
408407
Err(ConstEvalErr {
409408
kind: err.into(),
@@ -490,7 +489,7 @@ pub fn const_eval_provider<'a, 'tcx>(
490489
}
491490
};
492491

493-
let (res, ecx, span) = eval_body_and_ecx(tcx, cid, None, key.param_env);
492+
let (res, ecx) = eval_body_and_ecx(tcx, cid, None, key.param_env);
494493
res.map(|(miri_value, _, miri_ty)| {
495494
tcx.mk_const(ty::Const {
496495
val: ConstVal::Value(miri_value),
@@ -500,7 +499,7 @@ pub fn const_eval_provider<'a, 'tcx>(
500499
if tcx.is_static(def_id).is_some() {
501500
ecx.report(&mut err, true, None);
502501
}
503-
let trace = ecx.generate_stacktrace(None);
502+
let (trace, span) = ecx.generate_stacktrace(None);
504503
let err = ErrKind::Miri(err, trace);
505504
ConstEvalErr {
506505
kind: err.into(),

src/librustc_mir/interpret/eval_context.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,7 +1566,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
15661566
Ok(())
15671567
}
15681568

1569-
pub fn generate_stacktrace(&self, explicit_span: Option<Span>) -> Vec<FrameInfo> {
1569+
pub fn generate_stacktrace(&self, explicit_span: Option<Span>) -> (Vec<FrameInfo>, Span) {
15701570
let mut last_span = None;
15711571
let mut frames = Vec::new();
15721572
// skip 1 because the last frame is just the environment of the constant
@@ -1590,7 +1590,15 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
15901590
};
15911591
frames.push(FrameInfo { span, location });
15921592
}
1593-
frames
1593+
let frame = self.frame();
1594+
let bb = &frame.mir.basic_blocks()[frame.block];
1595+
let span = if let Some(stmt) = bb.statements.get(frame.stmt) {
1596+
stmt.source_info.span
1597+
} else {
1598+
bb.terminator().source_info.span
1599+
};
1600+
trace!("generate stacktrace: {:#?}, {:?}", frames, explicit_span);
1601+
(frames, span)
15941602
}
15951603

15961604
pub fn report(&self, e: &mut EvalError, as_err: bool, explicit_span: Option<Span>) {
@@ -1654,9 +1662,10 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
16541662
"constant evaluation error",
16551663
)
16561664
};
1665+
let (frames, span) = self.generate_stacktrace(explicit_span);
16571666
err.span_label(span, e.to_string());
1658-
for FrameInfo { span, location } in self.generate_stacktrace(explicit_span) {
1659-
err.span_note(span, &format!("inside call to {}", location));
1667+
for FrameInfo { span, location } in frames {
1668+
err.span_note(span, &format!("inside call to `{}`", location));
16601669
}
16611670
err.emit();
16621671
} else {

src/librustc_mir/transform/const_prop.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ impl<'b, 'a, 'tcx:'b> ConstPropagator<'b, 'a, 'tcx> {
8080
let value = match self.tcx.const_eval(self.param_env.and(cid)) {
8181
Ok(val) => val,
8282
Err(err) => {
83-
err.report(self.tcx, span, "constant propagated");
83+
err.report(self.tcx, err.span, "constant propagated");
8484
return None;
8585
},
8686
};

src/librustc_trans/mir/constant.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,6 @@ impl<'a, 'tcx> FunctionCx<'a, 'tcx> {
203203
bx.tcx(),
204204
ty::ParamEnv::empty(traits::Reveal::All),
205205
self.instance,
206-
constant.span,
207206
None,
208207
mir::Field::new(field as usize),
209208
c,

src/test/ui/const-eval-overflow-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0080]: constant evaluation error
2-
--> $DIR/const-eval-overflow-2.rs:21:1
2+
--> $DIR/const-eval-overflow-2.rs:21:25
33
|
44
21 | const NEG_NEG_128: i8 = -NEG_128;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to negate with overflow
5+
| ^^^^^^^^ attempt to negate with overflow
66
|
77
note: for pattern here
88
--> $DIR/const-eval-overflow-2.rs:26:9

src/test/ui/const-eval-overflow-4.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: constant evaluation error
22
--> $DIR/const-eval-overflow-4.rs:23:13
33
|
44
23 | : [u32; (i8::MAX as i8 + 1i8) as usize]
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to add with overflow
5+
| ^^^^^^^^^^^^^^^^^^^^^ attempt to add with overflow
66

77
error: aborting due to previous error
88

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,10 @@ error[E0080]: constant evaluation error
55
| ^^^ referenced constant has errors
66

77
error[E0080]: constant evaluation error
8-
--> $DIR/conditional_array_execution.rs:13:1
8+
--> $DIR/conditional_array_execution.rs:13:19
99
|
1010
13 | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize]; //~ E0080
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
12-
|
13-
note: for constant propagated here
14-
--> $DIR/conditional_array_execution.rs:16:20
15-
|
16-
16 | println!("{}", FOO); //~ E0080
17-
| ^^^
11+
| ^^^^^ attempt to subtract with overflow
1812

1913
error: aborting due to 2 previous errors
2014

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,5 @@
1010

1111
static FOO: i32 = [][0];
1212
//~^ ERROR E0080
13-
//~| ERROR E0080
1413

1514
fn main() {}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,5 @@ error[E0080]: constant evaluation error
44
11 | static FOO: i32 = [][0];
55
| ^^^^^ index out of bounds: the len is 0 but the index is 0 at $DIR/index_out_of_bound.rs:11:19: 11:24
66

7-
error[E0080]: constant evaluation error
8-
--> $DIR/index_out_of_bound.rs:11:1
9-
|
10-
11 | static FOO: i32 = [][0];
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^ index out of bounds: the len is 0 but the index is 0 at $DIR/index_out_of_bound.rs:11:19: 11:24
12-
13-
error: aborting due to 2 previous errors
7+
error: aborting due to previous error
148

src/test/ui/const-eval/issue-43197.stderr

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,16 @@ error[E0080]: constant evaluation error
1111
| ^ referenced constant has errors
1212

1313
error[E0080]: constant evaluation error
14-
--> $DIR/issue-43197.rs:19:5
14+
--> $DIR/issue-43197.rs:19:24
1515
|
1616
19 | const Y: u32 = foo(0-1); //~ ERROR constant evaluation error
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
18-
|
19-
note: for constant propagated here
20-
--> $DIR/issue-43197.rs:20:26
21-
|
22-
20 | println!("{} {}", X, Y);
23-
| ^
17+
| ^^^ attempt to subtract with overflow
2418

2519
error[E0080]: constant evaluation error
26-
--> $DIR/issue-43197.rs:18:5
20+
--> $DIR/issue-43197.rs:18:20
2721
|
2822
18 | const X: u32 = 0-1; //~ ERROR constant evaluation error
29-
| ^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
30-
|
31-
note: for constant propagated here
32-
--> $DIR/issue-43197.rs:20:23
33-
|
34-
20 | println!("{} {}", X, Y);
35-
| ^
23+
| ^^^ attempt to subtract with overflow
3624

3725
error: aborting due to 4 previous errors
3826

src/test/ui/const-fn-error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const fn f(x: usize) -> usize {
1818
for i in 0..x {
1919
//~^ ERROR E0015
2020
//~| ERROR E0019
21+
//~| ERROR E0080
2122
sum += i;
2223
}
2324
sum
@@ -26,5 +27,4 @@ const fn f(x: usize) -> usize {
2627
#[allow(unused_variables)]
2728
fn main() {
2829
let a : [i32; f(X)];
29-
//~^ ERROR E0080
3030
}

src/test/ui/const-fn-error.stderr

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@ error[E0019]: constant function contains unimplemented expression type
1717
| ^^^^
1818

1919
error[E0080]: constant evaluation error
20-
--> $DIR/const-fn-error.rs:28:19
20+
--> $DIR/const-fn-error.rs:18:14
21+
|
22+
18 | for i in 0..x {
23+
| ^^^^ calling non-const fn `<I as std::iter::IntoIterator><std::ops::Range<usize>>::into_iter`
24+
...
25+
29 | let a : [i32; f(X)];
26+
| ---- inside call to `f`
27+
|
28+
note: for constant expression here
29+
--> $DIR/const-fn-error.rs:29:13
2130
|
22-
28 | let a : [i32; f(X)];
23-
| ^^^^
24-
| |
25-
| calling non-const fn `<I as std::iter::IntoIterator><std::ops::Range<usize>>::into_iter`
26-
| inside call to f
31+
29 | let a : [i32; f(X)];
32+
| ^^^^^^^^^^^
2733

2834
error: aborting due to 4 previous errors
2935

src/test/ui/const-len-underflow-separate-spans.stderr

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
11
error[E0080]: constant evaluation error
2-
--> $DIR/const-len-underflow-separate-spans.rs:17:1
2+
--> $DIR/const-len-underflow-separate-spans.rs:17:20
33
|
44
17 | const LEN: usize = ONE - TWO;
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ attempt to subtract with overflow
6-
|
7-
note: for constant propagated here
8-
--> $DIR/const-len-underflow-separate-spans.rs:21:17
9-
|
10-
21 | let a: [i8; LEN] = unimplemented!();
11-
| ^^^
5+
| ^^^^^^^^^ attempt to subtract with overflow
126

137
error[E0080]: constant evaluation error
148
--> $DIR/const-len-underflow-separate-spans.rs:21:17

src/test/ui/infinite-recursion-const-fn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#![feature(const_fn)]
1414
const fn a() -> usize { b() }
15-
const fn b() -> usize { a() }
16-
const ARR: [i32; a()] = [5; 6]; //~ ERROR constant evaluation error
15+
const fn b() -> usize { a() } //~ ERROR constant evaluation error
16+
const ARR: [i32; a()] = [5; 6];
1717

1818
fn main(){}

0 commit comments

Comments
 (0)