Skip to content

Commit 0ac6fd0

Browse files
committed
fix const_prop spans and re-bless tests
1 parent dc6ffae commit 0ac6fd0

14 files changed

+61
-52
lines changed

src/librustc_mir/const_eval/error.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::error::Error;
22
use std::fmt;
33

44
use rustc_middle::mir::AssertKind;
5-
use rustc_span::Symbol;
5+
use rustc_span::{Span, Symbol};
66

77
use super::InterpCx;
88
use crate::interpret::{ConstEvalErr, InterpErrorInfo, Machine};
@@ -53,8 +53,9 @@ impl Error for ConstEvalErrKind {}
5353
pub fn error_to_const_error<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>>(
5454
ecx: &InterpCx<'mir, 'tcx, M>,
5555
error: InterpErrorInfo<'tcx>,
56+
span: Option<Span>,
5657
) -> ConstEvalErr<'tcx> {
5758
error.print_backtrace();
5859
let stacktrace = ecx.generate_stacktrace();
59-
ConstEvalErr { error: error.kind, stacktrace, span: ecx.cur_span() }
60+
ConstEvalErr { error: error.kind, stacktrace, span: span.unwrap_or_else(|| ecx.cur_span()) }
6061
}

src/librustc_mir/const_eval/eval_queries.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ fn validate_and_turn_into_const<'tcx>(
213213
})();
214214

