Skip to content

Commit

Permalink
rustc: remove rustc_hir_pretty dependency.
Browse files Browse the repository at this point in the history
  • Loading branch information
Centril committed Mar 24, 2020
1 parent 92885e3 commit f1701dd
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 99 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3114,7 +3114,6 @@ dependencies = [
"rustc_errors",
"rustc_feature",
"rustc_hir",
"rustc_hir_pretty",
"rustc_index",
"rustc_macros",
"rustc_query_system",
Expand Down Expand Up @@ -4086,6 +4085,7 @@ dependencies = [
"rustc_ast_pretty",
"rustc_data_structures",
"rustc_hir",
"rustc_hir_pretty",
"rustc_parse",
"rustc_session",
"rustc_span",
Expand Down
1 change: 0 additions & 1 deletion src/librustc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ rustc_apfloat = { path = "../librustc_apfloat" }
rustc_attr = { path = "../librustc_attr" }
rustc_feature = { path = "../librustc_feature" }
rustc_hir = { path = "../librustc_hir" }
rustc_hir_pretty = { path = "../librustc_hir_pretty" }
rustc_target = { path = "../librustc_target" }
rustc_macros = { path = "../librustc_macros" }
rustc_data_structures = { path = "../librustc_data_structures" }
Expand Down
65 changes: 24 additions & 41 deletions src/librustc/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub use rustc_hir::definitions::{Definitions, DisambiguatedDefPathData};
use rustc_hir::intravisit;
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::*;
use rustc_hir_pretty::Nested;
use rustc_index::vec::IndexVec;
use rustc_span::hygiene::MacroKind;
use rustc_span::source_map::Spanned;
Expand Down Expand Up @@ -954,20 +953,18 @@ impl<'hir> Map<'hir> {
}
}

/// Get a representation of this `id` for debugging purposes.
/// NOTE: Do NOT use this in diagnostics!
pub fn node_to_string(&self, id: HirId) -> String {
hir_id_to_string(self, id, true)
}

pub fn hir_to_user_string(&self, id: HirId) -> String {
hir_id_to_string(self, id, false)
}

pub fn hir_to_pretty_string(&self, id: HirId) -> String {
rustc_hir_pretty::to_string(self, |s| s.print_node(self.get(id)))
hir_id_to_string(self, id)
}
}

impl<'hir> intravisit::Map<'hir> for Map<'hir> {
fn find(&self, hir_id: HirId) -> Option<Node<'hir>> {
self.find(hir_id)
}

fn body(&self, id: BodyId) -> &'hir Body<'hir> {
self.body(id)
}
Expand Down Expand Up @@ -1046,23 +1043,8 @@ pub(super) fn index_hir<'tcx>(tcx: TyCtxt<'tcx>, cnum: CrateNum) -> &'tcx Indexe
tcx.arena.alloc(IndexedHir { crate_hash, map })
}

/// Identical to the `PpAnn` implementation for `hir::Crate`,
/// except it avoids creating a dependency on the whole crate.
impl<'hir> rustc_hir_pretty::PpAnn for Map<'hir> {
fn nested(&self, state: &mut rustc_hir_pretty::State<'_>, nested: rustc_hir_pretty::Nested) {
match nested {
Nested::Item(id) => state.print_item(self.expect_item(id.id)),
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
Nested::Body(id) => state.print_expr(&self.body(id).value),
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
}
}
}

fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
let id_str = format!(" (hir_id={})", id);
let id_str = if include_id { &id_str[..] } else { "" };

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

let span_str = || map.tcx.sess.source_map().span_to_snippet(map.span(id)).unwrap_or_default();
let node_str = |prefix| format!("{} {}{}", prefix, span_str(), id_str);

