Skip to content

Commit 5b698a1

Browse files
committed
Unregress error spans in constant errors
1 parent 421622d commit 5b698a1

File tree

9 files changed

+26
-20
lines changed

9 files changed

+26
-20
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
@@ -789,7 +789,7 @@ impl<'a, 'tcx> PatternContext<'a, 'tcx> {
789789
let field = Field::new(i);
790790
let val = match cv.val {
791791
ConstVal::Value(miri) => const_val_field(
792-
self.tcx, self.param_env, instance, span,
792+
self.tcx, self.param_env, instance,
793793
variant_opt, field, miri, cv.ty,
794794
).unwrap(),
795795
_ => 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
@@ -1570,7 +1570,7 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
15701570
Ok(())
15711571
}
15721572

1573-
pub fn generate_stacktrace(&self, explicit_span: Option<Span>) -> Vec<FrameInfo> {
1573+
pub fn generate_stacktrace(&self, explicit_span: Option<Span>) -> (Vec<FrameInfo>, Span) {
15741574
let mut last_span = None;
15751575
let mut frames = Vec::new();
15761576
// skip 1 because the last frame is just the environment of the constant
@@ -1594,7 +1594,15 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
15941594
};
15951595
frames.push(FrameInfo { span, location });
15961596
}
1597-
frames
1597+
let frame = self.frame();
1598+
let bb = &frame.mir.basic_blocks()[frame.block];
1599+
let span = if let Some(stmt) = bb.statements.get(frame.stmt) {
1600+
stmt.source_info.span
1601+
} else {
1602+
bb.terminator().source_info.span
1603+
};
1604+
trace!("generate stacktrace: {:#?}, {:?}", frames, explicit_span);
1605+
(frames, span)
15981606
}
15991607

16001608
pub fn report(&self, e: &mut EvalError, as_err: bool, explicit_span: Option<Span>) {
@@ -1658,9 +1666,10 @@ impl<'a, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M
16581666
"constant evaluation error",
16591667
)
16601668
};
1669+
let (frames, span) = self.generate_stacktrace(explicit_span);
16611670
err.span_label(span, e.to_string());
1662-
for FrameInfo { span, location } in self.generate_stacktrace(explicit_span) {
1663-
err.span_note(span, &format!("inside call to {}", location));
1671+
for FrameInfo { span, location } in frames {
1672+
err.span_note(span, &format!("inside call to `{}`", location));
16641673
}
16651674
err.emit();
16661675
} 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/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-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/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)