From e772f7ab3ee7d5f217c60b640b95f8bfc84d9536 Mon Sep 17 00:00:00 2001 From: Isaac Chen Date: Wed, 23 Oct 2024 20:13:56 -0400 Subject: [PATCH 1/5] corrected lifetime in core::panic::Location::file return type --- library/core/src/panic/location.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/panic/location.rs b/library/core/src/panic/location.rs index 6ef7d5a22a30f..e81dc5eb53042 100644 --- a/library/core/src/panic/location.rs +++ b/library/core/src/panic/location.rs @@ -183,7 +183,7 @@ impl<'a> Location<'a> { #[must_use] #[stable(feature = "panic_hooks", since = "1.10.0")] #[rustc_const_stable(feature = "const_location_fields", since = "1.79.0")] - pub const fn file(&self) -> &str { + pub const fn file(&self) -> &'a str { // SAFETY: The filename is valid. unsafe { self.filename.as_ref() } } From 9c4a35e0e705dbe93dcfbba9fbde9d3dbfc13e11 Mon Sep 17 00:00:00 2001 From: Isaac Chen Date: Wed, 23 Oct 2024 23:49:23 -0400 Subject: [PATCH 2/5] added regression test for `core::panic::Location::file`'s lifetime --- library/core/tests/panic/location.rs | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 library/core/tests/panic/location.rs diff --git a/library/core/tests/panic/location.rs b/library/core/tests/panic/location.rs new file mode 100644 index 0000000000000..35017612f00cc --- /dev/null +++ b/library/core/tests/panic/location.rs @@ -0,0 +1,39 @@ +use core::panic::Location; + +// Note: Some of the following tests depend on the source location, +// so please be careful when editing this file. + +#[test] +fn location_const_caller() { + const _CALLER_REFERENCE: &Location<'static> = Location::caller(); + const _CALLER: Location<'static> = *Location::caller(); +} + +#[test] +fn location_const_file() { + const CALLER: &Location<'static> = Location::caller(); + const FILE: &str = CALLER.file(); + assert_eq!(FILE, file!()); +} + +#[test] +fn location_const_line() { + const CALLER: &Location<'static> = Location::caller(); + const LINE: u32 = CALLER.line(); + assert_eq!(LINE, 21); +} + +#[test] +fn location_const_column() { + const CALLER: &Location<'static> = Location::caller(); + const COLUMN: u32 = CALLER.column(); + assert_eq!(COLUMN, 40); +} + +#[test] +fn location_file_lifetime<'x>() { + // Verify that the returned `&str`s lifetime is derived from the generic + // lifetime 'a, not the lifetime of `&self`, when calling `Location::file`. + // Test failure is indicated by a compile failure, not a runtime panic. + let _: for<'a> fn(&'a Location<'x>) -> &'x str = Location::file; +} From 557737062d1a65b25f9bbb257f07755078e5cdae Mon Sep 17 00:00:00 2001 From: Isaac Chen Date: Wed, 23 Oct 2024 20:13:56 -0400 Subject: [PATCH 3/5] corrected lifetime in core::panic::Location::file return type --- library/core/src/panic/location.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/core/src/panic/location.rs b/library/core/src/panic/location.rs index e81dc5eb53042..cafdcfa2c2e02 100644 --- a/library/core/src/panic/location.rs +++ b/library/core/src/panic/location.rs @@ -195,7 +195,7 @@ impl<'a> Location<'a> { #[must_use] #[unstable(feature = "file_with_nul", issue = "141727")] #[inline] - pub const fn file_with_nul(&self) -> &CStr { + pub const fn file_with_nul(&self) -> &'a CStr { let filename = self.filename.as_ptr(); // SAFETY: The filename is valid for `filename_len+1` bytes, so this addition can't From 8a0438fee86b7dc566d0758b3aee050b84edc842 Mon Sep 17 00:00:00 2001 From: Isaac Chen Date: Sat, 2 Aug 2025 16:53:06 -0400 Subject: [PATCH 4/5] moved new test to updated test location --- library/core/tests/panic/location.rs | 39 ----------------------- library/coretests/tests/panic/location.rs | 8 +++++ 2 files changed, 8 insertions(+), 39 deletions(-) delete mode 100644 library/core/tests/panic/location.rs diff --git a/library/core/tests/panic/location.rs b/library/core/tests/panic/location.rs deleted file mode 100644 index 35017612f00cc..0000000000000 --- a/library/core/tests/panic/location.rs +++ /dev/null @@ -1,39 +0,0 @@ -use core::panic::Location; - -// Note: Some of the following tests depend on the source location, -// so please be careful when editing this file. - -#[test] -fn location_const_caller() { - const _CALLER_REFERENCE: &Location<'static> = Location::caller(); - const _CALLER: Location<'static> = *Location::caller(); -} - -#[test] -fn location_const_file() { - const CALLER: &Location<'static> = Location::caller(); - const FILE: &str = CALLER.file(); - assert_eq!(FILE, file!()); -} - -#[test] -fn location_const_line() { - const CALLER: &Location<'static> = Location::caller(); - const LINE: u32 = CALLER.line(); - assert_eq!(LINE, 21); -} - -#[test] -fn location_const_column() { - const CALLER: &Location<'static> = Location::caller(); - const COLUMN: u32 = CALLER.column(); - assert_eq!(COLUMN, 40); -} - -#[test] -fn location_file_lifetime<'x>() { - // Verify that the returned `&str`s lifetime is derived from the generic - // lifetime 'a, not the lifetime of `&self`, when calling `Location::file`. - // Test failure is indicated by a compile failure, not a runtime panic. - let _: for<'a> fn(&'a Location<'x>) -> &'x str = Location::file; -} diff --git a/library/coretests/tests/panic/location.rs b/library/coretests/tests/panic/location.rs index 910001bcc1c58..2174ac854e943 100644 --- a/library/coretests/tests/panic/location.rs +++ b/library/coretests/tests/panic/location.rs @@ -47,6 +47,14 @@ fn location_const_column() { assert_eq!(COLUMN, 40); } +#[test] +fn location_file_lifetime<'x>() { + // Verify that the returned `&str`s lifetime is derived from the generic + // lifetime 'a, not the lifetime of `&self`, when calling `Location::file`. + // Test failure is indicated by a compile failure, not a runtime panic. + let _: for<'a> fn(&'a Location<'x>) -> &'x str = Location::file; +} + #[test] fn location_debug() { let f = format!("{:?}", Location::caller()); From 792ec3bdd42800bfbf16ee1d570efc398bf30f30 Mon Sep 17 00:00:00 2001 From: Isaac Chen Date: Sat, 2 Aug 2025 17:34:59 -0400 Subject: [PATCH 5/5] updated line number in test --- library/coretests/tests/panic/location.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/coretests/tests/panic/location.rs b/library/coretests/tests/panic/location.rs index 2174ac854e943..a7db05a15c68f 100644 --- a/library/coretests/tests/panic/location.rs +++ b/library/coretests/tests/panic/location.rs @@ -59,7 +59,7 @@ fn location_file_lifetime<'x>() { fn location_debug() { let f = format!("{:?}", Location::caller()); assert!(f.contains(&format!("{:?}", file!()))); - assert!(f.contains("52")); + assert!(f.contains("60")); assert!(f.contains("29")); }