Skip to content

Commit 63980cd

Browse files
committed
Add a Hir wrapper type
1 parent 333c32a commit 63980cd

File tree

9 files changed

+47
-19
lines changed

9 files changed

+47
-19
lines changed

src/librustc/hir/mod.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,38 @@ pub mod exports;
77
pub mod map;
88

99
use crate::ty::query::Providers;
10+
use crate::ty::TyCtxt;
11+
use rustc_hir::print;
12+
use std::ops::Deref;
13+
14+
/// A wrapper type which allows you to access HIR.
15+
#[derive(Clone)]
16+
pub struct Hir<'tcx> {
17+
tcx: TyCtxt<'tcx>,
18+
map: &'tcx map::Map<'tcx>,
19+
}
20+
21+
impl<'tcx> Deref for Hir<'tcx> {
22+
type Target = &'tcx map::Map<'tcx>;
23+
24+
#[inline(always)]
25+
fn deref(&self) -> &Self::Target {
26+
&self.map
27+
}
28+
}
29+
30+
impl<'hir> print::PpAnn for Hir<'hir> {
31+
fn nested(&self, state: &mut print::State<'_>, nested: print::Nested) {
32+
self.map.nested(state, nested)
33+
}
34+
}
35+
36+
impl<'tcx> TyCtxt<'tcx> {
37+
#[inline(always)]
38+
pub fn hir(self) -> Hir<'tcx> {
39+
Hir { tcx: self, map: &self.hir_map }
40+
}
41+
}
1042

1143
pub fn provide(providers: &mut Providers<'_>) {
1244
map::provide(providers);

src/librustc/ty/context.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -966,7 +966,7 @@ pub struct GlobalCtxt<'tcx> {
966966
/// Export map produced by name resolution.
967967
export_map: FxHashMap<DefId, Vec<Export<hir::HirId>>>,
968968

969-
hir_map: hir_map::Map<'tcx>,
969+
pub(crate) hir_map: hir_map::Map<'tcx>,
970970

971971
/// A map from `DefPathHash` -> `DefId`. Includes `DefId`s from the local crate
972972
/// as well as all upstream crates. Only populated in incremental mode.
@@ -1019,11 +1019,6 @@ pub struct GlobalCtxt<'tcx> {
10191019
}
10201020

10211021
impl<'tcx> TyCtxt<'tcx> {
1022-
#[inline(always)]
1023-
pub fn hir(self) -> &'tcx hir_map::Map<'tcx> {
1024-
&self.hir_map
1025-
}
1026-
10271022
pub fn alloc_steal_mir(self, mir: BodyAndCache<'tcx>) -> &'tcx Steal<BodyAndCache<'tcx>> {
10281023
self.arena.alloc(Steal::new(mir))
10291024
}

src/librustc_driver/pretty.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<'hir> HirPrinterSupport<'hir> for NoAnn<'hir> {
143143
}
144144

145145
fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'hir>> {
146-
self.tcx.map(|tcx| tcx.hir())
146+
self.tcx.map(|tcx| *tcx.hir())
147147
}
148148

149149
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn {
@@ -155,7 +155,7 @@ impl<'hir> pprust::PpAnn for NoAnn<'hir> {}
155155
impl<'hir> pprust_hir::PpAnn for NoAnn<'hir> {
156156
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
157157
if let Some(tcx) = self.tcx {
158-
pprust_hir::PpAnn::nested(tcx.hir(), state, nested)
158+
pprust_hir::PpAnn::nested(*tcx.hir(), state, nested)
159159
}
160160
}
161161
}
@@ -217,7 +217,7 @@ impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
217217
}
218218

219219
fn hir_map<'a>(&'a self) -> Option<&'a hir_map::Map<'hir>> {
220-
self.tcx.map(|tcx| tcx.hir())
220+
self.tcx.map(|tcx| *tcx.hir())
221221
}
222222

