diff --git a/library/core/src/panic/location.rs b/library/core/src/panic/location.rs index 94cfd667ffae0..f1eedede8aab9 100644 --- a/library/core/src/panic/location.rs +++ b/library/core/src/panic/location.rs @@ -30,7 +30,7 @@ use crate::fmt; /// Files are compared as strings, not `Path`, which could be unexpected. /// See [`Location::file`]'s documentation for more discussion. #[lang = "panic_location"] -#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] +#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)] #[stable(feature = "panic_hooks", since = "1.10.0")] pub struct Location<'a> { // Note: this filename will have exactly one nul byte at its end, but otherwise @@ -43,6 +43,17 @@ pub struct Location<'a> { col: u32, } +#[stable(feature = "panic_hooks", since = "1.10.0")] +impl fmt::Debug for Location<'_> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Location") + .field("file", &self.file()) + .field("line", &self.line) + .field("column", &self.col) + .finish() + } +} + impl<'a> Location<'a> { /// Returns the source location of the caller of this function. If that function's caller is /// annotated then its call location will be returned, and so on up the stack to the first call diff --git a/library/coretests/tests/panic/location.rs b/library/coretests/tests/panic/location.rs index d20241d838001..5ce0b06e90e1a 100644 --- a/library/coretests/tests/panic/location.rs +++ b/library/coretests/tests/panic/location.rs @@ -29,3 +29,11 @@ fn location_const_column() { const COLUMN: u32 = CALLER.column(); assert_eq!(COLUMN, 40); } + +#[test] +fn location_debug() { + let f = format!("{:?}", Location::caller()); + assert!(f.contains(&format!("{:?}", file!()))); + assert!(f.contains("35")); + assert!(f.contains("29")); +}