Skip to content

Commit 2a372a3

Browse files
committed
syntax: tweak Debug impl for Hir
The default derive(Debug) impl for Hir is very noisy because it lists out the properties for every Hir value. We change the default to just print out the actual expressions and omit the properties. But one can opt back into seeing the properties via the "alternate" impl. i.e., {:#?} instead of {:?}.
1 parent 8f09510 commit 2a372a3

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

regex-syntax/src/hir/mod.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,14 @@ impl core::fmt::Display for ErrorKind {
157157
/// proportional to the size of the `Hir`. The regex it prints is guaranteed to
158158
/// be _semantically_ equivalent to the original concrete syntax, but it may
159159
/// look very different. (And potentially not practically readable by a human.)
160-
#[derive(Clone, Debug, Eq, PartialEq)]
160+
///
161+
/// An `Hir`'s `fmt::Debug` implementation currently does not use constant
162+
/// stack space. The default implementation will also suppress some details
163+
/// (such as the `Properties` inlined into every `Hir` value to make it less
164+
/// noisy), but using the "[alternate]" format option will show everything.
165+
///
166+
/// [alternate]: https://doc.rust-lang.org/std/fmt/struct.Formatter.html#method.alternate
167+
#[derive(Clone, Eq, PartialEq)]
161168
pub struct Hir {
162169
/// The underlying HIR kind.
163170
kind: HirKind,
@@ -413,6 +420,19 @@ impl HirKind {
413420
}
414421
}
415422

423+
impl core::fmt::Debug for Hir {
424+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
425+
if f.alternate() {
426+
f.debug_struct("Hir")
427+
.field("kind", &self.kind)
428+
.field("props", &self.props)
429+
.finish()
430+
} else {
431+
self.kind.fmt(f)
432+
}
433+
}
434+
}
435+
416436
/// Print a display representation of this Hir.
417437
///
418438
/// The result of this is a valid regular expression pattern string.

0 commit comments

Comments
 (0)