Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use #[track_caller] in const panic diagnostics. #87000

Merged
merged 1 commit into from
Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions compiler/rustc_mir/src/interpret/eval_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function at least used to be hot, we should have done a perf run... oh well.

}

#[inline(always)]
Expand Down Expand Up @@ -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))
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK this is also used for Miri error messages... do we really want those backtraces to skip frames? That sounds rather confusing. When we get a panic at runtime, with RUST_BACKTRACE we also show the full trace, not just the non-caller-location frames, right?

Copy link
Member

@RalfJung RalfJung Jul 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even during CTFE, while this makes sense for panics, note that this logic is used for all CTFE errors. I am not sure if that's always what we want.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh.. right. we should limit this to panics

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out this is also the source of some Miri panics in our diagnostics code which didn't expect pruned backtraces.

If we want this kind of pruning, we should not to it in the lowest-level generate_stacktrace function.

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),
Expand Down
23 changes: 23 additions & 0 deletions src/test/ui/consts/const-eval/const_panic_track_caller.rs
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions src/test/ui/consts/const-eval/const_panic_track_caller.stderr
Original file line number Diff line number Diff line change
@@ -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`.
5 changes: 2 additions & 3 deletions src/test/ui/consts/const-unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 2 additions & 12 deletions src/test/ui/consts/const-unwrap.stderr
Original file line number Diff line number Diff line change
@@ -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

Expand Down