Skip to content

Commit 3da5817

Browse files
authored
Rollup merge of rust-lang#132087 - ijchen:issue-131770-fix, r=dtolnay
Fix overly restrictive lifetime in `core::panic::Location::file` return type Fixes rust-lang#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.
2 parents 764ad79 + fd7b116 commit 3da5817

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

core/src/panic/location.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl<'a> Location<'a> {
183183
#[must_use]
184184
#[stable(feature = "panic_hooks", since = "1.10.0")]
185185
#[rustc_const_stable(feature = "const_location_fields", since = "1.79.0")]
186-
pub const fn file(&self) -> &str {
186+
pub const fn file(&self) -> &'a str {
187187
// SAFETY: The filename is valid.
188188
unsafe { self.filename.as_ref() }
189189
}
@@ -195,7 +195,7 @@ impl<'a> Location<'a> {
195195
#[must_use]
196196
#[unstable(feature = "file_with_nul", issue = "141727")]
197197
#[inline]
198-
pub const fn file_with_nul(&self) -> &CStr {
198+
pub const fn file_with_nul(&self) -> &'a CStr {
199199
let filename = self.filename.as_ptr();
200200

201201
// SAFETY: The filename is valid for `filename_len+1` bytes, so this addition can't

coretests/tests/panic/location.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,19 @@ fn location_const_column() {
4747
assert_eq!(COLUMN, 40);
4848
}
4949

50+
#[test]
51+
fn location_file_lifetime<'x>() {
52+
// Verify that the returned `&str`s lifetime is derived from the generic
53+
// lifetime 'a, not the lifetime of `&self`, when calling `Location::file`.
54+
// Test failure is indicated by a compile failure, not a runtime panic.
55+
let _: for<'a> fn(&'a Location<'x>) -> &'x str = Location::file;
56+
}
57+
5058
#[test]
5159
fn location_debug() {
5260
let f = format!("{:?}", Location::caller());
5361
assert!(f.contains(&format!("{:?}", file!())));
54-
assert!(f.contains("52"));
62+
assert!(f.contains("60"));
5563
assert!(f.contains("29"));
5664
}
5765

0 commit comments

Comments
 (0)