Skip to content

Commit 0b2329d

Browse files
committed
also make panic_fmt track_caller
1 parent f34e066 commit 0b2329d

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

src/libcore/macros/mod.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[cfg(bootstrap)]
12
#[doc(include = "panic.md")]
23
#[macro_export]
34
#[allow_internal_unstable(core_panic, track_caller)]
@@ -20,6 +21,26 @@ macro_rules! panic {
2021
);
2122
}
2223

24+
#[cfg(not(bootstrap))]
25+
#[doc(include = "panic.md")]
26+
#[macro_export]
27+
#[allow_internal_unstable(core_panic, track_caller)]
28+
#[stable(feature = "core", since = "1.6.0")]
29+
macro_rules! panic {
30+
() => (
31+
$crate::panic!("explicit panic")
32+
);
33+
($msg:expr) => (
34+
$crate::panicking::panic($msg)
35+
);
36+
($msg:expr,) => (
37+
$crate::panic!($msg)
38+
);
39+
($fmt:expr, $($arg:tt)+) => (
40+
$crate::panicking::panic_fmt($crate::format_args!($fmt, $($arg)+))
41+
);
42+
}
43+
2344
/// Asserts that two expressions are equal to each other (using [`PartialEq`]).
2445
///
2546
/// On panic, this macro will print the values of the expressions with their

src/libcore/panicking.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use crate::fmt;
3333
use crate::panic::{Location, PanicInfo};
3434

35+
/// The underlying implementation of libcore's `panic!` macro when no formatting is used.
3536
#[cold]
3637
// never inline unless panic_immediate_abort to avoid code
3738
// bloat at the call sites as much as possible
@@ -49,7 +50,10 @@ pub fn panic(expr: &str) -> ! {
4950
// truncation and padding (even though none is used here). Using
5051
// Arguments::new_v1 may allow the compiler to omit Formatter::pad from the
5152
// output binary, saving up to a few kilobytes.
52-
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), Location::caller())
53+
#[cfg(not(bootstrap))]
54+
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]));
55+
#[cfg(bootstrap)]
56+
panic_fmt(fmt::Arguments::new_v1(&[expr], &[]), Location::caller());
5357
}
5458

5559
#[cfg(not(bootstrap))]
@@ -62,13 +66,11 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
6266
unsafe { super::intrinsics::abort() }
6367
}
6468

65-
panic_fmt(
66-
format_args!("index out of bounds: the len is {} but the index is {}", len, index),
67-
Location::caller(),
68-
)
69+
panic!("index out of bounds: the len is {} but the index is {}", len, index)
6970
}
7071

71-
// For bootstrap, we need a variant with the old argument order.
72+
// For bootstrap, we need a variant with the old argument order, and a corresponding
73+
// `panic_fmt`.
7274
#[cfg(bootstrap)]
7375
#[cold]
7476
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
@@ -84,10 +86,12 @@ fn panic_bounds_check(location: &Location<'_>, index: usize, len: usize) -> ! {
8486
)
8587
}
8688

89+
/// The underlying implementation of libcore's `panic!` macro when formatting is used.
8790
#[cold]
8891
#[cfg_attr(not(feature = "panic_immediate_abort"), inline(never))]
8992
#[cfg_attr(feature = "panic_immediate_abort", inline)]
90-
pub fn panic_fmt(fmt: fmt::Arguments<'_>, location: &Location<'_>) -> ! {
93+
#[cfg_attr(not(bootstrap), track_caller)]
94+
pub fn panic_fmt(fmt: fmt::Arguments<'_>, #[cfg(bootstrap)] location: &Location<'_>) -> ! {
9195
if cfg!(feature = "panic_immediate_abort") {
9296
unsafe { super::intrinsics::abort() }
9397
}
@@ -99,6 +103,10 @@ pub fn panic_fmt(fmt: fmt::Arguments<'_>, location: &Location<'_>) -> ! {
99103
fn panic_impl(pi: &PanicInfo<'_>) -> !;
100104
}
101105

106+
#[cfg(bootstrap)]
102107
let pi = PanicInfo::internal_constructor(Some(&fmt), location);
108+
#[cfg(not(bootstrap))]
109+
let pi = PanicInfo::internal_constructor(Some(&fmt), Location::caller());
110+
103111
unsafe { panic_impl(&pi) }
104112
}

0 commit comments

Comments
 (0)