match map.find(id) {
Some(Node::Item(item)) => {
let item_str = match item.kind {
Expand Down Expand Up @@ -1133,22 +1118,20 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId, include_id: bool) -> String {
Some(Node::Field(ref field)) => {
format!("field {} in {}{}", field.ident, path_str(), id_str)
}
Some(Node::AnonConst(_)) => format!("const {}{}", map.hir_to_pretty_string(id), id_str),
Some(Node::Expr(_)) => format!("expr {}{}", map.hir_to_pretty_string(id), id_str),
Some(Node::Stmt(_)) => format!("stmt {}{}", map.hir_to_pretty_string(id), id_str),
Some(Node::PathSegment(_)) => {
format!("path segment {}{}", map.hir_to_pretty_string(id), id_str)
}
Some(Node::Ty(_)) => format!("type {}{}", map.hir_to_pretty_string(id), id_str),
Some(Node::TraitRef(_)) => format!("trait_ref {}{}", map.hir_to_pretty_string(id), id_str),
Some(Node::Binding(_)) => format!("local {}{}", map.hir_to_pretty_string(id), id_str),
Some(Node::Pat(_)) => format!("pat {}{}", map.hir_to_pretty_string(id), id_str),
Some(Node::Param(_)) => format!("param {}{}", map.hir_to_pretty_string(id), id_str),
Some(Node::Arm(_)) => format!("arm {}{}", map.hir_to_pretty_string(id), id_str),
Some(Node::Block(_)) => format!("block {}{}", map.hir_to_pretty_string(id), id_str),
Some(Node::Local(_)) => format!("local {}{}", map.hir_to_pretty_string(id), id_str),
Some(Node::AnonConst(_)) => node_str("const"),
Some(Node::Expr(_)) => node_str("expr"),
Some(Node::Stmt(_)) => node_str("stmt"),
Some(Node::PathSegment(_)) => node_str("path segment"),
Some(Node::Ty(_)) => node_str("type"),
Some(Node::TraitRef(_)) => node_str("trait ref"),
Some(Node::Binding(_)) => node_str("local"),
Some(Node::Pat(_)) => node_str("pat"),
Some(Node::Param(_)) => node_str("param"),
Some(Node::Arm(_)) => node_str("arm"),
Some(Node::Block(_)) => node_str("block"),
Some(Node::Local(_)) => node_str("local"),
Some(Node::Ctor(..)) => format!("ctor {}{}", path_str(), id_str),
Some(Node::Lifetime(_)) => format!("lifetime {}{}", map.hir_to_pretty_string(id), id_str),
Some(Node::Lifetime(_)) => node_str("lifetime"),
Some(Node::GenericParam(ref param)) => format!("generic_param {:?}{}", param, id_str),
Some(Node::Visibility(ref vis)) => format!("visibility {:?}{}", vis, id_str),
Some(Node::MacroDef(_)) => format!("macro {}{}", path_str(), id_str),
Expand Down
7 changes: 4 additions & 3 deletions src/librustc_driver/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ impl<'hir> pprust::PpAnn for NoAnn<'hir> {}
impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
if let Some(tcx) = self.tcx {
pprust_hir::PpAnn::nested(&tcx.hir(), state, nested)
pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
}
}
}
Expand Down Expand Up @@ -228,7 +228,7 @@ impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
if let Some(ref tcx) = self.tcx {
pprust_hir::PpAnn::nested(&tcx.hir(), state, nested)
pprust_hir::PpAnn::nested(&(&tcx.hir() as &dyn hir::intravisit::Map<'_>), state, nested)
}
}
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
Expand Down Expand Up @@ -334,7 +334,8 @@ impl<'a, 'tcx> pprust_hir::PpAnn for TypedAnnotation<'a, 'tcx> {
if let pprust_hir::Nested::Body(id) = nested {
self.tables.set(self.tcx.body_tables(id));
}
pprust_hir::PpAnn::nested(&self.tcx.hir(), state, nested);
let pp_ann = &(&self.tcx.hir() as &dyn hir::intravisit::Map<'_>);
pprust_hir::PpAnn::nested(pp_ann, state, nested);
self.tables.set(old_tables);
}
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
Expand Down
5 changes: 5 additions & 0 deletions src/librustc_hir/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ impl<'a> FnKind<'a> {

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

impl<'hir> Map<'hir> for ErasedMap<'hir> {
fn find(&self, _: HirId) -> Option<Node<'hir>> {
None
}
fn body(&self, id: BodyId) -> &'hir Body<'hir> {
self.0.body(id)
}
Expand Down
18 changes: 18 additions & 0 deletions src/librustc_hir_pretty/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ use std::borrow::Cow;
use std::cell::Cell;
use std::vec;

pub fn id_to_string(map: &dyn rustc_hir::intravisit::Map<'_>, hir_id: hir::HirId) -> String {
to_string(&map, |s| s.print_node(map.find(hir_id).unwrap()))
}

pub enum AnnNode<'a> {
Name(&'a ast::Name),
Block(&'a hir::Block<'a>),
Expand Down Expand Up @@ -61,6 +65,20 @@ impl PpAnn for hir::Crate<'_> {
}
}

/// Identical to the `PpAnn` implementation for `hir::Crate`,
/// except it avoids creating a dependency on the whole crate.
impl PpAnn for &dyn rustc_hir::intravisit::Map<'_> {
fn nested(&self, state: &mut State<'_>, nested: Nested) {
match nested {
Nested::Item(id) => state.print_item(self.item(id.id)),
Nested::TraitItem(id) => state.print_trait_item(self.trait_item(id)),
Nested::ImplItem(id) => state.print_impl_item(self.impl_item(id)),
Nested::Body(id) => state.print_expr(&self.body(id).value),
Nested::BodyParamPat(id, i) => state.print_pat(&self.body(id).params[i].pat),
}
}
}

pub struct State<'a> {
pub s: pp::Printer,
comments: Option<Comments<'a>>,
Expand Down
13 changes: 9 additions & 4 deletions src/librustc_metadata/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -829,8 +829,10 @@ impl EncodeContext<'tcx> {

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

EntryKind::AssocConst(
Expand Down Expand Up @@ -1047,8 +1049,11 @@ impl EncodeContext<'tcx> {
}

fn encode_rendered_const_for_body(&mut self, body_id: hir::BodyId) -> Lazy<RenderedConst> {
let body = self.tcx.hir().body(body_id);
let rendered = rustc_hir_pretty::to_string(&self.tcx.hir(), |s| s.print_expr(&body.value));
let hir = self.tcx.hir();
let body = hir.body(body_id);
let rendered = rustc_hir_pretty::to_string(&(&hir as &dyn intravisit::Map<'_>), |s| {
s.print_expr(&body.value)
});
let rendered_const = &RenderedConst(rendered);
self.lazy(rendered_const)
}
Expand Down
18 changes: 4 additions & 14 deletions src/librustc_passes/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -903,10 +903,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
}

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

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

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

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

