]> git.lizzy.rs Git - rust.git/commitdiff
Add a Hir wrapper type
authorJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Thu, 6 Feb 2020 10:59:29 +0000 (11:59 +0100)
committerJohn Kåre Alsaker <john.kare.alsaker@gmail.com>
Thu, 6 Feb 2020 11:05:40 +0000 (12:05 +0100)
src/librustc/hir/mod.rs
src/librustc/ty/context.rs
src/librustc_driver/pretty.rs
src/librustc_metadata/rmeta/encoder.rs
src/librustc_passes/check_const.rs
src/librustc_passes/entry.rs
src/librustc_typeck/check/mod.rs
src/librustc_typeck/check/pat.rs
src/librustdoc/test.rs

index 6275c0aabe896829883c90d467a78a4fad10f717..4a4d9cb81456d266de2d1235ffbb85b51c30545c 100644 (file)
@@ -7,6 +7,38 @@
 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);
index f12032943f91f11389f093dea8fb5bb64462375b..5a0cb45d0bbe842bddc64d03d998218b934f3345 100644 (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))
     }
index 345b03e6db243dacde0a32e45ff1f1adc59ea16d..4606ef81a360e3012b652c8bdca5ba52475355f3 100644 (file)
@@ -143,7 +143,7 @@ fn sess(&self) -> &Session {
     }
 
     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 @@ fn sess(&self) -> &Session {
     }
 
     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 @@ 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<'_>) {
@@ -334,7 +334,7 @@ fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested)
         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<'_>) {
index 54fbdb14010c94d246df13d71b9cc8e55162ecf6..4133047af78fed193724f18fffb5a40667a33b96 100644 (file)
@@ -796,7 +796,7 @@ fn encode_info_for_trait_item(&mut self, def_id: DefId) {
         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 @@ fn encode_deprecation(&mut self, def_id: DefId) {
 
     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)
     }
index faa85f68fab860f0dbe9809fd4d2e69a28613398..b178110f4f954104881f2a5fcd0b22e884aa618a 100644 (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());
index d36114fd3b5b5f04ba0fff88eea5bef33a000a45..ebd93e9ab85b813db0ca9c3249ef68f9ab261a79 100644 (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 @@
 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)>,
index d0275429747b60f00b10fc1c1eadfaee47a41bd0..62bc6724d0cfee2df75e99e4dabc8adc55aafa8c 100644 (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();
 }
index f9dee0e477f793f055fa5c5cf6b649826ba362e1..47baae68608963eea32a097bf3522cee8a0a5968 100644 (file)
@@ -693,7 +693,7 @@ fn check_pat_tuple_struct(
             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) {
index 2892c4b15379032b612306f94297461ddfffad87..ca173fdeb66d45ee6e1bd4ccd1c0124e9e0f6fbc 100644 (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(),
                     ),