diff options
| author | Alice Ryhl <aliceryhl@google.com> | 2025-06-23 13:45:56 +0000 |
|---|---|---|
| committer | Alice Ryhl <aliceryhl@google.com> | 2025-06-23 13:45:56 +0000 |
| commit | 6a1b7df4854b0023e34b18e2196d3069906da796 (patch) | |
| tree | f223bfb71503b72b153c0ae2ae7b343a6f85ac84 /library/core/src/panic | |
| parent | f3383e4942bca64ce523b7736a2bedd1b8426c11 (diff) | |
Use a NonNull pointer
Diffstat (limited to 'library/core/src/panic')
| -rw-r--r-- | library/core/src/panic/location.rs | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/library/core/src/panic/location.rs b/library/core/src/panic/location.rs index 1e950d6e498..97227020885 100644 --- a/library/core/src/panic/location.rs +++ b/library/core/src/panic/location.rs @@ -1,6 +1,7 @@ use crate::ffi::CStr; use crate::fmt; use crate::marker::PhantomData; +use crate::ptr::NonNull; /// A struct containing information about the location of a panic. /// @@ -37,7 +38,7 @@ pub struct Location<'a> { // A raw pointer is used rather than a reference because the pointer is valid for one more byte // than the length stored in this pointer; the additional byte is the NUL-terminator used by // `Location::file_with_nul`. - filename: *const str, + filename: NonNull<str>, line: u32, col: u32, _filename: PhantomData<&'a str>, @@ -144,7 +145,7 @@ impl<'a> Location<'a> { #[rustc_const_stable(feature = "const_location_fields", since = "1.79.0")] pub const fn file(&self) -> &str { // SAFETY: The filename is valid. - unsafe { &*self.filename } + unsafe { self.filename.as_ref() } } /// Returns the name of the source file as a nul-terminated `CStr`. @@ -155,12 +156,14 @@ impl<'a> Location<'a> { #[unstable(feature = "file_with_nul", issue = "141727")] #[inline] pub const fn file_with_nul(&self) -> &CStr { + let filename = self.filename.as_ptr(); + // SAFETY: The filename is valid for `filename_len+1` bytes, so this addition can't // overflow. - let cstr_len = unsafe { crate::mem::size_of_val_raw(self.filename).unchecked_add(1) }; + let cstr_len = unsafe { crate::mem::size_of_val_raw(filename).unchecked_add(1) }; // SAFETY: The filename is valid for `filename_len+1` bytes. - let slice = unsafe { crate::slice::from_raw_parts(self.filename as *const _, cstr_len) }; + let slice = unsafe { crate::slice::from_raw_parts(filename.cast(), cstr_len) }; // SAFETY: The filename is guaranteed to have a trailing nul byte and no interior nul bytes. unsafe { CStr::from_bytes_with_nul_unchecked(slice) } |
