Skip to content

Commit d2642ab

Browse files
committed
Adjust Map's to_string functionality.
`Map::node_to_string` just calls the free function `hir_id_to_string`. This commit removes the former and changes the latter into a `TyCtxt` method.
1 parent 6650252 commit d2642ab

File tree

8 files changed

+132
-131
lines changed

8 files changed

+132
-131
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
298298
// Combine the diverging and has_error flags.
299299
self.diverges.set(self.diverges.get() | old_diverges);
300300

301-
debug!("type of {} is...", self.tcx.hir().node_to_string(expr.hir_id));
301+
debug!("type of {} is...", self.tcx.hir_id_to_string(expr.hir_id));
302302
debug!("... {:?}, expected is {:?}", ty, expected);
303303

304304
ty

compiler/rustc_hir_typeck/src/expr_use_visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1235,7 +1235,7 @@ impl<'tcx, Cx: TypeInformationCtxt<'tcx>, D: Delegate<'tcx>> ExprUseVisitor<'tcx
12351235
self.cx.tainted_by_errors()?;
12361236
bug!(
12371237
"no type for node {} in mem_categorization",
1238-
self.cx.tcx().hir().node_to_string(id)
1238+
self.cx.tcx().hir_id_to_string(id)
12391239
);
12401240
}
12411241
}

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
140140

141141
pub(crate) fn local_ty(&self, span: Span, nid: HirId) -> Ty<'tcx> {
142142
self.locals.borrow().get(&nid).cloned().unwrap_or_else(|| {
143-
span_bug!(span, "no type for local variable {}", self.tcx.hir().node_to_string(nid))
143+
span_bug!(span, "no type for local variable {}", self.tcx.hir_id_to_string(nid))
144144
})
145145
}
146146

@@ -552,11 +552,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
552552
Some(&t) => t,
553553
None if let Some(e) = self.tainted_by_errors() => Ty::new_error(self.tcx, e),
554554
None => {
555-
bug!(
556-
"no type for node {} in fcx {}",
557-
self.tcx.hir().node_to_string(id),
558-
self.tag()
559-
);
555+
bug!("no type for node {} in fcx {}", self.tcx.hir_id_to_string(id), self.tag());
560556
}
561557
}
562558
}

compiler/rustc_middle/src/hir/map.rs

