Skip to content

Commit 230c9e9

Browse files
authored
Rollup merge of #107515 - Swatinem:hirvalidator, r=compiler-errors
Improve pretty-printing of `HirIdValidator` errors This now uses `node_to_string` for both missing and seen Ids, which includes the snippet of code for which the Id was allocated. Also removes the duplicated printing of `HirId`, as `node_to_string` also includes that.
2 parents e9c4e29 + 3a75f10 commit 230c9e9

File tree

7 files changed

+32
-49
lines changed

7 files changed

+32
-49
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
454454
None if let Some(e) = self.tainted_by_errors() => self.tcx.ty_error_with_guaranteed(e),
455455
None => {
456456
bug!(
457-
"no type for node {}: {} in fcx {}",
458-
id,
457+
"no type for node {} in fcx {}",
459458
self.tcx.hir().node_to_string(id),
460459
self.tag()
461460
);

compiler/rustc_hir_typeck/src/mem_categorization.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ impl<'a, 'tcx> MemCategorizationContext<'a, 'tcx> {
155155
None if self.is_tainted_by_errors() => Err(()),
156156
None => {
157157
bug!(
158-
"no type for node {}: {} in mem_categorization",
159-
id,
158+
"no type for node {} in mem_categorization",
160159
self.tcx().hir().node_to_string(id)
161160
);
162161
}

compiler/rustc_middle/src/hir/map/mod.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ impl<'hir> Map<'hir> {
290290
#[track_caller]
291291
pub fn parent_id(self, hir_id: HirId) -> HirId {
292292
self.opt_parent_id(hir_id)
293-
.unwrap_or_else(|| bug!("No parent for node {:?}", self.node_to_string(hir_id)))
293+
.unwrap_or_else(|| bug!("No parent for node {}", self.node_to_string(hir_id)))
294294
}
295295

296296
pub fn get_parent(self, hir_id: HirId) -> Node<'hir> {
@@ -1191,12 +1191,10 @@ fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
11911191
}
11921192

11931193
fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
1194-
let id_str = format!(" (hir_id={})", id);
1195-
11961194
let path_str = |def_id: LocalDefId| map.tcx.def_path_str(def_id.to_def_id());
11971195

11981196
let span_str = || map.tcx.sess.source_map().span_to_snippet(map.span(id)).unwrap_or_default();
1199-
let node_str = |prefix| format!("{} {}{}", prefix, span_str(), id_str);
1197+
let node_str = |prefix| format!("{id} ({prefix} `{}`)", span_str());
12001198

12011199
match map.find(id) {
12021200
Some(Node::Item(item)) => {
@@ -1225,18 +1223,18 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
12251223
ItemKind::TraitAlias(..) => "trait alias",
12261224
ItemKind::Impl { .. } => "impl",
12271225
};
1228-
format!("{} {}{}", item_str, path_str(item.owner_id.def_id), id_str)
1226+
format!("{id} ({item_str} {})", path_str(item.owner_id.def_id))
12291227
}
12301228
Some(Node::ForeignItem(item)) => {
1231-
format!("foreign item {}{}", path_str(item.owner_id.def_id), id_str)
1229+
format!("{id} (foreign item {})", path_str(item.owner_id.def_id))
12321230
}
12331231
Some(Node::ImplItem(ii)) => {
12341232
let kind = match ii.kind {
12351233
ImplItemKind::Const(..) => "assoc const",
12361234
ImplItemKind::Fn(..) => "method",
12371235
ImplItemKind::Type(_) => "assoc type",
12381236
};
1239-
format!("{} {} in {}{}", kind, ii.ident, path_str(ii.owner_id.def_id), id_str)
1237+
format!("{id} ({kind} `{}` in {})", ii.ident, path_str(ii.owner_id.def_id))
12401238
}
12411239
Some(Node::TraitItem(ti)) => {
12421240
let kind = match ti.kind {
@@ -1245,13 +1243,13 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
12451243
TraitItemKind::Type(..) => "assoc type",
12461244
};
12471245

1248-
format!("{} {} in {}{}", kind, ti.ident, path_str(ti.owner_id.def_id), id_str)
1246+
format!("{id} ({kind} `{}` in {})", ti.ident, path_str(ti.owner_id.def_id))
12491247
}
12501248
Some(Node::Variant(ref variant)) => {
1251-
format!("variant {} in {}{}", variant.ident, path_str(variant.def_id), id_str)
1249+
format!("{id} (variant `{}` in {})", variant.ident, path_str(variant.def_id))
12521250
}
12531251
Some(Node::Field(ref field)) => {
1254-
format!("field {} in {}{}", field.ident, path_str(field.def_id), id_str)
1252+
format!("{id} (field `{}` in {})", field.ident, path_str(field.def_id))
12551253
}
12561254
Some(Node::AnonConst(_)) => node_str("const"),
12571255
Some(Node::Expr(_)) => node_str("expr"),
@@ -1269,16 +1267,15 @@ fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
12691267
Some(Node::Infer(_)) => node_str("infer"),
12701268
Some(Node::Local(_)) => node_str("local"),
12711269
Some(Node::Ctor(ctor)) => format!(
1272-
"ctor {}{}",
1270+
"{id} (ctor {})",
12731271
ctor.ctor_def_id().map_or("<missing path>".into(), |def_id| path_str(def_id)),
1274-
id_str
12751272
),
12761273
Some(Node::Lifetime(_)) => node_str("lifetime"),
12771274
Some(Node::GenericParam(ref param)) => {
1278-
format!("generic_param {}{}", path_str(param.def_id), id_str)
1275+
format!("{id} (generic_param {})", path_str(param.def_id))
12791276
}
1280-
Some(Node::Crate(..)) => String::from("root_crate"),
1281-
None => format!("unknown node{}", id_str),
1277+
Some(Node::Crate(..)) => String::from("(root_crate)"),
1278+
None => format!("{id} (unknown node)"),
12821279
}
12831280
}
12841281

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2171,7 +2171,7 @@ impl<'tcx> TyCtxt<'tcx> {
21712171
self.late_bound_vars_map(id.owner)
21722172
.and_then(|map| map.get(&id.local_id).cloned())
21732173
.unwrap_or_else(|| {
2174-
bug!("No bound vars found for {:?} ({:?})", self.hir().node_to_string(id), id)
2174+
bug!("No bound vars found for {}", self.hir().node_to_string(id))
21752175
})
21762176
.iter(),
21772177
)

