Skip to content

Commit f1701dd

Browse files
committed
rustc: remove rustc_hir_pretty dependency.
1 parent 92885e3 commit f1701dd

File tree

17 files changed

+101
-99
lines changed

17 files changed

+101
-99
lines changed

Cargo.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -3114,7 +3114,6 @@ dependencies = [
31143114
"rustc_errors",
31153115
"rustc_feature",
31163116
"rustc_hir",
3117-
"rustc_hir_pretty",
31183117
"rustc_index",
31193118
"rustc_macros",
31203119
"rustc_query_system",
@@ -4086,6 +4085,7 @@ dependencies = [
40864085
"rustc_ast_pretty",
40874086
"rustc_data_structures",
40884087
"rustc_hir",
4088+
"rustc_hir_pretty",
40894089
"rustc_parse",
40904090
"rustc_session",
40914091
"rustc_span",

src/librustc/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ rustc_apfloat = { path = "../librustc_apfloat" }
2222
rustc_attr = { path = "../librustc_attr" }
2323
rustc_feature = { path = "../librustc_feature" }
2424
rustc_hir = { path = "../librustc_hir" }
25-
rustc_hir_pretty = { path = "../librustc_hir_pretty" }
2625
rustc_target = { path = "../librustc_target" }
2726
rustc_macros = { path = "../librustc_macros" }
2827
rustc_data_structures = { path = "../librustc_data_structures" }

src/librustc/hir/map/mod.rs

+24-41
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ pub use rustc_hir::definitions::{Definitions, DisambiguatedDefPathData};
1313
use rustc_hir::intravisit;
1414
use rustc_hir::itemlikevisit::ItemLikeVisitor;
1515
use rustc_hir::*;
16-
use rustc_hir_pretty::Nested;
1716
use rustc_index::vec::IndexVec;
1817
use rustc_span::hygiene::MacroKind;
1918
use rustc_span::source_map::Spanned;
@@ -954,20 +953,18 @@ impl<'hir> Map<'hir> {
954953
}
955954
}
956955

956+
/// Get a representation of this `id` for debugging purposes.
957+
/// NOTE: Do NOT use this in diagnostics!
957958
pub fn node_to_string(&self, id: HirId) -> String {
958-
hir_id_to_string(self, id, true)
959-
}
960-
961-
pub fn hir_to_user_string(&self, id: HirId) -> String {
962-
hir_id_to_string(self, id, false)
963-
}
964-
965-
pub fn hir_to_pretty_string(&self, id: HirId) -> String {
966-
rustc_hir_pretty::to_string(self, |s| s.print_node(self.get(id)))
959+
hir_id_to_string(self, id)
967960
}
968961
}
969962

970963
impl<'hir> intravisit::Map<'hir> for Map<'hir> {
964+
fn find(&self, hir_id: HirId) -> Option<Node<'hir>> {
965+
self.find(hir_id)
966+
}
967+
971968
fn body(&self, id: BodyId) -> &'hir Body<'hir> {
972969
self.body(id)
973970
}
@@ -1046,23 +1043,8 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> &'tcx Indexe
10461043
tcx.arena.alloc(IndexedHir { crate_hash, map })
10471044
}
10481045

1049-
/// Identical to the `PpAnn` implementation for `hir::Crate`,
1050-
/// except it avoids creating a dependency on the whole crate.
1051-
impl<'hir> rustc_hir_pretty::PpAnn for Map<'hir> {
1052-
fn nested(&self, state: &mut rustc_hir_pretty::State<'_>, nested: rustc_hir_pretty::Nested) {
1053-
match nested {
1054-
Nested::Item(id) => state.print_item(self.expect_item(id.id)),
1055-
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
1056-
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
1057-
Nested::Body(id) => state.print_expr(&self.body(id).value),
1058-
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
1059-
}
1060-
}
1061-
}
1062-
1063-
fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
1046+
fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
10641047
let id_str = format!(" (hir_id={})", id);
1065-
let id_str = if include_id { &id_str[..] } else { "" };
10661048