+117-113
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ impl<'tcx> TyCtxt<'tcx> {
275275
span_bug!(
276276
self.hir().span(hir_id),
277277
"body_owned_by: {} has no associated body",
278-
self.hir().node_to_string(hir_id)
278+
self.hir_id_to_string(hir_id)
279279
);
280280
})
281281
}
@@ -674,6 +674,106 @@ impl<'tcx> TyCtxt<'tcx> {
674674
}
675675
}
676676
}
677+
678+
/// Get a representation of this `id` for debugging purposes.
679+
/// NOTE: Do NOT use this in diagnostics!
680+
pub fn hir_id_to_string(self, id: HirId) -> String {
681+
let path_str = |def_id: LocalDefId| self.def_path_str(def_id);
682+
683+
let span_str = || {
684+
self.sess.source_map().span_to_snippet(Map { tcx: self }.span(id)).unwrap_or_default()
685+
};
686+
let node_str = |prefix| format!("{id} ({prefix} `{}`)", span_str());
687+
688+
match self.hir_node(id) {
689+
Node::Item(item) => {
690+
let item_str = match item.kind {
691+
ItemKind::ExternCrate(..) => "extern crate",
692+
ItemKind::Use(..) => "use",
693+
ItemKind::Static(..) => "static",
694+
ItemKind::Const(..) => "const",
695+
ItemKind::Fn { .. } => "fn",
696+
ItemKind::Macro(..) => "macro",
697+
ItemKind::Mod(..) => "mod",
698+
ItemKind::ForeignMod { .. } => "foreign mod",
699+
ItemKind::GlobalAsm { .. } => "global asm",
700+
ItemKind::TyAlias(..) => "ty",
701+
ItemKind::Enum(..) => "enum",
702+
ItemKind::Struct(..) => "struct",
703+
ItemKind::Union(..) => "union",
704+
ItemKind::Trait(..) => "trait",
705+
ItemKind::TraitAlias(..) => "trait alias",
706+
ItemKind::Impl { .. } => "impl",
707+
};
708+
format!("{id} ({item_str} {})", path_str(item.owner_id.def_id))
709+
}
710+
Node::ForeignItem(item) => {
711+
format!("{id} (foreign item {})", path_str(item.owner_id.def_id))
712+
}
713+
Node::ImplItem(ii) => {
714+
let kind = match ii.kind {
715+
ImplItemKind::Const(..) => "associated constant",
716+
ImplItemKind::Fn(fn_sig, _) => match fn_sig.decl.implicit_self {
717+
ImplicitSelfKind::None => "associated function",
718+
_ => "method",
719+
},
720+
ImplItemKind::Type(_) => "associated type",
721+
};
722+
format!("{id} ({kind} `{}` in {})", ii.ident, path_str(ii.owner_id.def_id))
723+
}
724+
Node::TraitItem(ti) => {
725+
let kind = match ti.kind {
726+
TraitItemKind::Const(..) => "associated constant",
727+
TraitItemKind::Fn(fn_sig, _) => match fn_sig.decl.implicit_self {
728+
ImplicitSelfKind::None => "associated function",
729+
_ => "trait method",
730+
},
731+
TraitItemKind::Type(..) => "associated type",
732+
};
733+
734+
format!("{id} ({kind} `{}` in {})", ti.ident, path_str(ti.owner_id.def_id))
735+
}
736+
Node::Variant(variant) => {
737+
format!("{id} (variant `{}` in {})", variant.ident, path_str(variant.def_id))
738+
}
739+
Node::Field(field) => {
740+
format!("{id} (field `{}` in {})", field.ident, path_str(field.def_id))
741+
}
742+
Node::AnonConst(_) => node_str("const"),
743+
Node::ConstBlock(_) => node_str("const"),
744+
Node::ConstArg(_) => node_str("const"),
745+
Node::Expr(_) => node_str("expr"),
746+
Node::ExprField(_) => node_str("expr field"),
747+
Node::Stmt(_) => node_str("stmt"),
748+
Node::PathSegment(_) => node_str("path segment"),
749+
Node::Ty(_) => node_str("type"),
750+
Node::AssocItemConstraint(_) => node_str("assoc item constraint"),
751+
Node::TraitRef(_) => node_str("trait ref"),
752+
Node::OpaqueTy(_) => node_str("opaque type"),
753+
Node::Pat(_) => node_str("pat"),
754+
Node::TyPat(_) => node_str("pat ty"),
755+
Node::PatField(_) => node_str("pattern field"),
756+
Node::PatExpr(_) => node_str("pattern literal"),
757+
Node::Param(_) => node_str("param"),
758+
Node::Arm(_) => node_str("arm"),
759+
Node::Block(_) => node_str("block"),
760+
Node::Infer(_) => node_str("infer"),
761+
Node::LetStmt(_) => node_str("local"),
762+
Node::Ctor(ctor) => format!(
763+
"{id} (ctor {})",
764+
ctor.ctor_def_id().map_or("<missing path>".into(), |def_id| path_str(def_id)),
765+
),
766+
Node::Lifetime(_) => node_str("lifetime"),
767+
Node::GenericParam(param) => {
768+
format!("{id} (generic_param {})", path_str(param.def_id))
769+
}
770+
Node::Crate(..) => String::from("(root_crate)"),
771+
Node::WherePredicate(_) => node_str("where predicate"),
772+
Node::Synthetic => unreachable!(),
773+
Node::Err(_) => node_str("error"),
774+
Node::PreciseCapturingNonLifetimeArg(_param) => node_str("parameter"),
775+
}
776+
}
677777
}
678778

679779
impl<'hir> Map<'hir> {
@@ -686,28 +786,34 @@ impl<'hir> Map<'hir> {
686786
}
687787
bug!(
688788
"expected foreign mod or inlined parent, found {}",
689-
self.node_to_string(HirId::make_owner(parent.def_id))
789+
self.tcx.hir_id_to_string(HirId::make_owner(parent.def_id))
690790
)
691791
}
692792

