11use crate :: ffi:: CStr ;
22use crate :: fmt;
33use crate :: marker:: PhantomData ;
4+ use crate :: ptr:: NonNull ;
45
56/// A struct containing information about the location of a panic.
67///
@@ -37,7 +38,7 @@ pub struct Location<'a> {
3738 // A raw pointer is used rather than a reference because the pointer is valid for one more byte
3839 // than the length stored in this pointer; the additional byte is the NUL-terminator used by
3940 // `Location::file_with_nul`.
40- filename : * const str ,
41+ filename : NonNull < str > ,
4142 line : u32 ,
4243 col : u32 ,
4344 _filename : PhantomData < & ' a str > ,
@@ -144,7 +145,7 @@ impl<'a> Location<'a> {
144145 #[ rustc_const_stable( feature = "const_location_fields" , since = "1.79.0" ) ]
145146 pub const fn file ( & self ) -> & str {
146147 // SAFETY: The filename is valid.
147- unsafe { & * self . filename }
148+ unsafe { self . filename . as_ref ( ) }
148149 }
149150
150151 /// Returns the name of the source file as a nul-terminated `CStr`.
@@ -155,12 +156,14 @@ impl<'a> Location<'a> {
155156 #[ unstable( feature = "file_with_nul" , issue = "141727" ) ]
156157 #[ inline]
157158 pub const fn file_with_nul ( & self ) -> & CStr {
159+ let filename = self . filename . as_ptr ( ) ;
160+
158161 // SAFETY: The filename is valid for `filename_len+1` bytes, so this addition can't
159162 // overflow.
160- let cstr_len = unsafe { crate :: mem:: size_of_val_raw ( self . filename ) . unchecked_add ( 1 ) } ;
163+ let cstr_len = unsafe { crate :: mem:: size_of_val_raw ( filename) . unchecked_add ( 1 ) } ;
161164
162165 // SAFETY: The filename is valid for `filename_len+1` bytes.
163- let slice = unsafe { crate :: slice:: from_raw_parts ( self . filename as * const _ , cstr_len) } ;
166+ let slice = unsafe { crate :: slice:: from_raw_parts ( filename. cast ( ) , cstr_len) } ;
164167
165168 // SAFETY: The filename is guaranteed to have a trailing nul byte and no interior nul bytes.
166169 unsafe { CStr :: from_bytes_with_nul_unchecked ( slice) }
0 commit comments