10671049
let path_str = || {
10681050
// This functionality is used for debugging, try to use `TyCtxt` to get
@@ -1083,6 +1065,9 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
10831065
})
10841066
};
10851067

1068+
let span_str = || map.tcx.sess.source_map().span_to_snippet(map.span(id)).unwrap_or_default();
1069+
let node_str = |prefix| format!("{} {}{}", prefix, span_str(), id_str);
1070+
10861071
match map.find(id) {
10871072
Some(Node::Item(item)) => {
10881073
let item_str = match item.kind {
@@ -1133,22 +1118,20 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
11331118
Some(Node::Field(ref field)) => {
11341119
format!("field {} in {}{}", field.ident, path_str(), id_str)
11351120
}
1136-
Some(Node::AnonConst(_)) => format!("const {}{}", map.hir_to_pretty_string(id), id_str),
1137-
Some(Node::Expr(_)) => format!("expr {}{}", map.hir_to_pretty_string(id), id_str),
1138-
Some(Node::Stmt(_)) => format!("stmt {}{}", map.hir_to_pretty_string(id), id_str),
1139-
Some(Node::PathSegment(_)) => {
1140-
format!("path segment {}{}", map.hir_to_pretty_string(id), id_str)
1141-
}
1142-
Some(Node::Ty(_)) => format!("type {}{}", map.hir_to_pretty_string(id), id_str),
1143-
Some(Node::TraitRef(_)) => format!("trait_ref {}{}", map.hir_to_pretty_string(id), id_str),
1144-
Some(Node::Binding(_)) => format!("local {}{}", map.hir_to_pretty_string(id), id_str),
1145-
Some(Node::Pat(_)) => format!("pat {}{}", map.hir_to_pretty_string(id), id_str),
1146-
Some(Node::Param(_)) => format!("param {}{}", map.hir_to_pretty_string(id), id_str),
1147-
Some(Node::Arm(_)) => format!("arm {}{}", map.hir_to_pretty_string(id), id_str),
1148-
Some(Node::Block(_)) => format!("block {}{}", map.hir_to_pretty_string(id), id_str),
1149-
Some(Node::Local(_)) => format!("local {}{}", map.hir_to_pretty_string(id), id_str),
1121+
Some(Node::AnonConst(_)) => node_str("const"),
1122+
Some(Node::Expr(_)) => node_str("expr"),
1123+
Some(Node::Stmt(_)) => node_str("stmt"),
1124+
Some(Node::PathSegment(_)) => node_str("path segment"),
1125+
Some(Node::Ty(_)) => node_str("type"),
1126+
Some(Node::TraitRef(_)) => node_str("trait ref"),
1127+
Some(Node::Binding(_)) => node_str("local"),
1128+
Some(Node::Pat(_)) => node_str("pat"),
1129+
Some(Node::Param(_)) => node_str("param"),
1130+
Some(Node::Arm(_)) => node_str("arm"),
1131+
Some(Node::Block(_)) => node_str("block"),
1132+
Some(Node::Local(_)) => node_str("local"),
11501133
Some(Node::Ctor(..)) => format!("ctor {}{}", path_str(), id_str),
1151-
Some(Node::Lifetime(_)) => format!("lifetime {}{}", map.hir_to_pretty_string(id), id_str),
1134+
Some(Node::Lifetime(_)) => node_str("lifetime"),
11521135
Some(Node::GenericParam(ref param)) => format!("generic_param {:?}{}", param, id_str),
11531136
Some(Node::Visibility(ref vis)) => format!("visibility {:?}{}", vis, id_str),
11541137
Some(Node::MacroDef(_)) => format!("macro {}{}", path_str(), id_str),

src/librustc_driver/pretty.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl<'hir> pprust::PpAnn for NoAnn<'hir> {}
155155
impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
156156
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
157157
if let Some(tcx) = self.tcx {
158-
pprust_hir::PpAnn::nested(&tcx.hir(), state, nested)
158+
pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
159159
}
160160
}
161161
}
@@ -228,7 +228,7 @@ impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
228228
impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
229229
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
230230
if let Some(ref tcx) = self.tcx {
231-
pprust_hir::PpAnn::nested(&tcx.hir(), state, nested)
231+
pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
232232
}
233233
}
234234
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
@@ -334,7 +334,8 @@ impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
334334
if let pprust_hir::Nested::Body(id) = nested {
335335
self.tables.set(self.tcx.body_tables(id));
336336
}
337-
pprust_hir::PpAnn::nested(&self.tcx.hir(), state, nested);
337+
let pp_ann = &(&self.tcx.hir() as &dyn hir::intravisit::Map<'_>);
338+
pprust_hir::PpAnn::nested(pp_ann, state, nested);
338339
self.tables.set(old_tables);
339340
}
340341
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {

src/librustc_hir/intravisit.rs

+5
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ impl<'a> FnKind<'a> {
121121

122122
/// An abstract representation of the HIR `rustc::hir::map::Map`.
123123
pub trait Map<'hir> {
124+
/// Retrieves the `Node` corresponding to `id`, returning `None` if cannot be found.
125+
fn find(&self, hir_id: HirId) -> Option<Node<'hir>>;
124126
fn body(&self, id: BodyId) -> &'hir Body<'hir>;
125127
fn item(&self, id: HirId) -> &'hir Item<'hir>;
126128
fn trait_item(&self, id: TraitItemId) -> &'hir TraitItem<'hir>;
@@ -132,6 +134,9 @@ pub trait Map<'hir> {
132134
pub struct ErasedMap<'hir>(&'hir dyn Map<'hir>);
133135

134136
impl<'hir> Map<'hir> for ErasedMap<'hir> {
137+
fn find(&self, _: HirId) -> Option<Node<'hir>> {
138+
None
139+
}
135140
fn body(&self, id: BodyId) -> &'hir Body<'hir> {
136141
self.0.body(id)
137142
}

src/librustc_hir_pretty/lib.rs

+18
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ use std::borrow::Cow;
1515
use std::cell::Cell;
1616
use std::vec;
1717

18+
pub fn id_to_string(map: &dyn rustc_hir::intravisit::Map<'_>, hir_id: hir::HirId) -> String {
19+
to_string(&map, |s| s.print_node(map.find(hir_id).unwrap()))
20+
}
21+
1822
pub enum AnnNode<'a> {
1923
Name(&'a ast::Name),
2024
Block(&'a hir::Block<'a>),
@@ -61,6 +65,20 @@ impl PpAnn for hir::Crate<'_> {
6165
}
6266
}
6367

