rustc: remove rustc_hir_pretty dependency.

This commit is contained in:
Mazdak Farrokhzad 2020-03-24 02:44:41 +01:00
parent 92885e3a5b
commit f1701ddef1
17 changed files with 101 additions and 99 deletions

View File

@ -3114,7 +3114,6 @@ dependencies = [
"rustc_errors",
"rustc_feature",
"rustc_hir",
"rustc_hir_pretty",
"rustc_index",
"rustc_macros",
"rustc_query_system",
@ -4086,6 +4085,7 @@ dependencies = [
"rustc_ast_pretty",
"rustc_data_structures",
"rustc_hir",
"rustc_hir_pretty",
"rustc_parse",
"rustc_session",
"rustc_span",

View File

@ -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" }

View File

@ -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;
@ -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)
}
@ -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
@ -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 {
@ -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),

View File

@ -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)
}
}
}
@ -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<'_>) {
@ -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<'_>) {

View File

@ -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>;
@ -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)
}

View File

@ -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>),
@ -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>>,

View File

@ -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(
@ -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)
}

View File

@ -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:
@ -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
@ -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.
@ -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);

View File

@ -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"

View File

@ -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();
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}
}
@ -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>(

View File

@ -2139,13 +2139,18 @@ fn compute_sig_of_foreign_fn_decl<'tcx>(
{
let check = |ast_ty: &hir::Ty<'_>, ty: Ty<'_>| {
if ty.is_simd() {
let snip = tcx
.sess
.source_map()
.span_to_snippet(ast_ty.span)
.map_or(String::new(), |s| format!(" `{}`", s));
tcx.sess
.struct_span_err(
ast_ty.span,
&format!(
"use of SIMD type `{}` in FFI is highly experimental and \
"use of SIMD type{} in FFI is highly experimental and \
may result in invalid code",
tcx.hir().hir_to_pretty_string(ast_ty.hir_id)
snip
),
)
.help("add `#![feature(simd_ffi)]` to the crate attributes to enable")

View File

@ -482,8 +482,8 @@ fn build_module(cx: &DocContext<'_>, did: DefId, visited: &mut FxHashSet<DefId>)
}
pub fn print_inlined_const(cx: &DocContext<'_>, did: DefId) -> String {
if let Some(node_id) = cx.tcx.hir().as_local_hir_id(did) {
cx.tcx.hir().hir_to_pretty_string(node_id)
if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(did) {
rustc_hir_pretty::id_to_string(&cx.tcx.hir(), hir_id)
} else {
cx.tcx.rendered_const(did)
}

View File

@ -578,7 +578,7 @@ pub fn print_const_expr(cx: &DocContext<'_>, body: hir::BodyId) -> String {
None
};
snippet.unwrap_or_else(|| cx.tcx.hir().hir_to_pretty_string(body.hir_id))
snippet.unwrap_or_else(|| rustc_hir_pretty::id_to_string(&cx.tcx.hir(), body.hir_id))
}
/// Given a type Path, resolve it to a Type using the TyCtxt

View File

@ -26,6 +26,7 @@ extern crate rustc_errors;
extern crate rustc_expand;
extern crate rustc_feature;
extern crate rustc_hir;
extern crate rustc_hir_pretty;
extern crate rustc_index;
extern crate rustc_infer;
extern crate rustc_interface;

View File

@ -910,7 +910,7 @@ impl<'a, 'hir> intravisit::Visitor<'hir> for HirCollector<'a, 'hir> {
fn visit_item(&mut self, item: &'hir hir::Item) {
let name = if let hir::ItemKind::Impl { ref self_ty, .. } = item.kind {
self.map.hir_to_pretty_string(self_ty.hir_id)
rustc_hir_pretty::id_to_string(&self.map, self_ty.hir_id)
} else {
item.ident.to_string()
};