Skip to content

Commit cbe0980

Browse files
authored
Rollup merge of rust-lang#70344 - Centril:hir-pretty, r=eddyb
Decouple `rustc_hir::print` into `rustc_hir_pretty` High level summary: - The HIR pretty printer, `rustc_hir::print` is moved into a new crate `rustc_hir_pretty`. - `rustc_ast_pretty` and `rustc_errors` are dropped as `rustc_hir` dependencies. - The dependence on HIR pretty is generally reduced, leaving `rustc_save_analysis`, `rustdoc`, `rustc_metadata`, and `rustc_driver` as the remaining clients. The main goal here is to reduce `rustc_hir`'s dependencies and its size such that it can start and finish earlier, thereby working towards rust-lang#65031. r? @Zoxc
2 parents 3b1d735 + b514c42 commit cbe0980

File tree

37 files changed

+286
-313
lines changed

37 files changed

+286
-313
lines changed

Cargo.lock

+15-2
Original file line numberDiff line numberDiff line change
@@ -3668,6 +3668,7 @@ dependencies = [
36683668
"rustc_errors",
36693669
"rustc_feature",
36703670
"rustc_hir",
3671+
"rustc_hir_pretty",
36713672
"rustc_interface",
36723673
"rustc_lint",
36733674
"rustc_metadata",
@@ -3742,9 +3743,7 @@ dependencies = [
37423743
"lazy_static 1.4.0",
37433744
"log",
37443745
"rustc_ast",
3745-
"rustc_ast_pretty",
37463746
"rustc_data_structures",
3747-
"rustc_errors",
37483747
"rustc_index",
37493748
"rustc_macros",
37503749
"rustc_span",
@@ -3753,6 +3752,18 @@ dependencies = [
37533752
"smallvec 1.0.0",
37543753
]
37553754

3755+
[[package]]
3756+
name = "rustc_hir_pretty"
3757+
version = "0.0.0"
3758+
dependencies = [
3759+
"rustc_ast",
3760+
"rustc_ast_pretty",
3761+
"rustc_data_structures",
3762+
"rustc_hir",
3763+
"rustc_span",
3764+
"rustc_target",
3765+
]
3766+
37563767
[[package]]
37573768
name = "rustc_incremental"
37583769
version = "0.0.0"
@@ -3903,6 +3914,7 @@ dependencies = [
39033914
"rustc_errors",
39043915
"rustc_expand",
39053916
"rustc_hir",
3917+
"rustc_hir_pretty",
39063918
"rustc_index",
39073919
"rustc_session",
39083920
"rustc_span",
@@ -4087,6 +4099,7 @@ dependencies = [
40874099
"rustc_ast_pretty",
40884100
"rustc_data_structures",
40894101
"rustc_hir",
4102+
"rustc_hir_pretty",
40904103
"rustc_parse",
40914104
"rustc_session",
40924105
"rustc_span",

src/librustc/hir/map/mod.rs

+24-41
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
1010
use rustc_hir::definitions::{DefKey, DefPath, Definitions};
1111
use rustc_hir::intravisit;
1212
use rustc_hir::itemlikevisit::ItemLikeVisitor;
13-
use rustc_hir::print::Nested;
1413
use rustc_hir::*;
1514
use rustc_index::vec::IndexVec;
1615
use rustc_span::hygiene::MacroKind;
@@ -890,20 +889,18 @@ impl<'hir> Map<'hir> {
890889
}
891890
}
892891

892+
/// Get a representation of this `id` for debugging purposes.
893+
/// NOTE: Do NOT use this in diagnostics!
893894
pub fn node_to_string(&self, id: HirId) -> String {
894-
hir_id_to_string(self, id, true)
895-
}
896-
897-
pub fn hir_to_user_string(&self, id: HirId) -> String {
898-
hir_id_to_string(self, id, false)
899-
}
900-
901-
pub fn hir_to_pretty_string(&self, id: HirId) -> String {
902-
print::to_string(self, |s| s.print_node(self.get(id)))
895+
hir_id_to_string(self, id)
903896
}
904897
}
905898

906899
impl<'hir> intravisit::Map<'hir> for Map<'hir> {
900+
fn find(&self, hir_id: HirId) -> Option<Node<'hir>> {
901+
self.find(hir_id)
902+
}
903+
907904
fn body(&self, id: BodyId) -> &'hir Body<'hir> {
908905
self.body(id)
909906
}
@@ -982,23 +979,8 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> &'tcx Indexe
982979
tcx.arena.alloc(IndexedHir { crate_hash, map })
983980
}
984981

985-
/// Identical to the `PpAnn` implementation for `hir::Crate`,
986-
/// except it avoids creating a dependency on the whole crate.
987-
impl<'hir> print::PpAnn for Map<'hir> {
988-
fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) {
989-
match nested {
990-
Nested::Item(id) => state.print_item(self.expect_item(id.id)),
991-
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
992-
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
993-
Nested::Body(id) => state.print_expr(&self.body(id).value),
994-
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
995-
}
996-
}
997-
}
998-
999-
fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
982+
fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
1000983
let id_str = format!(" (hir_id={})", id);
1001-
let id_str = if include_id { &id_str[..] } else { "" };
1002984

1003985
let path_str = || {
1004986
// This functionality is used for debugging, try to use `TyCtxt` to get
@@ -1019,6 +1001,9 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
10191001
})
10201002
};
10211003

