1
1
use crate :: ffi:: CStr ;
2
2
use crate :: fmt;
3
3
use crate :: marker:: PhantomData ;
4
+ use crate :: ptr:: NonNull ;
4
5
5
6
/// A struct containing information about the location of a panic.
6
7
///
@@ -37,7 +38,7 @@ pub struct Location<'a> {
37
38
// A raw pointer is used rather than a reference because the pointer is valid for one more byte
38
39
// than the length stored in this pointer; the additional byte is the NUL-terminator used by
39
40
// `Location::file_with_nul`.
40
- filename : * const str ,
41
+ filename : NonNull < str > ,
41
42
line : u32 ,
42
43
col : u32 ,
43
44
_filename : PhantomData < & ' a str > ,
@@ -144,7 +145,7 @@ impl<'a> Location<'a> {
144
145
#[ rustc_const_stable( feature = "const_location_fields" , since = "1.79.0" ) ]
145
146
pub const fn file ( & self ) -> & str {
146
147
// SAFETY: The filename is valid.
147
- unsafe { & * self . filename }
148
+ unsafe { self . filename . as_ref ( ) }
148
149
}
149
150
150
151
/// Returns the name of the source file as a nul-terminated `CStr`.
@@ -155,12 +156,14 @@ impl<'a> Location<'a> {
155
156
#[ unstable( feature = "file_with_nul" , issue = "141727" ) ]
156
157
#[ inline]
157
158
pub const fn file_with_nul ( & self ) -> & CStr {
159
+ let filename = self . filename . as_ptr ( ) ;
160
+
158
161
// SAFETY: The filename is valid for `filename_len+1` bytes, so this addition can't
159
162
// 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 ) } ;
161
164
162
165
// 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) } ;
164
167
165
168
// SAFETY: The filename is guaranteed to have a trailing nul byte and no interior nul bytes.
166
169
unsafe { CStr :: from_bytes_with_nul_unchecked ( slice) }
0 commit comments