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);
/// 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.
}
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))
}
}
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 {
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)
}
}
}
}
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 {
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<'_>) {
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<'_>) {
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(
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)
}
//! 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;
}
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());
-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;
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)>,
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();
}
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) {
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(),
),