1004+
let span_str = || map.tcx.sess.source_map().span_to_snippet(map.span(id)).unwrap_or_default();
1005+
let node_str = |prefix| format!("{} {}{}", prefix, span_str(), id_str);
1006+
10221007
match map.find(id) {
10231008
Some(Node::Item(item)) => {
10241009
let item_str = match item.kind {
@@ -1069,22 +1054,20 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
10691054
Some(Node::Field(ref field)) => {
10701055
format!("field {} in {}{}", field.ident, path_str(), id_str)
10711056
}
1072-
Some(Node::AnonConst(_)) => format!("const {}{}", map.hir_to_pretty_string(id), id_str),
1073-
Some(Node::Expr(_)) => format!("expr {}{}", map.hir_to_pretty_string(id), id_str),
1074-
Some(Node::Stmt(_)) => format!("stmt {}{}", map.hir_to_pretty_string(id), id_str),
1075-
Some(Node::PathSegment(_)) => {
1076-
format!("path segment {}{}", map.hir_to_pretty_string(id), id_str)
1077-
}
1078-
Some(Node::Ty(_)) => format!("type {}{}", map.hir_to_pretty_string(id), id_str),
1079-
Some(Node::TraitRef(_)) => format!("trait_ref {}{}", map.hir_to_pretty_string(id), id_str),
1080-
Some(Node::Binding(_)) => format!("local {}{}", map.hir_to_pretty_string(id), id_str),
1081-
Some(Node::Pat(_)) => format!("pat {}{}", map.hir_to_pretty_string(id), id_str),
1082-
Some(Node::Param(_)) => format!("param {}{}", map.hir_to_pretty_string(id), id_str),
1083-
Some(Node::Arm(_)) => format!("arm {}{}", map.hir_to_pretty_string(id), id_str),
1084-
Some(Node::Block(_)) => format!("block {}{}", map.hir_to_pretty_string(id), id_str),
1085-
Some(Node::Local(_)) => format!("local {}{}", map.hir_to_pretty_string(id), id_str),
1057+
Some(Node::AnonConst(_)) => node_str("const"),
1058+
Some(Node::Expr(_)) => node_str("expr"),
1059+
Some(Node::Stmt(_)) => node_str("stmt"),
1060+
Some(Node::PathSegment(_)) => node_str("path segment"),
1061+
Some(Node::Ty(_)) => node_str("type"),
1062+
Some(Node::TraitRef(_)) => node_str("trait ref"),
1063+
Some(Node::Binding(_)) => node_str("local"),
1064+
Some(Node::Pat(_)) => node_str("pat"),
1065+
Some(Node::Param(_)) => node_str("param"),
1066+
Some(Node::Arm(_)) => node_str("arm"),
1067+
Some(Node::Block(_)) => node_str("block"),
1068+
Some(Node::Local(_)) => node_str("local"),
10861069
Some(Node::Ctor(..)) => format!("ctor {}{}", path_str(), id_str),
1087-
Some(Node::Lifetime(_)) => format!("lifetime {}{}", map.hir_to_pretty_string(id), id_str),
1070+
Some(Node::Lifetime(_)) => node_str("lifetime"),
10881071
Some(Node::GenericParam(ref param)) => format!("generic_param {:?}{}", param, id_str),
10891072
Some(Node::Visibility(ref vis)) => format!("visibility {:?}{}", vis, id_str),
10901073
Some(Node::MacroDef(_)) => format!("macro {}{}", path_str(), id_str),

src/librustc_driver/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ rustc_data_structures = { path = "../librustc_data_structures" }
2121
rustc_errors = { path = "../librustc_errors" }
2222
rustc_feature = { path = "../librustc_feature" }
2323
rustc_hir = { path = "../librustc_hir" }
24+
rustc_hir_pretty = { path = "../librustc_hir_pretty" }
2425
rustc_metadata = { path = "../librustc_metadata" }
2526
rustc_mir = { path = "../librustc_mir" }
2627
rustc_parse = { path = "../librustc_parse" }

src/librustc_driver/pretty.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_ast::ast;
77
use rustc_ast_pretty::pprust;
88
use rustc_hir as hir;
99
use rustc_hir::def_id::LOCAL_CRATE;
10-
use rustc_hir::print as pprust_hir;
10+
use rustc_hir_pretty as pprust_hir;
1111
use rustc_mir::util::{write_mir_graphviz, write_mir_pretty};
1212
use rustc_session::config::{Input, PpMode, PpSourceMode};
1313
use rustc_session::Session;
@@ -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/Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ path = "lib.rs"
1010
doctest = false
1111

1212
[dependencies]
13-
rustc_ast_pretty = { path = "../librustc_ast_pretty" }
1413
rustc_target = { path = "../librustc_target" }
1514
rustc_macros = { path = "../librustc_macros" }
1615
rustc_data_structures = { path = "../librustc_data_structures" }
1716
rustc_index = { path = "../librustc_index" }
1817
rustc_span = { path = "../librustc_span" }
19-
rustc_errors = { path = "../librustc_errors" }
2018
rustc_serialize = { path = "../libserialize", package = "serialize" }
2119
rustc_ast = { path = "../librustc_ast" }
2220
lazy_static = "1"

0 commit comments

Comments
 (0)