Skip to content

Commit 2152c14

Browse files
authored
Rollup merge of #87000 - m-ou-se:const-panic-track-caller, r=oli-obk
Use #[track_caller] in const panic diagnostics. This change stops const panic diagnostics from reporting inside #[track_caller] functions by skipping over them.
2 parents 98f3558 + 0a4b53f commit 2152c14

File tree

5 files changed

+53
-17
lines changed

5 files changed

+53
-17
lines changed

compiler/rustc_mir/src/interpret/eval_context.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
398398

399399
#[inline(always)]
400400
pub fn cur_span(&self) -> Span {
401-
self.stack().last().map_or(self.tcx.span, |f| f.current_span())
401+
self.stack()
402+
.iter()
403+
.rev()
404+
.find(|frame| !frame.instance.def.requires_caller_location(*self.tcx))
405+
.map_or(self.tcx.span, |f| f.current_span())
402406
}
403407

404408
#[inline(always)]
@@ -927,7 +931,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
927931
#[must_use]
928932
pub fn generate_stacktrace(&self) -> Vec<FrameInfo<'tcx>> {
929933
let mut frames = Vec::new();
930-
for frame in self.stack().iter().rev() {
934+
for frame in self
935+
.stack()
936+
.iter()
937+
.rev()
938+
.skip_while(|frame| frame.instance.def.requires_caller_location(*self.tcx))
939+
{
931940
let lint_root = frame.current_source_info().and_then(|source_info| {
932941
match &frame.body.source_scopes[source_info.scope].local_data {
933942
mir::ClearCrossCrate::Set(data) => Some(data.lint_root),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#![feature(const_panic)]
2+
#![allow(non_fmt_panics)]
3+
#![crate_type = "lib"]
4+
5+
#[track_caller]
6+
const fn a() -> u32 {
7+
panic!("hey")
8+
}
9+
10+
#[track_caller]
11+
const fn b() -> u32 {
12+
a()
13+
}
14+
15+
const fn c() -> u32 {
16+
b()
17+
//~^ ERROR evaluation of constant value failed
18+
//~| NOTE the evaluated program panicked
19+
//~| NOTE inside
20+
}
21+
22+
const X: u32 = c();
23+
//~^ NOTE inside
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0080]: evaluation of constant value failed
2+
--> $DIR/const_panic_track_caller.rs:16:5
3+
|
4+
LL | b()
5+
| ^^^
6+
| |
7+
| the evaluated program panicked at 'hey', $DIR/const_panic_track_caller.rs:16:5
8+
| inside `c` at $DIR/const_panic_track_caller.rs:16:5
9+
...
10+
LL | const X: u32 = c();
11+
| --- inside `X` at $DIR/const_panic_track_caller.rs:22:16
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0080`.

src/test/ui/consts/const-unwrap.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44

55
const FOO: i32 = Some(42i32).unwrap();
66

7-
// This causes an error, but it is attributed to the `panic` *inside* `Option::unwrap` (maybe due
8-
// to `track_caller`?). A note points to the originating `const`.
9-
const BAR: i32 = Option::<i32>::None.unwrap(); //~ NOTE
7+
const BAR: i32 = Option::<i32>::None.unwrap();
8+
//~^ERROR: evaluation of constant value failed
109

1110
fn main() {
1211
println!("{}", FOO);

src/test/ui/consts/const-unwrap.stderr

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
error[E0080]: evaluation of constant value failed
2-
--> $SRC_DIR/core/src/option.rs:LL:COL
3-
|
4-
LL | None => panic!("called `Option::unwrap()` on a `None` value"),
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6-
| |
7-
| the evaluated program panicked at 'called `Option::unwrap()` on a `None` value', $DIR/const-unwrap.rs:9:38
8-
| inside `Option::<i32>::unwrap` at $SRC_DIR/core/src/panic.rs:LL:COL
9-
|
10-
::: $DIR/const-unwrap.rs:9:18
2+
--> $DIR/const-unwrap.rs:7:18
113
|
124
LL | const BAR: i32 = Option::<i32>::None.unwrap();
13-
| ---------------------------- inside `BAR` at $DIR/const-unwrap.rs:9:18
14-
|
15-
= note: this error originates in the macro `$crate::panic::panic_2015` (in Nightly builds, run with -Z macro-backtrace for more info)
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'called `Option::unwrap()` on a `None` value', $DIR/const-unwrap.rs:7:38
166

177
error: aborting due to previous error
188

0 commit comments

Comments
 (0)