Use #[track_caller] in const panic diagnostics.

It was already used for the message. This also uses it for the spans
used for the error and backtrace.
This commit is contained in:
Mara Bos 2021-07-09 15:21:58 +02:00
parent 95fb131521
commit 0a4b53f57d
5 changed files with 53 additions and 17 deletions

View File

@ -398,7 +398,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
#[inline(always)]
pub fn cur_span(&self) -> Span {
self.stack().last().map_or(self.tcx.span, |f| f.current_span())
self.stack()
.iter()
.rev()
.find(|frame| !frame.instance.def.requires_caller_location(*self.tcx))
.map_or(self.tcx.span, |f| f.current_span())
}
#[inline(always)]
@ -927,7 +931,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
#[must_use]
pub fn generate_stacktrace(&self) -> Vec<FrameInfo<'tcx>> {
let mut frames = Vec::new();
for frame in self.stack().iter().rev() {
for frame in self
.stack()
.iter()
.rev()
.skip_while(|frame| frame.instance.def.requires_caller_location(*self.tcx))
{
let lint_root = frame.current_source_info().and_then(|source_info| {
match &frame.body.source_scopes[source_info.scope].local_data {
mir::ClearCrossCrate::Set(data) => Some(data.lint_root),

View File

@ -0,0 +1,23 @@
#![feature(const_panic)]
#![allow(non_fmt_panics)]
#![crate_type = "lib"]
#[track_caller]
const fn a() -> u32 {
panic!("hey")
}
#[track_caller]
const fn b() -> u32 {
a()
}
const fn c() -> u32 {
b()
//~^ ERROR evaluation of constant value failed
//~| NOTE the evaluated program panicked
//~| NOTE inside
}
const X: u32 = c();
//~^ NOTE inside

View File

@ -0,0 +1,15 @@
error[E0080]: evaluation of constant value failed
--> $DIR/const_panic_track_caller.rs:16:5
|
LL | b()
| ^^^
| |
| the evaluated program panicked at 'hey', $DIR/const_panic_track_caller.rs:16:5
| inside `c` at $DIR/const_panic_track_caller.rs:16:5
...
LL | const X: u32 = c();
| --- inside `X` at $DIR/const_panic_track_caller.rs:22:16
error: aborting due to previous error
For more information about this error, try `rustc --explain E0080`.

View File

@ -4,9 +4,8 @@
const FOO: i32 = Some(42i32).unwrap();
// This causes an error, but it is attributed to the `panic` *inside* `Option::unwrap` (maybe due
// to `track_caller`?). A note points to the originating `const`.
const BAR: i32 = Option::<i32>::None.unwrap(); //~ NOTE
const BAR: i32 = Option::<i32>::None.unwrap();
//~^ERROR: evaluation of constant value failed
fn main() {
println!("{}", FOO);

View File

@ -1,18 +1,8 @@
error[E0080]: evaluation of constant value failed
--> $SRC_DIR/core/src/option.rs:LL:COL
|
LL | None => panic!("called `Option::unwrap()` on a `None` value"),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| the evaluated program panicked at 'called `Option::unwrap()` on a `None` value', $DIR/const-unwrap.rs:9:38
| inside `Option::<i32>::unwrap` at $SRC_DIR/core/src/panic.rs:LL:COL
|
::: $DIR/const-unwrap.rs:9:18
--> $DIR/const-unwrap.rs:7:18
|
LL | const BAR: i32 = Option::<i32>::None.unwrap();
| ---------------------------- inside `BAR` at $DIR/const-unwrap.rs:9:18
|
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'called `Option::unwrap()` on a `None` value', $DIR/const-unwrap.rs:7:38
error: aborting due to previous error