|
1 | 1 | use crate::fmt;
|
| 2 | +#[cfg(not(bootstrap))] |
| 3 | +use crate::{ffi::CStr, marker::PhantomData}; |
2 | 4 |
|
3 | 5 | /// A struct containing information about the location of a panic.
|
4 | 6 | ///
|
@@ -28,8 +30,21 @@ use crate::fmt;
|
28 | 30 | /// Files are compared as strings, not `Path`, which could be unexpected.
|
29 | 31 | /// See [`Location::file`]'s documentation for more discussion.
|
30 | 32 | #[lang = "panic_location"]
|
| 33 | +#[derive(Copy, Clone)] |
| 34 | +#[stable(feature = "panic_hooks", since = "1.10.0")] |
| 35 | +#[cfg(not(bootstrap))] |
| 36 | +pub struct Location<'a> { |
| 37 | + file: *const (), |
| 38 | + _file_actual: PhantomData<&'a CStr>, |
| 39 | + line: u32, |
| 40 | + col: u32, |
| 41 | +} |
| 42 | + |
| 43 | +/// Location |
| 44 | +#[lang = "panic_location"] |
31 | 45 | #[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
|
32 | 46 | #[stable(feature = "panic_hooks", since = "1.10.0")]
|
| 47 | +#[cfg(bootstrap)] |
33 | 48 | pub struct Location<'a> {
|
34 | 49 | file: &'a str,
|
35 | 50 | line: u32,
|
@@ -126,7 +141,17 @@ impl<'a> Location<'a> {
|
126 | 141 | #[rustc_const_unstable(feature = "const_location_fields", issue = "102911")]
|
127 | 142 | #[inline]
|
128 | 143 | pub const fn file(&self) -> &str {
|
129 |
| - self.file |
| 144 | + #[cfg(bootstrap)] |
| 145 | + { |
| 146 | + self.file |
| 147 | + } |
| 148 | + #[cfg(not(bootstrap))] |
| 149 | + // SAFETY: nyaaa |
| 150 | + unsafe { |
| 151 | + let cstr = CStr::from_ptr(self.file as _); |
| 152 | + let len = cstr.count_bytes(); |
| 153 | + crate::str::from_utf8_unchecked(crate::slice::from_raw_parts(self.file as _, len)) |
| 154 | + } |
130 | 155 | }
|
131 | 156 |
|
132 | 157 | /// Returns the line number from which the panic originated.
|
@@ -180,21 +205,63 @@ impl<'a> Location<'a> {
|
180 | 205 | }
|
181 | 206 | }
|
182 | 207 |
|
183 |
| -#[unstable( |
184 |
| - feature = "panic_internals", |
185 |
| - reason = "internal details of the implementation of the `panic!` and related macros", |
186 |
| - issue = "none" |
187 |
| -)] |
188 |
| -impl<'a> Location<'a> { |
189 |
| - #[doc(hidden)] |
190 |
| - pub const fn internal_constructor(file: &'a str, line: u32, col: u32) -> Self { |
191 |
| - Location { file, line, col } |
| 208 | +// cfg(not(bootstrap)) NOTE: inline this module when bumping, it's only here to group the cfg |
| 209 | +#[cfg(not(bootstrap))] |
| 210 | +mod location_impls { |
| 211 | + use super::Location; |
| 212 | + |
| 213 | + impl Location<'_> { |
| 214 | + fn to_parts(&self) -> (&str, u32, u32) { |
| 215 | + (self.file(), self.line, self.col) |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + #[stable(feature = "panic_hooks", since = "1.10.0")] |
| 220 | + impl crate::fmt::Debug for Location<'_> { |
| 221 | + fn fmt(&self, f: &mut crate::fmt::Formatter<'_>) -> crate::fmt::Result { |
| 222 | + f.debug_struct("Location") |
| 223 | + .field("file", &self.file()) |
| 224 | + .field("line", &self.line) |
| 225 | + .field("col", &self.col) |
| 226 | + .finish() |
| 227 | + } |
| 228 | + } |
| 229 | + |
| 230 | + #[stable(feature = "panic_hooks", since = "1.10.0")] |
| 231 | + impl crate::cmp::PartialEq for Location<'_> { |
| 232 | + fn eq(&self, other: &Self) -> bool { |
| 233 | + self.to_parts() == other.to_parts() |
| 234 | + } |
| 235 | + } |
| 236 | + |
| 237 | + #[stable(feature = "panic_hooks", since = "1.10.0")] |
| 238 | + impl crate::cmp::Eq for Location<'_> {} |
| 239 | + |
| 240 | + #[stable(feature = "panic_hooks", since = "1.10.0")] |
| 241 | + impl crate::cmp::PartialOrd for Location<'_> { |
| 242 | + fn partial_cmp(&self, other: &Self) -> Option<crate::cmp::Ordering> { |
| 243 | + self.to_parts().partial_cmp(&other.to_parts()) |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + #[stable(feature = "panic_hooks", since = "1.10.0")] |
| 248 | + impl crate::cmp::Ord for Location<'_> { |
| 249 | + fn cmp(&self, other: &Self) -> crate::cmp::Ordering { |
| 250 | + self.to_parts().cmp(&other.to_parts()) |
| 251 | + } |
| 252 | + } |
| 253 | + |
| 254 | + #[stable(feature = "panic_hooks", since = "1.10.0")] |
| 255 | + impl crate::hash::Hash for Location<'_> { |
| 256 | + fn hash<H: crate::hash::Hasher>(&self, state: &mut H) { |
| 257 | + self.to_parts().hash(state) |
| 258 | + } |
192 | 259 | }
|
193 | 260 | }
|
194 | 261 |
|
195 | 262 | #[stable(feature = "panic_hook_display", since = "1.26.0")]
|
196 | 263 | impl fmt::Display for Location<'_> {
|
197 | 264 | fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
198 |
| - write!(formatter, "{}:{}:{}", self.file, self.line, self.col) |
| 265 | + write!(formatter, "{}:{}:{}", self.file(), self.line, self.col) |
199 | 266 | }
|
200 | 267 | }
|
0 commit comments