Skip to content

Commit 9be8ffc

Browse files
committed
Auto merge of #73583 - anp:location-eq, r=dtolnay
Derive common traits for panic::Location. Now that `#[track_caller]` is on track to stabilize, one of the roughest edges of working with it is the fact that you can't do much with `Location` except turn it back into a `(&str, u32, u32)`. Which makes sense because the type was defined around the panic machinery originally passing around that tuple (it has the same layout as Location even). This PR derives common traits for the type in accordance with the [API guidelines](https://rust-lang.github.io/api-guidelines/interoperability.html#types-eagerly-implement-common-traits-c-common-traits) (those apply to core, right?). There's a risk here, e.g. if we ever change the representation of `Location` in a way that makes it harder to implement `Ord`, we might not be able to make that change in a backwards-compatible way. I don't think there's any other compatibility hazard here, as the only changes we currently imagine for the type are to add end fields. cc @rust-lang/libs
2 parents 76e8333 + 416dc4b commit 9be8ffc

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/libcore/panic.rs

+23-1
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,14 @@ impl fmt::Display for PanicInfo<'_> {
173173
///
174174
/// panic!("Normal panic");
175175
/// ```
176+
///
177+
/// # Comparisons
178+
///
179+
/// Comparisons for equality and ordering are made in file, line, then column priority.
180+
/// Files are compared as strings, not `Path`, which could be unexpected.
181+
/// See [`Location::file`]'s documentation for more discussion.
176182
#[lang = "panic_location"]
177-
#[derive(Debug)]
183+
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
178184
#[stable(feature = "panic_hooks", since = "1.10.0")]
179185
pub struct Location<'a> {
180186
file: &'a str,
@@ -246,6 +252,22 @@ impl<'a> Location<'a> {
246252

247253
/// Returns the name of the source file from which the panic originated.
248254
///
255+
/// # `&str`, not `&Path`
256+
///
257+
/// The returned name refers to a source path on the compiling system, but it isn't valid to
258+
/// represent this directly as a `&Path`. The compiled code may run on a different system with
259+
/// a different `Path` implementation than the system providing the contents and this library
260+
/// does not currently have a different "host path" type.
261+
///
262+
/// The most surprising behavior occurs when "the same" file is reachable via multiple paths in
263+
/// the module system (usually using the `#[path = "..."]` attribute or similar), which can
264+
/// cause what appears to be identical code to return differing values from this function.
265+
///
266+
/// # Cross-compilation
267+
///
268+
/// This value is not suitable for passing to `Path::new` or similar constructors when the host
269+
/// platform and target platform differ.
270+
///
249271
/// # Examples
250272
///
251273
/// ```should_panic

0 commit comments

Comments
 (0)