68+
/// Identical to the `PpAnn` implementation for `hir::Crate`,
69+
/// except it avoids creating a dependency on the whole crate.
70+
impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {
71+
fn nested(&self, state: &mut State<'_>, nested: Nested) {
72+
match nested {
73+
Nested::Item(id) => state.print_item(self.item(id.id)),
74+
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
75+
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
76+
Nested::Body(id) => state.print_expr(&self.body(id).value),
77+
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
78+
}
79+
}
80+
}
81+
6482
pub struct State<'a> {
6583
pub s: pp::Printer,
6684
comments: Option<Comments<'a>>,

src/librustc_metadata/rmeta/encoder.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -829,8 +829,10 @@ impl EncodeContext<'tcx> {
829829

830830
record!(self.per_def.kind[def_id] <- match trait_item.kind {
831831
ty::AssocKind::Const => {
832-
let rendered =
833-
rustc_hir_pretty::to_string(&self.tcx.hir(), |s| s.print_trait_item(ast_item));
832+
let rendered = rustc_hir_pretty::to_string(
833+
&(&self.tcx.hir() as &dyn intravisit::Map<'_>),
834+
|s| s.print_trait_item(ast_item)
835+
);
834836
let rendered_const = self.lazy(RenderedConst(rendered));
835837

836838
EntryKind::AssocConst(
@@ -1047,8 +1049,11 @@ impl EncodeContext<'tcx> {
10471049
}
10481050

10491051
fn encode_rendered_const_for_body(&mut self, body_id: hir::BodyId) -> Lazy<RenderedConst> {
1050-
let body = self.tcx.hir().body(body_id);
1051-
let rendered = rustc_hir_pretty::to_string(&self.tcx.hir(), |s| s.print_expr(&body.value));
1052+
let hir = self.tcx.hir();
1053+
let body = hir.body(body_id);
1054+
let rendered = rustc_hir_pretty::to_string(&(&hir as &dyn intravisit::Map<'_>), |s| {
1055+
s.print_expr(&body.value)
1056+
});
10521057
let rendered_const = &RenderedConst(rendered);
10531058
self.lazy(rendered_const)
10541059
}

src/librustc_passes/liveness.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -903,10 +903,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
903903
}
904904

905905
fn compute(&mut self, body: &hir::Expr<'_>) -> LiveNode {
906-
debug!(
907-
"compute: using id for body, {}",
908-
self.ir.tcx.hir().hir_to_pretty_string(body.hir_id)
909-
);
906+
debug!("compute: using id for body, {:?}", body);
910907

911908
// the fallthrough exit is only for those cases where we do not
912909
// explicitly return:
@@ -979,7 +976,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
979976
}
980977

981978
fn propagate_through_expr(&mut self, expr: &Expr<'_>, succ: LiveNode) -> LiveNode {
982-
debug!("propagate_through_expr: {}", self.ir.tcx.hir().hir_to_pretty_string(expr.hir_id));
979+
debug!("propagate_through_expr: {:?}", expr);
983980

984981
match expr.kind {
985982
// Interesting cases with control flow or which gen/kill
@@ -990,10 +987,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
990987
hir::ExprKind::Field(ref e, _) => self.propagate_through_expr(&e, succ),
991988

992989
hir::ExprKind::Closure(..) => {
993-
debug!(
994-
"{} is an ExprKind::Closure",
995-
self.ir.tcx.hir().hir_to_pretty_string(expr.hir_id)
996-
);
990+
debug!("{:?} is an ExprKind::Closure", expr);
997991

998992
// the construction of a closure itself is not important,
999993
// but we have to consider the closed over variables.
@@ -1344,11 +1338,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
13441338
let mut first_merge = true;
13451339
let ln = self.live_node(expr.hir_id, expr.span);
13461340
self.init_empty(ln, succ);
1347-
debug!(
1348-
"propagate_through_loop: using id for loop body {} {}",
1349-
expr.hir_id,
1350-
self.ir.tcx.hir().hir_to_pretty_string(body.hir_id)
1351-
);
1341+
debug!("propagate_through_loop: using id for loop body {} {:?}", expr.hir_id, body);
13521342

13531343
self.break_ln.insert(expr.hir_id, succ);
13541344

src/librustc_save_analysis/Cargo.toml

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ path = "lib.rs"
1111
[dependencies]
1212
log = "0.4"
1313
rustc = { path = "../librustc" }
14+
rustc_ast = { path = "../librustc_ast" }
1415
rustc_ast_pretty = { path = "../librustc_ast_pretty" }
1516
rustc_data_structures = { path = "../librustc_data_structures" }
16-
rustc_session = { path = "../librustc_session" }
1717
rustc_hir = { path = "../librustc_hir" }
18+
rustc_hir_pretty = { path = "../librustc_hir_pretty" }
1819
rustc_parse = { path = "../librustc_parse" }
1920
serde_json = "1"
20-
rustc_ast = { path = "../librustc_ast" }
21+
rustc_session = { path = "../librustc_session" }
2122
rustc_span = { path = "../librustc_span" }
2223
rls-data = "0.19"
2324
rls-span = "0.5"

src/librustc_save_analysis/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -404,14 +404,15 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
404404
Some(impl_id) => match self.tcx.hir().get_if_local(impl_id) {
405405
Some(Node::Item(item)) => match item.kind {
406406
hir::ItemKind::Impl { ref self_ty, .. } => {
407+
let hir = self.tcx.hir();
408+
407409
let mut qualname = String::from("<");
408-
qualname.push_str(&self.tcx.hir().hir_to_pretty_string(self_ty.hir_id));
410+
qualname.push_str(&rustc_hir_pretty::id_to_string(&hir, self_ty.hir_id));
409411

410412
let trait_id = self.tcx.trait_id_of_impl(impl_id);
411413
let mut docs = String::new();
412414
let mut attrs = vec![];
413-
let hir_id = self.tcx.hir().node_to_hir_id(id);
414-
if let Some(Node::ImplItem(item)) = self.tcx.hir().find(hir_id) {
415+
if let Some(Node::ImplItem(item)) = hir.find(hir.node_to_hir_id(id)) {
415416
docs = self.docs_for_attrs(&item.attrs);
416417
attrs = item.attrs.to_vec();
417418
}

src/librustc_typeck/check/callee.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
265265
if let &ty::Adt(adt_def, ..) = t {
266266
if adt_def.is_enum() {
267267
if let hir::ExprKind::Call(ref expr, _) = call_expr.kind {
268-
unit_variant = Some(self.tcx.hir().hir_to_pretty_string(expr.hir_id))
268+
unit_variant =
269+
self.tcx.sess.source_map().span_to_snippet(expr.span).ok();
269270
}
270271
}
271272
}

src/librustc_typeck/check/expr.rs

+15-23
Original file line numberDiff line numberDiff line change
@@ -1671,20 +1671,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16711671
if let (Some(len), Ok(user_index)) =
16721672
(len.try_eval_usize(self.tcx, self.param_env), field.as_str().parse::<u64>())
16731673
{
1674-
let base = self
1675-
.tcx
1676-
.sess
1677-
.source_map()
1678-
.span_to_snippet(base.span)
1679-
.unwrap_or_else(|_| self.tcx.hir().hir_to_pretty_string(base.hir_id));
1680-
let help = "instead of using tuple indexing, use array indexing";
1681-
let suggestion = format!("{}[{}]", base, field);
1682-
let applicability = if len < user_index {
1683-
Applicability::MachineApplicable
1684-
} else {
1685-
Applicability::MaybeIncorrect
1686-
};
1687-
err.span_suggestion(expr.span, help, suggestion, applicability);
1674+
if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span) {
1675+
let help = "instead of using tuple indexing, use array indexing";
1676+
let suggestion = format!("{}[{}]", base, field);
1677+
let applicability = if len < user_index {
1678+
Applicability::MachineApplicable
1679+
} else {
1680+
Applicability::MaybeIncorrect
1681+
};
1682+
err.span_suggestion(expr.span, help, suggestion, applicability);
1683+
}
16881684
}
16891685
}
16901686

@@ -1695,15 +1691,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16951691
base: &hir::Expr<'_>,
16961692
field: ast::Ident,
16971693
) {
1698-
let base = self
1699-
.tcx
1700-
.sess
1701-
.source_map()
1702-
.span_to_snippet(base.span)
1703-
.unwrap_or_else(|_| self.tcx.hir().hir_to_pretty_string(base.hir_id));
1704-
let msg = format!("`{}` is a raw pointer; try dereferencing it", base);
1705-
let suggestion = format!("(*{}).{}", base, field);
1706-
err.span_suggestion(expr.span, &msg, suggestion, Applicability::MaybeIncorrect);
1694+
if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span) {
1695+
let msg = format!("`{}` is a raw pointer; try dereferencing it", base);
1696+
let suggestion = format!("(*{}).{}", base, field);
1697+
err.span_suggestion(expr.span, &msg, suggestion, Applicability::MaybeIncorrect);
1698+
}
17071699
}
17081700

17091701
fn no_such_field_err<T: Display>(

0 commit comments

Comments
 (0)