223223
fn pp_ann<'a>(&'a self) -> &'a dyn pprust_hir::PpAnn {
@@ -228,7 +228,7 @@ impl<'hir> HirPrinterSupport<'hir> for IdentifiedAnnotation<'hir> {
228228
impl<'hir> pprust_hir::PpAnn for IdentifiedAnnotation<'hir> {
229229
fn nested(&self, state: &mut pprust_hir::State<'_>, nested: pprust_hir::Nested) {
230230
if let Some(ref tcx) = self.tcx {
231-
pprust_hir::PpAnn::nested(tcx.hir(), state, nested)
231+
pprust_hir::PpAnn::nested(*tcx.hir(), state, nested)
232232
}
233233
}
234234
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> {
334334
if let pprust_hir::Nested::Body(id) = nested {
335335
self.tables.set(self.tcx.body_tables(id));
336336
}
337-
pprust_hir::PpAnn::nested(self.tcx.hir(), state, nested);
337+
pprust_hir::PpAnn::nested(*self.tcx.hir(), state, nested);
338338
self.tables.set(old_tables);
339339
}
340340
fn pre(&self, s: &mut pprust_hir::State<'_>, node: pprust_hir::AnnNode<'_>) {

src/librustc_metadata/rmeta/encoder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,7 @@ impl EncodeContext<'tcx> {
796796
record!(self.per_def.kind[def_id] <- match trait_item.kind {
797797
ty::AssocKind::Const => {
798798
let rendered =
799-
hir::print::to_string(self.tcx.hir(), |s| s.print_trait_item(ast_item));
799+
hir::print::to_string(&self.tcx.hir(), |s| s.print_trait_item(ast_item));
800800
let rendered_const = self.lazy(RenderedConst(rendered));
801801

802802
EntryKind::AssocConst(
@@ -1009,7 +1009,7 @@ impl EncodeContext<'tcx> {
10091009

10101010
fn encode_rendered_const_for_body(&mut self, body_id: hir::BodyId) -> Lazy<RenderedConst> {
10111011
let body = self.tcx.hir().body(body_id);
1012-
let rendered = hir::print::to_string(self.tcx.hir(), |s| s.print_expr(&body.value));
1012+
let rendered = hir::print::to_string(&self.tcx.hir(), |s| s.print_expr(&body.value));
10131013
let rendered_const = &RenderedConst(rendered);
10141014
self.lazy(rendered_const)
10151015
}

src/librustc_passes/check_const.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//! through, but errors for structured control flow in a `const` should be emitted here.
99
1010
use rustc::hir::map::Map;
11+
use rustc::hir::Hir;
1112
use rustc::session::config::nightly_options;
1213
use rustc::session::parse::feature_err;
1314
use rustc::ty::query::Providers;
@@ -74,7 +75,7 @@ enum ConstKind {
7475
}
7576

7677
impl ConstKind {
77-
fn for_body(body: &hir::Body<'_>, hir_map: &Map<'_>) -> Option<Self> {
78+
fn for_body(body: &hir::Body<'_>, hir_map: Hir<'_>) -> Option<Self> {
7879
let is_const_fn = |id| hir_map.fn_sig_by_hir_id(id).unwrap().header.is_const();
7980

8081
let owner = hir_map.body_owner(body.id());

src/librustc_passes/entry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc::hir::map as hir_map;
1+
use rustc::hir::Hir;
22
use rustc::session::config::EntryFnType;
33
use rustc::session::{config, Session};
44
use rustc::ty::query::Providers;
@@ -15,7 +15,7 @@ use syntax::entry::EntryPointType;
1515
struct EntryContext<'a, 'tcx> {
1616
session: &'a Session,
1717

18-
map: &'a hir_map::Map<'tcx>,
18+
map: Hir<'tcx>,
1919

2020
/// The top-level function called `main`.
2121
main_fn: Option<(HirId, Span)>,

src/librustc_typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2588,7 +2588,7 @@ fn report_unexpected_variant_res(tcx: TyCtxt<'_>, res: Res, span: Span, qpath: &
25882588
E0533,
25892589
"expected unit struct, unit variant or constant, found {} `{}`",
25902590
res.descr(),
2591-
hir::print::to_string(tcx.hir(), |s| s.print_qpath(qpath, false))
2591+
hir::print::to_string(&tcx.hir(), |s| s.print_qpath(qpath, false))
25922592
)
25932593
.emit();
25942594
}

src/librustc_typeck/check/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
693693
let msg = format!(
694694
"expected tuple struct or tuple variant, found {} `{}`",
695695
res.descr(),
696-
hir::print::to_string(tcx.hir(), |s| s.print_qpath(qpath, false)),
696+
hir::print::to_string(&tcx.hir(), |s| s.print_qpath(qpath, false)),
697697
);
698698
let mut err = struct_span_err!(tcx.sess, pat.span, E0164, "{}", msg);
699699
match (res, &pat.kind) {

src/librustdoc/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ pub fn run(options: Options) -> i32 {
107107
let mut hir_collector = HirCollector {
108108
sess: compiler.session(),
109109
collector: &mut collector,
110-
map: tcx.hir(),
110+
map: *tcx.hir(),
111111
codes: ErrorCodes::from(
112112
compiler.session().opts.unstable_features.is_nightly_build(),
113113
),

0 commit comments

Comments
 (0)