about summary refs log tree commit diff
diff options
context:
space:
mode:
authorJacob Pratt <jacob@jhpratt.dev>2025-08-22 22:00:44 -0400
committerGitHub <noreply@github.com>2025-08-22 22:00:44 -0400
commitdbc38eed1dc7fba0a6c3d6c3edc4ec1f8fa88099 (patch)
treebf585068c5f5726305731a6740becefdb298f38f
parent6545b056388a727c2ac4a4e70fb01d60a11398b1 (diff)
parent792ec3bdd42800bfbf16ee1d570efc398bf30f30 (diff)
downloadrust-dbc38eed1dc7fba0a6c3d6c3edc4ec1f8fa88099.tar.gz
rust-dbc38eed1dc7fba0a6c3d6c3edc4ec1f8fa88099.zip
Rollup merge of #132087 - ijchen:issue-131770-fix, r=dtolnay
Fix overly restrictive lifetime in `core::panic::Location::file` return type

Fixes #131770 by relaxing the lifetime to match what's stored in the struct. See that issue for more details and discussion.

Since this is a breaking change, I think a crater run is in order. Since this change should only have an effect at compile-time, I think just a check run is sufficient.
-rw-r--r--library/core/src/panic/location.rs4
-rw-r--r--library/coretests/tests/panic/location.rs10
2 files changed, 11 insertions, 3 deletions
diff --git a/library/core/src/panic/location.rs b/library/core/src/panic/location.rs
index 6ef7d5a22a3..cafdcfa2c2e 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() }
     }
@@ -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
diff --git a/library/coretests/tests/panic/location.rs b/library/coretests/tests/panic/location.rs
index 910001bcc1c..a7db05a15c6 100644
--- a/library/coretests/tests/panic/location.rs
+++ b/library/coretests/tests/panic/location.rs
@@ -48,10 +48,18 @@ fn location_const_column() {
 }
 
 #[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());
     assert!(f.contains(&format!("{:?}", file!())));
-    assert!(f.contains("52"));
+    assert!(f.contains("60"));
     assert!(f.contains("29"));
 }