compiler/rustc_middle/src/ty/typeck_results.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl<'tcx> TypeckResults<'tcx> {
372372

373373
pub fn node_type(&self, id: hir::HirId) -> Ty<'tcx> {
374374
self.node_type_opt(id).unwrap_or_else(|| {
375-
bug!("node_type: no type for node `{}`", tls::with(|tcx| tcx.hir().node_to_string(id)))
375+
bug!("node_type: no type for node {}", tls::with(|tcx| tcx.hir().node_to_string(id)))
376376
})
377377
}
378378

@@ -551,9 +551,8 @@ fn validate_hir_id_for_typeck_results(hir_owner: OwnerId, hir_id: hir::HirId) {
551551
fn invalid_hir_id_for_typeck_results(hir_owner: OwnerId, hir_id: hir::HirId) {
552552
ty::tls::with(|tcx| {
553553
bug!(
554-
"node {} with HirId::owner {:?} cannot be placed in TypeckResults with hir_owner {:?}",
554+
"node {} cannot be placed in TypeckResults with hir_owner {:?}",
555555
tcx.hir().node_to_string(hir_id),
556-
hir_id.owner,
557556
hir_owner
558557
)
559558
});

compiler/rustc_passes/src/hir_id_validator.rs

+14-25
Original file line numberDiff line numberDiff line change
@@ -74,37 +74,26 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> {
7474
.expect("owning item has no entry");
7575

7676
if max != self.hir_ids_seen.len() - 1 {
77-
// Collect the missing ItemLocalIds
78-
let missing: Vec<_> = (0..=max as u32)
79-
.filter(|&i| !self.hir_ids_seen.contains(ItemLocalId::from_u32(i)))
80-
.collect();
81-
82-
// Try to map those to something more useful
83-
let mut missing_items = Vec::with_capacity(missing.len());
77+
let hir = self.tcx.hir();
78+
let pretty_owner = hir.def_path(owner.def_id).to_string_no_crate_verbose();
8479

85-
for local_id in missing {
86-
let hir_id = HirId { owner, local_id: ItemLocalId::from_u32(local_id) };
80+
let missing_items: Vec<_> = (0..=max as u32)
81+
.map(|i| ItemLocalId::from_u32(i))
82+
.filter(|&local_id| !self.hir_ids_seen.contains(local_id))
83+
.map(|local_id| hir.node_to_string(HirId { owner, local_id }))
84+
.collect();
8785

88-
trace!("missing hir id {:#?}", hir_id);
86+
let seen_items: Vec<_> = self
87+
.hir_ids_seen
88+
.iter()
89+
.map(|local_id| hir.node_to_string(HirId { owner, local_id }))
90+
.collect();
8991

90-
missing_items.push(format!(
91-
"[local_id: {}, owner: {}]",
92-
local_id,
93-
self.tcx.hir().def_path(owner.def_id).to_string_no_crate_verbose()
94-
));
95-
}
9692
self.error(|| {
9793
format!(
9894
"ItemLocalIds not assigned densely in {}. \
99-
Max ItemLocalId = {}, missing IDs = {:#?}; seens IDs = {:#?}",
100-
self.tcx.hir().def_path(owner.def_id).to_string_no_crate_verbose(),
101-
max,
102-
missing_items,
103-
self.hir_ids_seen
104-
.iter()
105-
.map(|local_id| HirId { owner, local_id })
106-
.map(|h| format!("({:?} {})", h, self.tcx.hir().node_to_string(h)))
107-
.collect::<Vec<_>>()
95+
Max ItemLocalId = {}, missing IDs = {:#?}; seen IDs = {:#?}",
96+
pretty_owner, max, missing_items, seen_items
10897
)
10998
});
11099
}

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl<'tcx, 'a> GeneratorData<'tcx, 'a> {
133133
.cloned()
134134
.unwrap_or_else(|| {
135135
bug!(
136-
"node_type: no type for node `{}`",
136+
"node_type: no type for node {}",
137137
ty::tls::with(|tcx| tcx
138138
.hir()
139139
.node_to_string(await_expr.hir_id))

0 commit comments

Comments
 (0)