Rollup merge of #142373 - m-ou-se:debug-for-location, r=tgross35

Fix Debug for Location

Fixes https://github.com/rust-lang/rust/issues/142279
This commit is contained in:
Jakub Beránek
2025-06-16 14:31:11 +02:00
committed by GitHub
2 changed files with 20 additions and 1 deletions
+12 -1
View File
@@ -30,7 +30,7 @@
/// 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
@@ -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"));
}