hir::ExprKind::Closure(..) => {
debug!(
"{} is an ExprKind::Closure",
self.ir.tcx.hir().hir_to_pretty_string(expr.hir_id)
);
debug!("{:?} is an ExprKind::Closure", expr);

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

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

Expand Down
5 changes: 3 additions & 2 deletions src/librustc_save_analysis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ path = "lib.rs"
[dependencies]
log = "0.4"
rustc = { path = "../librustc" }
rustc_ast = { path = "../librustc_ast" }
rustc_ast_pretty = { path = "../librustc_ast_pretty" }
rustc_data_structures = { path = "../librustc_data_structures" }
rustc_session = { path = "../librustc_session" }
rustc_hir = { path = "../librustc_hir" }
rustc_hir_pretty = { path = "../librustc_hir_pretty" }
rustc_parse = { path = "../librustc_parse" }
serde_json = "1"
rustc_ast = { path = "../librustc_ast" }
rustc_session = { path = "../librustc_session" }
rustc_span = { path = "../librustc_span" }
rls-data = "0.19"
rls-span = "0.5"
7 changes: 4 additions & 3 deletions src/librustc_save_analysis/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,14 +404,15 @@ impl<'l, 'tcx> SaveContext<'l, 'tcx> {
Some(impl_id) => match self.tcx.hir().get_if_local(impl_id) {
Some(Node::Item(item)) => match item.kind {
hir::ItemKind::Impl { ref self_ty, .. } => {
let hir = self.tcx.hir();

let mut qualname = String::from("<");
qualname.push_str(&self.tcx.hir().hir_to_pretty_string(self_ty.hir_id));
qualname.push_str(&rustc_hir_pretty::id_to_string(&hir, self_ty.hir_id));

let trait_id = self.tcx.trait_id_of_impl(impl_id);
let mut docs = String::new();
let mut attrs = vec![];
let hir_id = self.tcx.hir().node_to_hir_id(id);
if let Some(Node::ImplItem(item)) = self.tcx.hir().find(hir_id) {
if let Some(Node::ImplItem(item)) = hir.find(hir.node_to_hir_id(id)) {
docs = self.docs_for_attrs(&item.attrs);
attrs = item.attrs.to_vec();
}
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_typeck/check/callee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let &ty::Adt(adt_def, ..) = t {
if adt_def.is_enum() {
if let hir::ExprKind::Call(ref expr, _) = call_expr.kind {
unit_variant = Some(self.tcx.hir().hir_to_pretty_string(expr.hir_id))
unit_variant =
self.tcx.sess.source_map().span_to_snippet(expr.span).ok();
}
}
}
Expand Down
38 changes: 15 additions & 23 deletions src/librustc_typeck/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1671,20 +1671,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if let (Some(len), Ok(user_index)) =
(len.try_eval_usize(self.tcx, self.param_env), field.as_str().parse::<u64>())
{
let base = self
.tcx
.sess
.source_map()
.span_to_snippet(base.span)
.unwrap_or_else(|_| self.tcx.hir().hir_to_pretty_string(base.hir_id));
let help = "instead of using tuple indexing, use array indexing";
let suggestion = format!("{}[{}]", base, field);
let applicability = if len < user_index {
Applicability::MachineApplicable
} else {
Applicability::MaybeIncorrect
};
err.span_suggestion(expr.span, help, suggestion, applicability);
if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span) {
let help = "instead of using tuple indexing, use array indexing";
let suggestion = format!("{}[{}]", base, field);
let applicability = if len < user_index {
Applicability::MachineApplicable
} else {
Applicability::MaybeIncorrect
};
err.span_suggestion(expr.span, help, suggestion, applicability);
}
}
}

Expand All @@ -1695,15 +1691,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
base: &hir::Expr<'_>,
field: ast::Ident,
) {
let base = self
.tcx
.sess
.source_map()
.span_to_snippet(base.span)
.unwrap_or_else(|_| self.tcx.hir().hir_to_pretty_string(base.hir_id));
let msg = format!("`{}` is a raw pointer; try dereferencing it", base);
let suggestion = format!("(*{}).{}", base, field);
err.span_suggestion(expr.span, &msg, suggestion, Applicability::MaybeIncorrect);
if let Ok(base) = self.tcx.sess.source_map().span_to_snippet(base.span) {
let msg = format!("`{}` is a raw pointer; try dereferencing it", base);
let suggestion = format!("(*{}).{}", base, field);
err.span_suggestion(expr.span, &msg, suggestion, Applicability::MaybeIncorrect);
}
}

fn no_such_field_err<T: Display>(
Expand Down
Loading

0 comments on commit f1701dd

Please sign in to comment.