1
1
use crate :: ffi:: CStr ;
2
2
use crate :: fmt;
3
+ use crate :: marker:: PhantomData ;
3
4
4
5
/// A struct containing information about the location of a panic.
5
6
///
@@ -33,16 +34,18 @@ use crate::fmt;
33
34
#[ derive( Copy , Clone , Debug , Eq , Hash , Ord , PartialEq , PartialOrd ) ]
34
35
#[ stable( feature = "panic_hooks" , since = "1.10.0" ) ]
35
36
pub struct Location < ' a > {
36
- // Note: this filename will have exactly one nul byte at its end, but otherwise
37
- // it must never contain interior nul bytes. This is relied on for the conversion
38
- // to `CStr` below.
39
- //
40
- // The prefix of the string without the trailing nul byte will be a regular UTF8 `str`.
41
- file_bytes_with_nul : & ' a [ u8 ] ,
37
+ // A raw pointer is used rather than a reference because the pointer is valid for one more byte
38
+ // than the length stored in this pointer; the additional byte is the NUL-terminator used by
39
+ // `Location::file_with_nul`.
40
+ filename : * const str ,
42
41
line : u32 ,
43
42
col : u32 ,
43
+ _filename : PhantomData < & ' a str > ,
44
44
}
45
45
46
+ unsafe impl Send for Location < ' _ > { }
47
+ unsafe impl Sync for Location < ' _ > { }
48
+
46
49
impl < ' a > Location < ' a > {
47
50
/// Returns the source location of the caller of this function. If that function's caller is
48
51
/// annotated then its call location will be returned, and so on up the stack to the first call
@@ -132,10 +135,8 @@ impl<'a> Location<'a> {
132
135
#[ stable( feature = "panic_hooks" , since = "1.10.0" ) ]
133
136
#[ rustc_const_stable( feature = "const_location_fields" , since = "1.79.0" ) ]
134
137
pub const fn file ( & self ) -> & str {
135
- let str_len = self . file_bytes_with_nul . len ( ) - 1 ;
136
- // SAFETY: `file_bytes_with_nul` without the trailing nul byte is guaranteed to be
137
- // valid UTF8.
138
- unsafe { crate :: str:: from_raw_parts ( self . file_bytes_with_nul . as_ptr ( ) , str_len) }
138
+ // SAFETY: The filename is valid.
139
+ unsafe { & * self . filename }
139
140
}
140
141
141
142
/// Returns the name of the source file as a nul-terminated `CStr`.
@@ -146,9 +147,15 @@ impl<'a> Location<'a> {
146
147
#[ unstable( feature = "file_with_nul" , issue = "141727" ) ]
147
148
#[ inline]
148
149
pub const fn file_with_nul ( & self ) -> & CStr {
149
- // SAFETY: `file_bytes_with_nul` is guaranteed to have a trailing nul byte and no
150
- // interior nul bytes.
151
- unsafe { CStr :: from_bytes_with_nul_unchecked ( self . file_bytes_with_nul ) }
150
+ // SAFETY: The filename is valid for `filename_len+1` bytes, so this addition can't
151
+ // overflow.
152
+ let cstr_len = unsafe { crate :: mem:: size_of_val_raw ( self . filename ) . unchecked_add ( 1 ) } ;
153
+
154
+ // SAFETY: The filename is valid for `filename_len+1` bytes.
155
+ let slice = unsafe { crate :: slice:: from_raw_parts ( self . filename as * const _ , cstr_len) } ;
156
+
157
+ // SAFETY: The filename is guaranteed to have a trailing nul byte and no interior nul bytes.
158
+ unsafe { CStr :: from_bytes_with_nul_unchecked ( slice) }
152
159
}
153
160
154
161
/// Returns the line number from which the panic originated.
0 commit comments