Skip to content

Commit 12255d6

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

17 files changed

Lines changed: 112 additions & 126 deletions

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() {}

0 commit comments

Comments
 (0)