From 0a4b53f57dc36e3f997a72161a0eb6426122e6bd Mon Sep 17 00:00:00 2001 From: Mara Bos Date: Fri, 9 Jul 2021 15:21:58 +0200 Subject: [PATCH] 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. --- .../rustc_mir/src/interpret/eval_context.rs | 13 +++++++++-- .../const-eval/const_panic_track_caller.rs | 23 +++++++++++++++++++ .../const_panic_track_caller.stderr | 15 ++++++++++++ src/test/ui/consts/const-unwrap.rs | 5 ++-- src/test/ui/consts/const-unwrap.stderr | 14 ++--------- 5 files changed, 53 insertions(+), 17 deletions(-) create mode 100644 src/test/ui/consts/const-eval/const_panic_track_caller.rs create mode 100644 src/test/ui/consts/const-eval/const_panic_track_caller.stderr diff --git a/compiler/rustc_mir/src/interpret/eval_context.rs b/compiler/rustc_mir/src/interpret/eval_context.rs index 227abeb7e7cc7..648a7abfdc7b1 100644 --- a/compiler/rustc_mir/src/interpret/eval_context.rs +++ b/compiler/rustc_mir/src/interpret/eval_context.rs @@ -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> { 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), diff --git a/src/test/ui/consts/const-eval/const_panic_track_caller.rs b/src/test/ui/consts/const-eval/const_panic_track_caller.rs new file mode 100644 index 0000000000000..7c2532673c835 --- /dev/null +++ b/src/test/ui/consts/const-eval/const_panic_track_caller.rs @@ -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 diff --git a/src/test/ui/consts/const-eval/const_panic_track_caller.stderr b/src/test/ui/consts/const-eval/const_panic_track_caller.stderr new file mode 100644 index 0000000000000..9a458db6ea24d --- /dev/null +++ b/src/test/ui/consts/const-eval/const_panic_track_caller.stderr @@ -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`. diff --git a/src/test/ui/consts/const-unwrap.rs b/src/test/ui/consts/const-unwrap.rs index 6ed60ed87bf76..729ae535ceff6 100644 --- a/src/test/ui/consts/const-unwrap.rs +++ b/src/test/ui/consts/const-unwrap.rs @@ -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::::None.unwrap(); //~ NOTE +const BAR: i32 = Option::::None.unwrap(); +//~^ERROR: evaluation of constant value failed fn main() { println!("{}", FOO); diff --git a/src/test/ui/consts/const-unwrap.stderr b/src/test/ui/consts/const-unwrap.stderr index 9a820ff721719..d2cbe4550f4bb 100644 --- a/src/test/ui/consts/const-unwrap.stderr +++ b/src/test/ui/consts/const-unwrap.stderr @@ -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::::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::::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