diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 6275c0aabe8..4a4d9cb8145 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -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); diff --git a/src/librustc/ty/context.rs b/src/librustc/ty/context.rs index f12032943f9..5a0cb45d0bb 100644 --- a/src/librustc/ty/context.rs +++ b/src/librustc/ty/context.rs @@ -966,7 +966,7 @@ pub struct GlobalCtxt<'tcx> { /// Export map produced by name resolution. export_map: FxHashMap>>, - 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> { self.arena.alloc(Steal::new(mir)) } diff --git a/src/librustc_driver/pretty.rs b/src/librustc_driver/pretty.rs index 345b03e6db2..4606ef81a36 100644 --- a/src/librustc_driver/pretty.rs +++ b/src/librustc_driver/pretty.rs @@ -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<'_>) { diff --git a/src/librustc_metadata/rmeta/encoder.rs b/src/librustc_metadata/rmeta/encoder.rs index 54fbdb14010..4133047af78 100644 --- a/src/librustc_metadata/rmeta/encoder.rs +++ b/src/librustc_metadata/rmeta/encoder.rs @@ -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 { 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) } diff --git a/src/librustc_passes/check_const.rs b/src/librustc_passes/check_const.rs index faa85f68fab..b178110f4f9 100644 --- a/src/librustc_passes/check_const.rs +++ b/src/librustc_passes/check_const.rs @@ -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 { + fn for_body(body: &hir::Body<'_>, hir_map: Hir<'_>) -> Option { 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()); diff --git a/src/librustc_passes/entry.rs b/src/librustc_passes/entry.rs index d36114fd3b5..ebd93e9ab85 100644 --- a/src/librustc_passes/entry.rs +++ b/src/librustc_passes/entry.rs @@ -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)>, diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index d0275429747..62bc6724d0c 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -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(); } diff --git a/src/librustc_typeck/check/pat.rs b/src/librustc_typeck/check/pat.rs index f9dee0e477f..47baae68608 100644 --- a/src/librustc_typeck/check/pat.rs +++ b/src/librustc_typeck/check/pat.rs @@ -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) { diff --git a/src/librustdoc/test.rs b/src/librustdoc/test.rs index 2892c4b1537..ca173fdeb66 100644 --- a/src/librustdoc/test.rs +++ b/src/librustdoc/test.rs @@ -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(), ),