Skip to content

Commit 4d90b34

Browse files
committed
changed Location<'_> lifetime to 'static in Panic[Hook]Info
1 parent 52618eb commit 4d90b34

File tree

4 files changed

+26
-8
lines changed

4 files changed

+26
-8
lines changed

library/core/src/panic/panic_info.rs

Lines changed: 3 additions & 3 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: &'a 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: &'a Location<'static>,
3737
can_unwind: bool,
3838
force_no_backtrace: bool,
3939
) -> Self {
@@ -88,7 +88,7 @@ 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<&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.
9494
Some(&self.location)

library/std/src/panic.rs

Lines changed: 3 additions & 3 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: &'a 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: &'a Location<'static>,
5353
payload: &'a (dyn Any + Send),
5454
can_unwind: bool,
5555
force_no_backtrace: bool,
@@ -160,7 +160,7 @@ 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<&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.
166166
Some(&self.location)

library/std/src/panicking.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,7 @@ fn payload_as_str(payload: &dyn Any) -> &str {
795795
#[optimize(size)]
796796
fn panic_with_hook(
797797
payload: &mut dyn PanicPayload,
798-
location: &Location<'_>,
798+
location: &Location<'static>,
799799
can_unwind: bool,
800800
force_no_backtrace: bool,
801801
) -> ! {

library/std/tests/panic.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#![allow(dead_code)]
22

3+
use core::panic::PanicInfo;
34
use std::cell::RefCell;
4-
use std::panic::{AssertUnwindSafe, UnwindSafe};
5+
use std::panic::{AssertUnwindSafe, Location, PanicHookInfo, UnwindSafe};
56
use std::rc::Rc;
67
use std::sync::{Arc, Mutex, RwLock};
78

@@ -54,3 +55,20 @@ fn panic_safety_traits() {
5455
assert::<Arc<AssertUnwindSafe<T>>>();
5556
}
5657
}
58+
59+
#[test]
60+
fn panic_info_static_location<'x>() {
61+
// Verify that the returned `Location<'_>`s generic lifetime is 'static when
62+
// calling `PanicInfo::location`. Test failure is indicated by a compile
63+
// failure, not a runtime panic.
64+
let _: for<'a> fn(&'a PanicInfo<'x>) -> Option<&'a Location<'static>> = PanicInfo::location;
65+
}
66+
67+
#[test]
68+
fn panic_hook_info_static_location<'x>() {
69+
// Verify that the returned `Location<'_>`s generic lifetime is 'static when
70+
// calling `PanicHookInfo::location`. Test failure is indicated by a compile
71+
// failure, not a runtime panic.
72+
let _: for<'a> fn(&'a PanicHookInfo<'x>) -> Option<&'a Location<'static>> =
73+
PanicHookInfo::location;
74+
}

0 commit comments

Comments
 (0)