Skip to content

Commit d77ab4f

Browse files
committed
Auto merge of #146561 - ijchen:panic_location_static, r=Mark-Simulacrum
Change `Location<'_>` lifetime to `'static` in `Panic[Hook]Info` I'm writing a library that would benefit from being able to store a `&'static str` instead of a `String` for the file location of a panic. Currently, the API of [`std::panic::PanicHookInfo::location`](https://doc.rust-lang.org/nightly/std/panic/struct.PanicHookInfo.html#method.location) and [`core::panic::PanicInfo::location`](https://doc.rust-lang.org/nightly/core/panic/struct.PanicInfo.html#method.location) return a `Location<'_>` (and by extension, a file name `&str`) whose lifetime is tied to the borrow of the `Panic[Hook]Info`. It is my understanding that the `Location`/`&str` will always be `Location<'static>`/`&'static str`, since the file name is embedded directly into the compiled binary (and I don't see a likely reason/way for that to ever change in the future). Since it seems unlikely to ever need to change, and since making the guarantee that the returned lifetime is `'static` has a real benefit (allows users to avoid unnecessary allocations), I think changing to the returned `Location<'_>`'s lifetime to `'static` would be a worthwhile change. see also the recent [#1320870](#132087), which made a similar change to [`std::panic::Location`](https://doc.rust-lang.org/nightly/std/panic/struct.Location.html)
2 parents 2574810 + e3b8df1 commit d77ab4f

3 files changed

Lines changed: 9 additions & 9 deletions

File tree

library/core/src/panic/panic_info.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::panic::Location;
1313
#[derive(Debug)]
1414
pub struct PanicInfo<'a> {
1515
message: &'a fmt::Arguments<'a>,
16-
location: &'a Location<'a>,
16+
location: &'static Location<'static>,
1717
can_unwind: bool,
1818
force_no_backtrace: bool,
1919
}
@@ -33,7 +33,7 @@ impl<'a> PanicInfo<'a> {
3333
#[inline]
3434
pub(crate) fn new(
3535
message: &'a fmt::Arguments<'a>,
36-
location: &'a Location<'a>,
36+
location: &'static Location<'static>,
3737
can_unwind: bool,
3838
force_no_backtrace: bool,
3939
) -> Self {
@@ -88,10 +88,10 @@ impl<'a> PanicInfo<'a> {
8888
/// ```
8989
#[must_use]
9090
#[stable(feature = "panic_hooks", since = "1.10.0")]
91-
pub fn location(&self) -> Option<&Location<'_>> {
91+
pub fn location(&self) -> Option<&'static Location<'static>> {
9292
// NOTE: If this is changed to sometimes return None,
9393
// deal with that case in std::panicking::default_hook and core::panicking::panic_fmt.
94-
Some(&self.location)
94+
Some(self.location)
9595
}
9696

9797
/// Returns the payload associated with the panic.

library/std/src/panic.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ pub type PanicInfo<'a> = PanicHookInfo<'a>;
4141
#[derive(Debug)]
4242
pub struct PanicHookInfo<'a> {
4343
payload: &'a (dyn Any + Send),
44-
location: &'a Location<'a>,
44+
location: &'static Location<'static>,
4545
can_unwind: bool,
4646
force_no_backtrace: bool,
4747
}
4848

4949
impl<'a> PanicHookInfo<'a> {
5050
#[inline]
5151
pub(crate) fn new(
52-
location: &'a Location<'a>,
52+
location: &'static Location<'static>,
5353
payload: &'a (dyn Any + Send),
5454
can_unwind: bool,
5555
force_no_backtrace: bool,
@@ -160,10 +160,10 @@ impl<'a> PanicHookInfo<'a> {
160160
#[must_use]
161161
#[inline]
162162
#[stable(feature = "panic_hooks", since = "1.10.0")]
163-
pub fn location(&self) -> Option<&Location<'_>> {
163+
pub fn location(&self) -> Option<&'static Location<'static>> {
164164
// NOTE: If this is changed to sometimes return None,
165165
// deal with that case in std::panicking::default_hook and core::panicking::panic_fmt.
166-
Some(&self.location)
166+
Some(self.location)
167167
}
168168

169169
/// Returns whether the panic handler is allowed to unwind the stack from

library/std/src/panicking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ fn payload_as_str(payload: &dyn Any) -> &str {
776776
#[optimize(size)]
777777
fn panic_with_hook(
778778
payload: &mut dyn PanicPayload,
779-
location: &Location<'_>,
779+
location: &'static Location<'static>,
780780
can_unwind: bool,
781781
force_no_backtrace: bool,
782782
) -> ! {

0 commit comments

Comments
 (0)