Add a Hir wrapper type

This commit is contained in:
John Kåre Alsaker 2020-02-06 11:59:29 +01:00
parent 333c32a5a4
commit 63980cd0fb
9 changed files with 47 additions and 19 deletions

View File

@ -7,6 +7,38 @@ pub mod exports;
pub mod map;
use crate::ty::query::Providers;
use crate::ty::TyCtxt;
use rustc_hir::print;
use std::ops::Deref;
/// A wrapper type which allows you to access HIR.
#[derive(Clone)]
pub struct Hir<'tcx> {
tcx: TyCtxt<'tcx>,
map: &'tcx map::Map<'tcx>,
}
impl<'tcx> Deref for Hir<'tcx> {
type Target = &'tcx map::Map<'tcx>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.map
}
}
impl<'hir> print::PpAnn for Hir<'hir> {
fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) {
self.map.nested(state, nested)
}
}
impl<'tcx> TyCtxt<'tcx> {
#[inline(always)]
pub fn hir(self) -> Hir<'tcx> {
Hir { tcx: self, map: &self.hir_map }
}
}
pub fn provide(providers: &mut Providers<'_>) {
map::provide(providers);

View File

@ -966,7 +966,7 @@ pub struct GlobalCtxt<'tcx> {
/// Export map produced by name resolution.
export_map: FxHashMap<DefId, Vec<Export<hir::HirId>>>,
hir_map: hir_map::Map<'tcx>,
pub(crate) hir_map: hir_map::Map<'tcx>,
/// A map from `DefPathHash` -> `DefId`. Includes `DefId`s from the local crate
/// as well as all upstream crates. Only populated in incremental mode.
@ -1019,11 +1019,6 @@ pub struct GlobalCtxt<'tcx> {
}
impl<'tcx> TyCtxt<'tcx> {
#[inline(always)]
pub fn hir(self) -> &'tcx hir_map::Map<'tcx> {
&self.hir_map
}
pub fn alloc_steal_mir(self, mir: BodyAndCache<'tcx>) -> &'tcx Steal<BodyAndCache<'tcx>> {
self.arena.alloc(Steal::new(mir))
}

View File

@ -143,7 +143,7 @@ impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> {
}
fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'hir>> {
self.tcx.map(|tcx| tcx.hir())
self.tcx.map(|tcx| *tcx.hir())
}
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn {
@ -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(), state, nested)
}
}
}
@ -217,7 +217,7 @@ impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
}
fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'hir>> {
self.tcx.map(|tcx| tcx.hir())
self.tcx.map(|tcx| *tcx.hir())
}
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn {
@ -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(), state, nested)
}
}
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {
@ -334,7 +334,7 @@ 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);
pprust_hir::PpAnn::nested(*self.tcx.hir(), state, nested);
self.tables.set(old_tables);
}
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {

View File

@ -796,7 +796,7 @@ impl EncodeContext<'tcx> {
record!(self.per_def.kind[def_id] <- match trait_item.kind {
ty::AssocKind::Const => {
let rendered =
hir::print::to_string(self.tcx.hir(), |s| s.print_trait_item(ast_item));
hir::print::to_string(&self.tcx.hir(), |s| s.print_trait_item(ast_item));
let rendered_const = self.lazy(RenderedConst(rendered));
EntryKind::AssocConst(
@ -1009,7 +1009,7 @@ 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 = hir::print::to_string(self.tcx.hir(), |s| s.print_expr(&body.value));
let rendered = hir::print::to_string(&self.tcx.hir(), |s| s.print_expr(&body.value));
let rendered_const = &RenderedConst(rendered);
self.lazy(rendered_const)
}

View File

@ -8,6 +8,7 @@
//! through, but errors for structured control flow in a `const` should be emitted here.
use rustc::hir::map::Map;
use rustc::hir::Hir;
use rustc::session::config::nightly_options;
use rustc::session::parse::feature_err;
use rustc::ty::query::Providers;
@ -74,7 +75,7 @@ enum ConstKind {
}
impl ConstKind {
fn for_body(body: &hir::Body<'_>, hir_map: &Map<'_>) -> Option<Self> {
fn for_body(body: &hir::Body<'_>, hir_map: Hir<'_>) -> Option<Self> {
let is_const_fn = |id| hir_map.fn_sig_by_hir_id(id).unwrap().header.is_const();
let owner = hir_map.body_owner(body.id());

View File

@ -1,4 +1,4 @@
use rustc::hir::map as hir_map;
use rustc::hir::Hir;
use rustc::session::config::EntryFnType;
use rustc::session::{config, Session};
use rustc::ty::query::Providers;
@ -15,7 +15,7 @@ use syntax::entry::EntryPointType;
struct EntryContext<'a, 'tcx> {
session: &'a Session,
map: &'a hir_map::Map<'tcx>,
map: Hir<'tcx>,
/// The top-level function called `main`.
main_fn: Option<(HirId, Span)>,

View File

@ -2588,7 +2588,7 @@ fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span, qpath: &
E0533,
"expected unit struct, unit variant or constant, found {} `{}`",
res.descr(),
hir::print::to_string(tcx.hir(), |s| s.print_qpath(qpath, false))
hir::print::to_string(&tcx.hir(), |s| s.print_qpath(qpath, false))
)
.emit();
}

View File

@ -693,7 +693,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let msg = format!(
"expected tuple struct or tuple variant, found {} `{}`",
res.descr(),
hir::print::to_string(tcx.hir(), |s| s.print_qpath(qpath, false)),
hir::print::to_string(&tcx.hir(), |s| s.print_qpath(qpath, false)),
);
let mut err = struct_span_err!(tcx.sess, pat.span, E0164, "{}", msg);
match (res, &pat.kind) {

View File

@ -107,7 +107,7 @@ pub fn run(options: Options) -> i32 {
let mut hir_collector = HirCollector {
sess: compiler.session(),
collector: &mut collector,
map: tcx.hir(),
map: *tcx.hir(),
codes: ErrorCodes::from(
compiler.session().opts.unstable_features.is_nightly_build(),
),