693793
pub fn expect_item(self, id: LocalDefId) -> &'hir Item<'hir> {
694794
match self.tcx.expect_hir_owner_node(id) {
695795
OwnerNode::Item(item) => item,
696-
_ => bug!("expected item, found {}", self.node_to_string(HirId::make_owner(id))),
796+
_ => bug!("expected item, found {}", self.tcx.hir_id_to_string(HirId::make_owner(id))),
697797
}
698798
}
699799

700800
pub fn expect_impl_item(self, id: LocalDefId) -> &'hir ImplItem<'hir> {
701801
match self.tcx.expect_hir_owner_node(id) {
702802
OwnerNode::ImplItem(item) => item,
703-
_ => bug!("expected impl item, found {}", self.node_to_string(HirId::make_owner(id))),
803+
_ => bug!(
804+
"expected impl item, found {}",
805+
self.tcx.hir_id_to_string(HirId::make_owner(id))
806+
),
704807
}
705808
}
706809

707810
pub fn expect_trait_item(self, id: LocalDefId) -> &'hir TraitItem<'hir> {
708811
match self.tcx.expect_hir_owner_node(id) {
709812
OwnerNode::TraitItem(item) => item,
710-
_ => bug!("expected trait item, found {}", self.node_to_string(HirId::make_owner(id))),
813+
_ => bug!(
814+
"expected trait item, found {}",
815+
self.tcx.hir_id_to_string(HirId::make_owner(id))
816+
),
711817
}
712818
}
713819

