core/panic/location.rs
1use crate::cmp::Ordering;
2use crate::ffi::CStr;
3use crate::fmt;
4use crate::hash::{Hash, Hasher};
5use crate::marker::PhantomData;
6use crate::ptr::NonNull;
7
8/// A struct containing information about the location of a panic.
9///
10/// This structure is created by [`PanicHookInfo::location()`] and [`PanicInfo::location()`].
11///
12/// [`PanicInfo::location()`]: crate::panic::PanicInfo::location
13/// [`PanicHookInfo::location()`]: ../../std/panic/struct.PanicHookInfo.html#method.location
14///
15/// # Examples
16///
17/// ```should_panic
18/// use std::panic;
19///
20/// panic::set_hook(Box::new(|panic_info| {
21/// if let Some(location) = panic_info.location() {
22/// println!("panic occurred in file '{}' at line {}", location.file(), location.line());
23/// } else {
24/// println!("panic occurred but can't get location information...");
25/// }
26/// }));
27///
28/// panic!("Normal panic");
29/// ```
30///
31/// # Comparisons
32///
33/// Comparisons for equality and ordering are made in file, line, then column priority.
34/// Files are compared as strings, not `Path`, which could be unexpected.
35/// See [`Location::file`]'s documentation for more discussion.
36#[lang = "panic_location"]
37#[derive(Copy, Clone)]
38#[stable(feature = "panic_hooks", since = "1.10.0")]
39pub struct Location<'a> {
40 // A raw pointer is used rather than a reference because the pointer is valid for one more byte
41 // than the length stored in this pointer; the additional byte is the NUL-terminator used by
42 // `Location::file_with_nul`.
43 filename: NonNull<str>,
44 line: u32,
45 col: u32,
46 _filename: PhantomData<&'a str>,
47}
48
49#[stable(feature = "panic_hooks", since = "1.10.0")]
50impl PartialEq for Location<'_> {
51 fn eq(&self, other: &Self) -> bool {
52 // Compare col / line first as they're cheaper to compare and more likely to differ,
53 // while not impacting the result.
54 self.col == other.col && self.line == other.line && self.file() == other.file()
55 }
56}
57
58#[stable(feature = "panic_hooks", since = "1.10.0")]
59impl Eq for Location<'_> {}
60
61#[stable(feature = "panic_hooks", since = "1.10.0")]
62impl Ord for Location<'_> {
63 fn cmp(&self, other: &Self) -> Ordering {
64 self.file()
65 .cmp(other.file())
66 .then_with(|| self.line.cmp(&other.line))
67 .then_with(|| self.col.cmp(&other.col))
68 }
69}
70
71#[stable(feature = "panic_hooks", since = "1.10.0")]
72impl PartialOrd for Location<'_> {
73 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
74 Some(self.cmp(other))
75 }
76}
77
78#[stable(feature = "panic_hooks", since = "1.10.0")]
79impl Hash for Location<'_> {
80 fn hash<H: Hasher>(&self, state: &mut H) {
81 self.file().hash(state);
82 self.line.hash(state);
83 self.col.hash(state);
84 }
85}
86
87#[stable(feature = "panic_hooks", since = "1.10.0")]
88impl fmt::Debug for Location<'_> {
89 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90 f.debug_struct("Location")
91 .field("file", &self.file())
92 .field("line", &self.line)
93 .field("column", &self.col)
94 .finish()
95 }
96}
97
98impl<'a> Location<'a> {
99 /// Returns the source location of the caller of this function. If that function's caller is
100 /// annotated then its call location will be returned, and so on up the stack to the first call
101 /// within a non-tracked function body.
102 ///
103 /// # Examples
104 ///
105 /// ```standalone_crate
106 /// use std::panic::Location;
107 ///
108 /// /// Returns the [`Location`] at which it is called.
109 /// #[track_caller]
110 /// fn get_caller_location() -> &'static Location<'static> {
111 /// Location::caller()
112 /// }
113 ///
114 /// /// Returns a [`Location`] from within this function's definition.
115 /// fn get_just_one_location() -> &'static Location<'static> {
116 /// get_caller_location()
117 /// }
118 ///
119 /// let fixed_location = get_just_one_location();
120 /// assert_eq!(fixed_location.file(), file!());
121 /// assert_eq!(fixed_location.line(), 14);
122 /// assert_eq!(fixed_location.column(), 5);
123 ///
124 /// // running the same untracked function in a different location gives us the same result
125 /// let second_fixed_location = get_just_one_location();
126 /// assert_eq!(fixed_location.file(), second_fixed_location.file());
127 /// assert_eq!(fixed_location.line(), second_fixed_location.line());
128 /// assert_eq!(fixed_location.column(), second_fixed_location.column());
129 ///
130 /// let this_location = get_caller_location();
131 /// assert_eq!(this_location.file(), file!());
132 /// assert_eq!(this_location.line(), 28);
133 /// assert_eq!(this_location.column(), 21);
134 ///
135 /// // running the tracked function in a different location produces a different value
136 /// let another_location = get_caller_location();
137 /// assert_eq!(this_location.file(), another_location.file());
138 /// assert_ne!(this_location.line(), another_location.line());
139 /// assert_ne!(this_location.column(), another_location.column());
140 /// ```
141 #[must_use]
142 #[stable(feature = "track_caller", since = "1.46.0")]
143 #[rustc_const_stable(feature = "const_caller_location", since = "1.79.0")]
144 #[track_caller]
145 #[inline]
146 pub const fn caller() -> &'static Location<'static> {
147 crate::intrinsics::caller_location()
148 }
149
150 /// Returns the name of the source file from which the panic originated.
151 ///
152 /// # `&str`, not `&Path`
153 ///
154 /// The returned name refers to a source path on the compiling system, but it isn't valid to
155 /// represent this directly as a `&Path`. The compiled code may run on a different system with
156 /// a different `Path` implementation than the system providing the contents and this library
157 /// does not currently have a different "host path" type.
158 ///
159 /// The most surprising behavior occurs when "the same" file is reachable via multiple paths in
160 /// the module system (usually using the `#[path = "..."]` attribute or similar), which can
161 /// cause what appears to be identical code to return differing values from this function.
162 ///
163 /// # Cross-compilation
164 ///
165 /// This value is not suitable for passing to `Path::new` or similar constructors when the host
166 /// platform and target platform differ.
167 ///
168 /// # Examples
169 ///
170 /// ```should_panic
171 /// use std::panic;
172 ///
173 /// panic::set_hook(Box::new(|panic_info| {
174 /// if let Some(location) = panic_info.location() {
175 /// println!("panic occurred in file '{}'", location.file());
176 /// } else {
177 /// println!("panic occurred but can't get location information...");
178 /// }
179 /// }));
180 ///
181 /// panic!("Normal panic");
182 /// ```
183 #[must_use]
184 #[stable(feature = "panic_hooks", since = "1.10.0")]
185 #[rustc_const_stable(feature = "const_location_fields", since = "1.79.0")]
186 pub const fn file(&self) -> &str {
187 // SAFETY: The filename is valid.
188 unsafe { self.filename.as_ref() }
189 }
190
191 /// Returns the name of the source file as a nul-terminated `CStr`.
192 ///
193 /// This is useful for interop with APIs that expect C/C++ `__FILE__` or
194 /// `std::source_location::file_name`, both of which return a nul-terminated `const char*`.
195 #[must_use]
196 #[unstable(feature = "file_with_nul", issue = "141727")]
197 #[inline]
198 pub const fn file_with_nul(&self) -> &CStr {
199 let filename = self.filename.as_ptr();
200
201 // SAFETY: The filename is valid for `filename_len+1` bytes, so this addition can't
202 // overflow.
203 let cstr_len = unsafe { crate::mem::size_of_val_raw(filename).unchecked_add(1) };
204
205 // SAFETY: The filename is valid for `filename_len+1` bytes.
206 let slice = unsafe { crate::slice::from_raw_parts(filename.cast(), cstr_len) };
207
208 // SAFETY: The filename is guaranteed to have a trailing nul byte and no interior nul bytes.
209 unsafe { CStr::from_bytes_with_nul_unchecked(slice) }
210 }
211
212 /// Returns the line number from which the panic originated.
213 ///
214 /// # Examples
215 ///
216 /// ```should_panic
217 /// use std::panic;
218 ///
219 /// panic::set_hook(Box::new(|panic_info| {
220 /// if let Some(location) = panic_info.location() {
221 /// println!("panic occurred at line {}", location.line());
222 /// } else {
223 /// println!("panic occurred but can't get location information...");
224 /// }
225 /// }));
226 ///
227 /// panic!("Normal panic");
228 /// ```
229 #[must_use]
230 #[stable(feature = "panic_hooks", since = "1.10.0")]
231 #[rustc_const_stable(feature = "const_location_fields", since = "1.79.0")]
232 #[inline]
233 pub const fn line(&self) -> u32 {
234 self.line
235 }
236
237 /// Returns the column from which the panic originated.
238 ///
239 /// # Examples
240 ///
241 /// ```should_panic
242 /// use std::panic;
243 ///
244 /// panic::set_hook(Box::new(|panic_info| {
245 /// if let Some(location) = panic_info.location() {
246 /// println!("panic occurred at column {}", location.column());
247 /// } else {
248 /// println!("panic occurred but can't get location information...");
249 /// }
250 /// }));
251 ///
252 /// panic!("Normal panic");
253 /// ```
254 #[must_use]
255 #[stable(feature = "panic_col", since = "1.25.0")]
256 #[rustc_const_stable(feature = "const_location_fields", since = "1.79.0")]
257 #[inline]
258 pub const fn column(&self) -> u32 {
259 self.col
260 }
261}
262
263#[stable(feature = "panic_hook_display", since = "1.26.0")]
264impl fmt::Display for Location<'_> {
265 #[inline]
266 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
267 write!(formatter, "{}:{}:{}", self.file(), self.line, self.col)
268 }
269}
270
271#[stable(feature = "panic_hooks", since = "1.10.0")]
272unsafe impl Send for Location<'_> {}
273#[stable(feature = "panic_hooks", since = "1.10.0")]
274unsafe impl Sync for Location<'_> {}