Skip to content

Commit fbffaa9

Browse files
Rollup merge of #105558 - Nilstrieb:less-spam-hir-tree, r=cjgillot
Reduce HIR debug output HIR debug output is currently very verbose, especially when used with the alternate (`#`) flag. This commit reduces the amount of noisy newlines by forcing a few small key types to stay on one line, which makes the output easier to read and scroll by. ``` $ rustc +after hello_world.rs -Zunpretty=hir-tree | wc -l 582 $ rustc +before hello_world.rs -Zunpretty=hir-tree | wc -l 932 ```
2 parents da1ca5d + e1787f5 commit fbffaa9

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

compiler/rustc_data_structures/src/sorted_map.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::stable_hasher::{HashStable, StableHasher, StableOrd};
22
use std::borrow::Borrow;
33
use std::cmp::Ordering;
4+
use std::fmt::Debug;
45
use std::mem;
56
use std::ops::{Bound, Index, IndexMut, RangeBounds};
67

@@ -16,7 +17,7 @@ pub use index_map::SortedIndexMultiMap;
1617
/// stores data in a more compact way. It also supports accessing contiguous
1718
/// ranges of elements as a slice, and slices of already sorted elements can be
1819
/// inserted efficiently.
19-
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Encodable, Decodable)]
20+
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
2021
pub struct SortedMap<K, V> {
2122
data: Vec<(K, V)>,
2223
}
@@ -314,5 +315,11 @@ impl<K: HashStable<CTX> + StableOrd, V: HashStable<CTX>, CTX> HashStable<CTX> fo
314315
}
315316
}
316317

318+
impl<K: Debug, V: Debug> Debug for SortedMap<K, V> {
319+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
320+
f.debug_map().entries(self.data.iter().map(|(a, b)| (a, b))).finish()
321+
}
322+
}
323+
317324
#[cfg(test)]
318325
mod tests;

compiler/rustc_hir/src/hir.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,11 @@ impl fmt::Debug for OwnerNodes<'_> {
854854
&self
855855
.nodes
856856
.iter_enumerated()
857-
.map(|(id, parented_node)| (id, parented_node.as_ref().map(|node| node.parent)))
857+
.map(|(id, parented_node)| {
858+
let parented_node = parented_node.as_ref().map(|node| node.parent);
859+
860+
debug_fn(move |f| write!(f, "({id:?}, {parented_node:?})"))
861+
})
858862
.collect::<Vec<_>>(),
859863
)
860864
.field("bodies", &self.bodies)
@@ -3615,3 +3619,13 @@ mod size_asserts {
36153619
static_assert_size!(TyKind<'_>, 32);
36163620
// tidy-alphabetical-end
36173621
}
3622+
3623+
fn debug_fn(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Debug {
3624+
struct DebugFn<F>(F);
3625+
impl<F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result> fmt::Debug for DebugFn<F> {
3626+
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
3627+
(self.0)(fmt)
3628+
}
3629+
}
3630+
DebugFn(f)
3631+
}

compiler/rustc_hir/src/hir_id.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
use crate::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_ID};
22
use rustc_data_structures::stable_hasher::{HashStable, StableHasher, StableOrd, ToStableHashKey};
33
use rustc_span::{def_id::DefPathHash, HashStableContext};
4-
use std::fmt;
4+
use std::fmt::{self, Debug};
55

6-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
6+
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
77
#[derive(Encodable, Decodable)]
88
pub struct OwnerId {
99
pub def_id: LocalDefId,
1010
}
1111

12+
impl Debug for OwnerId {
13+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14+
// Example: DefId(0:1 ~ aa[7697]::{use#0})
15+
Debug::fmt(&self.def_id, f)
16+
}
17+
}
18+
1219
impl From<OwnerId> for HirId {
1320
fn from(owner: OwnerId) -> HirId {
1421
HirId { owner, local_id: ItemLocalId::from_u32(0) }
@@ -60,14 +67,22 @@ impl<CTX: HashStableContext> ToStableHashKey<CTX> for OwnerId {
6067
/// the `local_id` part of the `HirId` changing, which is a very useful property in
6168
/// incremental compilation where we have to persist things through changes to
6269
/// the code base.
63-
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
70+
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
6471
#[derive(Encodable, Decodable, HashStable_Generic)]
6572
#[rustc_pass_by_value]
6673
pub struct HirId {
6774
pub owner: OwnerId,
6875
pub local_id: ItemLocalId,
6976
}
7077

78+
impl Debug for HirId {
79+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
80+
// Example: HirId(DefId(0:1 ~ aa[7697]::{use#0}).10)
81+
// Don't use debug_tuple to always keep this on one line.
82+
write!(f, "HirId({:?}.{:?})", self.owner, self.local_id)
83+
}
84+
}
85+
7186
impl HirId {
7287
/// Signal local id which should never be used.
7388
pub const INVALID: HirId =

src/test/ui/thir-tree.stdout

+1-6
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,7 @@ Thir {
3232
kind: Scope {
3333
region_scope: Node(2),
3434
lint_level: Explicit(
35-
HirId {
36-
owner: OwnerId {
37-
def_id: DefId(0:3 ~ thir_tree[8f1d]::main),
38-
},
39-
local_id: 2,
40-
},
35+
HirId(DefId(0:3 ~ thir_tree[8f1d]::main).2),
4136
),
4237
value: e0,
4338
},

0 commit comments

Comments
 (0)