@@ -718,14 +824,14 @@ impl<'hir> Map<'hir> {
718824
pub fn expect_variant(self, id: HirId) -> &'hir Variant<'hir> {
719825
match self.tcx.hir_node(id) {
720826
Node::Variant(variant) => variant,
721-
_ => bug!("expected variant, found {}", self.node_to_string(id)),
827+
_ => bug!("expected variant, found {}", self.tcx.hir_id_to_string(id)),
722828
}
723829
}
724830

725831
pub fn expect_field(self, id: HirId) -> &'hir FieldDef<'hir> {
726832
match self.tcx.hir_node(id) {
727833
Node::Field(field) => field,
728-
_ => bug!("expected field, found {}", self.node_to_string(id)),
834+
_ => bug!("expected field, found {}", self.tcx.hir_id_to_string(id)),
729835
}
730836
}
731837

@@ -735,7 +841,7 @@ impl<'hir> Map<'hir> {
735841
_ => {
736842
bug!(
737843
"expected foreign item, found {}",
738-
self.node_to_string(HirId::make_owner(id.def_id))
844+
self.tcx.hir_id_to_string(HirId::make_owner(id.def_id))
739845
)
740846
}
741847
}
@@ -748,7 +854,7 @@ impl<'hir> Map<'hir> {
748854
_ => {
749855
bug!(
750856
"expected opaque type definition, found {}",
751-
self.node_to_string(self.tcx.local_def_id_to_hir_id(id))
857+
self.tcx.hir_id_to_string(self.tcx.local_def_id_to_hir_id(id))
752858
)
753859
}
754860
}
@@ -757,7 +863,7 @@ impl<'hir> Map<'hir> {
757863
pub fn expect_expr(self, id: HirId) -> &'hir Expr<'hir> {
758864
match self.tcx.hir_node(id) {
759865
Node::Expr(expr) => expr,
760-
_ => bug!("expected expr, found {}", self.node_to_string(id)),
866+
_ => bug!("expected expr, found {}", self.tcx.hir_id_to_string(id)),
761867
}
762868
}
763869

@@ -796,7 +902,7 @@ impl<'hir> Map<'hir> {
796902
}
797903

798904
pub fn name(self, id: HirId) -> Symbol {
799-
self.opt_name(id).unwrap_or_else(|| bug!("no name for {}", self.node_to_string(id)))
905+
self.opt_name(id).unwrap_or_else(|| bug!("no name for {}", self.tcx.hir_id_to_string(id)))
800906
}
801907

802908
/// Given a node ID, gets a list of attributes associated with the AST
@@ -977,12 +1083,6 @@ impl<'hir> Map<'hir> {
9771083
}
9781084
}
9791085

980-
/// Get a representation of this `id` for debugging purposes.
981-
/// NOTE: Do NOT use this in diagnostics!
982-
pub fn node_to_string(self, id: HirId) -> String {
983-
hir_id_to_string(self, id)
984-
}
985-
9861086
/// Returns the HirId of `N` in `struct Foo<const N: usize = { ... }>` when
9871087
/// called with the HirId for the `{ ... }` anon const
9881088
pub fn opt_const_param_default_param_def_id(self, anon_const: HirId) -> Option<LocalDefId> {
@@ -1147,102 +1247,6 @@ fn upstream_crates(tcx: TyCtxt<'_>) -> Vec<(StableCrateId, Svh)> {
11471247
upstream_crates
11481248
}
11491249

1150-
fn hir_id_to_string(map: Map<'_>, id: HirId) -> String {
1151-
let path_str = |def_id: LocalDefId| map.tcx.def_path_str(def_id);
1152-
1153-
let span_str = || map.tcx.sess.source_map().span_to_snippet(map.span(id)).unwrap_or_default();
1154-
let node_str = |prefix| format!("{id} ({prefix} `{}`)", span_str());
1155-
1156-
match map.tcx.hir_node(id) {
1157-
Node::Item(item) => {
1158-
let item_str = match item.kind {
1159-
ItemKind::ExternCrate(..) => "extern crate",
1160-
ItemKind::Use(..) => "use",
1161-
ItemKind::Static(..) => "static",
1162-
ItemKind::Const(..) => "const",
1163-
ItemKind::Fn { .. } => "fn",
1164-
ItemKind::Macro(..) => "macro",
1165-
ItemKind::Mod(..) => "mod",
1166-
ItemKind::ForeignMod { .. } => "foreign mod",
1167-
ItemKind::GlobalAsm { .. } => "global asm",
1168-
ItemKind::TyAlias(..) => "ty",
1169-
ItemKind::Enum(..) => "enum",
1170-
ItemKind::Struct(..) => "struct",
1171-
ItemKind::Union(..) => "union",
1172-
ItemKind::Trait(..) => "trait",
1173-
ItemKind::TraitAlias(..) => "trait alias",
1174-
ItemKind::Impl { .. } => "impl",
1175-
};
1176-
format!("{id} ({item_str} {})", path_str(item.owner_id.def_id))
1177-
}
1178-
Node::ForeignItem(item) => {
1179-
format!("{id} (foreign item {})", path_str(item.owner_id.def_id))
1180-
}
1181-
Node::ImplItem(ii) => {
1182-
let kind = match ii.kind {
1183-
ImplItemKind::Const(..) => "associated constant",
1184-
ImplItemKind::Fn(fn_sig, _) => match fn_sig.decl.implicit_self {
1185-
ImplicitSelfKind::None => "associated function",
1186-
_ => "method",
1187-
},
1188-
ImplItemKind::Type(_) => "associated type",
1189-
};
1190-
format!("{id} ({kind} `{}` in {})", ii.ident, path_str(ii.owner_id.def_id))
1191-
}
1192-
Node::TraitItem(ti) => {
1193-
let kind = match ti.kind {
1194-
TraitItemKind::Const(..) => "associated constant",
1195-
TraitItemKind::Fn(fn_sig, _) => match fn_sig.decl.implicit_self {
1196-
ImplicitSelfKind::None => "associated function",
1197-
_ => "trait method",
1198-
},
1199-
TraitItemKind::Type(..) => "associated type",
1200-
};
1201-
1202-
format!("{id} ({kind} `{}` in {})", ti.ident, path_str(ti.owner_id.def_id))
1203-
}
1204-
Node::Variant(variant) => {
1205-
format!("{id} (variant `{}` in {})", variant.ident, path_str(variant.def_id))
1206-
}
1207-
Node::Field(field) => {
1208-
format!("{id} (field `{}` in {})", field.ident, path_str(field.def_id))
1209-
}
1210-
Node::AnonConst(_) => node_str("const"),
1211-
Node::ConstBlock(_) => node_str("const"),
1212-
Node::ConstArg(_) => node_str("const"),
1213-
Node::Expr(_) => node_str("expr"),
1214-
Node::ExprField(_) => node_str("expr field"),
1215-
Node::Stmt(_) => node_str("stmt"),
1216-
Node::PathSegment(_) => node_str("path segment"),
1217-
Node::Ty(_) => node_str("type"),
1218-
Node::AssocItemConstraint(_) => node_str("assoc item constraint"),
1219-
Node::TraitRef(_) => node_str("trait ref"),
1220-
Node::OpaqueTy(_) => node_str("opaque type"),
1221-
Node::Pat(_) => node_str("pat"),
1222-
Node::TyPat(_) => node_str("pat ty"),
1223-
Node::PatField(_) => node_str("pattern field"),
1224-
Node::PatExpr(_) => node_str("pattern literal"),
1225-
Node::Param(_) => node_str("param"),
1226-
Node::Arm(_) => node_str("arm"),
1227-
Node::Block(_) => node_str("block"),
1228-
Node::Infer(_) => node_str("infer"),
1229-
Node::LetStmt(_) => node_str("local"),
1230-
Node::Ctor(ctor) => format!(
1231-
"{id} (ctor {})",
1232-
ctor.ctor_def_id().map_or("<missing path>".into(), |def_id| path_str(def_id)),
1233-
),
1234-
Node::Lifetime(_) => node_str("lifetime"),
1235-
Node::GenericParam(param) => {
1236-
format!("{id} (generic_param {})", path_str(param.def_id))
1237-
}
1238-
Node::Crate(..) => String::from("(root_crate)"),
1239-
Node::WherePredicate(_) => node_str("where predicate"),
1240-
Node::Synthetic => unreachable!(),
1241-
Node::Err(_) => node_str("error"),
1242-
Node::PreciseCapturingNonLifetimeArg(_param) => node_str("parameter"),
1243-
}
1244-
}
1245-
12461250
pub(super) fn hir_module_items(tcx: TyCtxt<'_>, module_id: LocalModDefId) -> ModuleItems {
12471251
let mut collector = ItemCollector::new(tcx, false);
12481252

compiler/rustc_middle/src/ty/context.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -3118,9 +3118,11 @@ impl<'tcx> TyCtxt<'tcx> {
31183118

31193119
pub fn late_bound_vars(self, id: HirId) -> &'tcx List<ty::BoundVariableKind> {
31203120
self.mk_bound_variable_kinds(
3121-
&self.late_bound_vars_map(id.owner).get(&id.local_id).cloned().unwrap_or_else(|| {
3122-
bug!("No bound vars found for {}", self.hir().node_to_string(id))
3123-
}),
3121+
&self
3122+
.late_bound_vars_map(id.owner)
3123+
.get(&id.local_id)
3124+
.cloned()
3125+
.unwrap_or_else(|| bug!("No bound vars found for {}", self.hir_id_to_string(id))),
31243126
)
31253127
}
31263128

compiler/rustc_middle/src/ty/typeck_results.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl<'tcx> TypeckResults<'tcx> {
310310

311311
pub fn node_type(&self, id: HirId) -> Ty<'tcx> {
312312
self.node_type_opt(id).unwrap_or_else(|| {
313-
bug!("node_type: no type for node {}", tls::with(|tcx| tcx.hir().node_to_string(id)))
313+
bug!("node_type: no type for node {}", tls::with(|tcx| tcx.hir_id_to_string(id)))
314314
})
315315
}
316316

@@ -554,7 +554,7 @@ fn invalid_hir_id_for_typeck_results(hir_owner: OwnerId, hir_id: HirId) {
554554
ty::tls::with(|tcx| {
555555
bug!(
556556
"node {} cannot be placed in TypeckResults with hir_owner {:?}",
557-
tcx.hir().node_to_string(hir_id),
557+
tcx.hir_id_to_string(hir_id),
558558
hir_owner
559559
)
560560
});

0 commit comments

Comments
 (0)