215215
val.map_err(|error| {
216-
let err = error_to_const_error(&ecx, error);
216+
let err = error_to_const_error(&ecx, error, None);
217217
err.struct_error(ecx.tcx_at(), "it is undefined behavior to use this value", |mut diag| {
218218
diag.note(note_on_undefined_behavior_error());
219219
diag.emit();
@@ -312,7 +312,7 @@ pub fn const_eval_raw_provider<'tcx>(
312312
res.and_then(|body| eval_body_using_ecx(&mut ecx, cid, &body))
313313
.map(|place| RawConst { alloc_id: place.ptr.assert_ptr().alloc_id, ty: place.layout.ty })
314314
.map_err(|error| {
315-
let err = error_to_const_error(&ecx, error);
315+
let err = error_to_const_error(&ecx, error, None);
316316
// errors in statics are always emitted as fatal errors
317317
if is_static {
318318
// Ensure that if the above error was either `TooGeneric` or `Reported`

src/librustc_mir/interpret/cast.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -268,11 +268,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
268268
(&ty::Array(_, length), &ty::Slice(_)) => {
269269
let ptr = self.read_immediate(src)?.to_scalar()?;
270270
// u64 cast is from usize to u64, which is always good
271-
let val = Immediate::new_slice(
272-
ptr,
273-
length.eval_usize(self.tcx, self.param_env),
274-
self,
275-
);
271+
let val =
272+
Immediate::new_slice(ptr, length.eval_usize(self.tcx, self.param_env), self);
276273
self.write_immediate(val, dest)
277274
}
278275
(&ty::Dynamic(..), &ty::Dynamic(..)) => {

src/librustc_mir/interpret/eval_context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
314314

315315
#[inline(always)]
316316
pub fn cur_span(&self) -> Span {
317-
self
318-
.stack()
317+
self.stack()
319318
.last()
320319
.and_then(|f| f.current_source_info())
321320
.map(|si| si.span)
@@ -419,7 +418,8 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
419418
let did = instance.def_id();
420419
if let Some(did) = did.as_local() {
421420
if self.tcx_at().has_typeck_tables(did) {
422-
if let Some(error_reported) = self.tcx_at().typeck_tables_of(did).tainted_by_errors {
421+
if let Some(error_reported) = self.tcx_at().typeck_tables_of(did).tainted_by_errors
422+
{
423423
throw_inval!(TypeckError(error_reported))
424424
}
425425
}

src/librustc_mir/interpret/place.rs

+20-7
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,10 @@ where
404404
// to get some code to work that probably ought to work.
405405
field_layout.align.abi
406406
}
407-
None => span_bug!(self.cur_span(), "cannot compute offset for extern type field at non-0 offset"),
407+
None => span_bug!(
408+
self.cur_span(),
409+
"cannot compute offset for extern type field at non-0 offset"
410+
),
408411
};
409412
(base.meta, offset.align_to(align))
410413
} else {
@@ -440,7 +443,11 @@ where
440443
assert!(!field_layout.is_unsized());
441444
base.offset(offset, MemPlaceMeta::None, field_layout, self)
442445
}
443-
_ => span_bug!(self.cur_span(), "`mplace_index` called on non-array type {:?}", base.layout.ty),
446+
_ => span_bug!(
447+
self.cur_span(),
448+
"`mplace_index` called on non-array type {:?}",
449+
base.layout.ty
450+
),
444451
}
445452
}
446453

@@ -484,7 +491,9 @@ where
484491
// (that have count 0 in their layout).
485492
let from_offset = match base.layout.fields {
486493
FieldsShape::Array { stride, .. } => stride * from, // `Size` multiplication is checked
487-
_ => span_bug!(self.cur_span(), "unexpected layout of index access: {:#?}", base.layout),
494+
_ => {
495+
span_bug!(self.cur_span(), "unexpected layout of index access: {:#?}", base.layout)
496+
}
488497
};
489498

490499
// Compute meta and new layout
@@ -497,7 +506,9 @@ where
497506
let len = Scalar::from_machine_usize(inner_len, self);
498507
(MemPlaceMeta::Meta(len), base.layout.ty)
499508
}
500-
_ => span_bug!(self.cur_span(), "cannot subslice non-array type: `{:?}`", base.layout.ty),
509+
_ => {
510+
span_bug!(self.cur_span(), "cannot subslice non-array type: `{:?}`", base.layout.ty)
511+
}
501512
};
502513
let layout = self.layout_of(ty)?;
503514
base.offset(from_offset, meta, layout, self)
@@ -776,9 +787,11 @@ where
776787
Immediate::Scalar(scalar) => {
777788
match dest.layout.abi {
778789
Abi::Scalar(_) => {} // fine
779-
_ => {
780-
span_bug!(self.cur_span(), "write_immediate_to_mplace: invalid Scalar layout: {:#?}", dest.layout)
781-
}
790+
_ => span_bug!(
791+
self.cur_span(),
792+
"write_immediate_to_mplace: invalid Scalar layout: {:#?}",
793+
dest.layout
794+
),
782795
}
783796
self.memory.get_raw_mut(ptr.alloc_id)?.write_scalar(
784797
&tcx,

src/librustc_mir/transform/const_prop.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
405405
Ok(op) => Some(op),
406406
Err(error) => {
407407
let tcx = self.ecx.tcx.at(c.span);
408-
let err = error_to_const_error(&self.ecx, error);
408+
let err = error_to_const_error(&self.ecx, error, Some(c.span));
409409
if let Some(lint_root) = self.lint_root(source_info) {
410410
let lint_only = match c.literal.val {
411411
// Promoteds must lint and not error as the user didn't ask for them
@@ -417,12 +417,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
417417
if lint_only {
418418
// Out of backwards compatibility we cannot report hard errors in unused
419419
// generic functions using associated constants of the generic parameters.
420-
err.report_as_lint(
421-
tcx,
422-
"erroneous constant used",
423-
lint_root,
424-
Some(c.span),
425-
);
420+
err.report_as_lint(tcx, "erroneous constant used", lint_root, Some(c.span));
426421
} else {
427422
err.report_as_error(tcx, "erroneous constant used");
428423
}

src/test/ui/consts/const-eval/infinite_loop.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ LL | n = if n % 2 == 0 { n/2 } else { 3*n + 1 };
2323
= help: add `#![feature(const_if_match)]` to the crate attributes to enable
2424

2525
error[E0080]: evaluation of constant value failed
26-
--> $DIR/infinite_loop.rs:8:20
26+
--> $DIR/infinite_loop.rs:8:17
2727
|
2828
LL | n = if n % 2 == 0 { n/2 } else { 3*n + 1 };
29-
| ^^^^^^^^^^ exceeded interpreter step limit (see `#[const_eval_limit]`)
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ exceeded interpreter step limit (see `#[const_eval_limit]`)
3030

3131
error: aborting due to 3 previous errors
3232

src/test/ui/consts/const_limit/const_eval_limit_reached.stderr

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
error: any use of this value will cause an error
2-
--> $DIR/const_eval_limit_reached.rs:8:11
2+
--> $DIR/const_eval_limit_reached.rs:8:5
33
|
4-
LL | / const X: usize = {
5-
LL | | let mut x = 0;
6-
LL | | while x != 1000 {
7-
| | ^^^^^^^^^ exceeded interpreter step limit (see `#[const_eval_limit]`)
8-
LL | |
9-
... |
10-
LL | | x
11-
LL | | };
12-
| |__-
4+
LL | / const X: usize = {
5+
LL | | let mut x = 0;
6+
LL | | while x != 1000 {
7+
| |_____^
8+
LL | ||
9+
LL | || x += 1;
10+
LL | || }
11+
| ||_____^ exceeded interpreter step limit (see `#[const_eval_limit]`)
12+
LL | |
13+
LL | | x
14+
LL | | };
15+
| |__-
1316
|
1417
= note: `#[deny(const_err)]` on by default
1518

src/test/ui/consts/recursive-zst-static.default.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0391]: cycle detected when const-evaluating `FOO`
2-
--> $DIR/recursive-zst-static.rs:10:18
2+
--> $DIR/recursive-zst-static.rs:10:1
33
|
44
LL | static FOO: () = FOO;
5-
| ^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: ...which requires const-evaluating `FOO`...
88
--> $DIR/recursive-zst-static.rs:10:1

src/test/ui/consts/recursive-zst-static.unleash.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0391]: cycle detected when const-evaluating `FOO`
2-
--> $DIR/recursive-zst-static.rs:10:18
2+
--> $DIR/recursive-zst-static.rs:10:1
33
|
44
LL | static FOO: () = FOO;
5-
| ^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: ...which requires const-evaluating `FOO`...
88
--> $DIR/recursive-zst-static.rs:10:1

src/test/ui/consts/uninhabited-const-issue-61744.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
// build-fail
22

33
pub const unsafe fn fake_type<T>() -> T {
4-
hint_unreachable()
4+
hint_unreachable() //~ ERROR evaluation of constant value failed
55
}
66

77
pub const unsafe fn hint_unreachable() -> ! {
8-
fake_type() //~ ERROR evaluation of constant value failed
8+
fake_type()
99
}
1010

1111
trait Const {

src/test/ui/consts/uninhabited-const-issue-61744.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $DIR/uninhabited-const-issue-61744.rs:8:5
2+
--> $DIR/uninhabited-const-issue-61744.rs:4:5
33
|
44
LL | hint_unreachable()
5-
| ------------------
5+
| ^^^^^^^^^^^^^^^^^^
66
| |
7+
| reached the configured maximum number of stack frames
78
| inside `fake_type::<!>` at $DIR/uninhabited-const-issue-61744.rs:4:5
89
| inside `fake_type::<!>` at $DIR/uninhabited-const-issue-61744.rs:4:5
910
| inside `fake_type::<!>` at $DIR/uninhabited-const-issue-61744.rs:4:5
@@ -71,9 +72,8 @@ LL | hint_unreachable()
7172
| inside `fake_type::<i32>` at $DIR/uninhabited-const-issue-61744.rs:4:5
7273
...
7374
LL | fake_type()
74-
| ^^^^^^^^^^^
75+
| -----------
7576
| |
76-
| reached the configured maximum number of stack frames
7777
| inside `hint_unreachable` at $DIR/uninhabited-const-issue-61744.rs:8:5
7878
| inside `hint_unreachable` at $DIR/uninhabited-const-issue-61744.rs:8:5
7979
| inside `hint_unreachable` at $DIR/uninhabited-const-issue-61744.rs:8:5

src/test/ui/recursion/recursive-static-definition.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0391]: cycle detected when const-evaluating `FOO`
2-
--> $DIR/recursive-static-definition.rs:1:23
2+
--> $DIR/recursive-static-definition.rs:1:1
33
|
44
LL | pub static FOO: u32 = FOO;
5-
| ^^^
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: ...which requires const-evaluating `FOO`...
88
--> $DIR/recursive-static-definition.rs:1:1

src/test/ui/write-to-static-mut-in-static.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | pub static mut B: () = unsafe { A = 1; };
55
| ^^^^^ modifying a static's initial value from another static's initializer
66

77
error[E0391]: cycle detected when const-evaluating `C`
8-
--> $DIR/write-to-static-mut-in-static.rs:5:34
8+
--> $DIR/write-to-static-mut-in-static.rs:5:1
99
|
1010
LL | pub static mut C: u32 = unsafe { C = 1; 0 };
11-
| ^^^^^
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212
|
1313
note: ...which requires const-evaluating `C`...
1414
--> $DIR/write-to-static-mut-in-static.rs:5:1

0 commit comments

Comments
 (0)