Skip to content

Commit 47e574e

Browse files
committed
Add HIR queries
1 parent 8498c5f commit 47e574e

File tree

7 files changed

+102
-64
lines changed

7 files changed

+102
-64
lines changed

src/librustc/hir/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,36 @@ pub mod map;
88

99
use crate::ty::query::Providers;
1010
use crate::ty::TyCtxt;
11+
use rustc_data_structures::fx::FxHashMap;
1112
use rustc_hir::def_id::LOCAL_CRATE;
1213
use rustc_hir::print;
14+
use rustc_hir::Body;
1315
use rustc_hir::Crate;
16+
use rustc_hir::HirId;
17+
use rustc_hir::ItemLocalId;
18+
use rustc_hir::Node;
19+
use rustc_index::vec::IndexVec;
1420
use std::ops::Deref;
1521

22+
#[derive(HashStable)]
23+
pub struct HirOwner<'tcx> {
24+
parent: HirId,
25+
node: Node<'tcx>,
26+
}
27+
28+
#[derive(HashStable)]
29+
pub struct HirItem<'tcx> {
30+
parent: ItemLocalId,
31+
node: Node<'tcx>,
32+
}
33+
34+
#[derive(HashStable)]
35+
pub struct HirOwnerItems<'tcx> {
36+
owner: &'tcx HirOwner<'tcx>,
37+
items: IndexVec<ItemLocalId, Option<HirItem<'tcx>>>,
38+
bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
39+
}
40+
1641
/// A wrapper type which allows you to access HIR.
1742
#[derive(Clone)]
1843
pub struct Hir<'tcx> {

src/librustc/ich/hcx.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -164,15 +164,6 @@ impl<'a> StableHashingContext<'a> {
164164
}
165165
IGNORED_ATTRIBUTES.with(|attrs| attrs.contains(&name))
166166
}
167-
168-
pub fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F) {
169-
let prev_hash_node_ids = self.node_id_hashing_mode;
170-
self.node_id_hashing_mode = NodeIdHashingMode::Ignore;
171-
172-
f(self);
173-
174-
self.node_id_hashing_mode = prev_hash_node_ids;
175-
}
176167
}
177168

178169
/// Something that can provide a stable hashing context.

src/librustc/ich/impls_hir.rs

Lines changed: 9 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
111111
}
112112
}
113113
}
114+
115+
fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F) {
116+
let prev_hash_node_ids = self.node_id_hashing_mode;
117+
self.node_id_hashing_mode = NodeIdHashingMode::Ignore;
118+
119+
f(self);
120+
121+
self.node_id_hashing_mode = prev_hash_node_ids;
122+
}
114123
}
115124

116125
impl<'a> ToStableHashKey<StableHashingContext<'a>> for DefId {
@@ -164,59 +173,6 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::ItemLocalId {
164173
}
165174
}
166175

167-
impl<'a> HashStable<StableHashingContext<'a>> for hir::TraitItem<'_> {
168-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
169-
let hir::TraitItem { hir_id: _, ident, ref attrs, ref generics, ref kind, span } = *self;
170-
171-
hcx.hash_hir_item_like(|hcx| {
172-
ident.name.hash_stable(hcx, hasher);
173-
attrs.hash_stable(hcx, hasher);
174-
generics.hash_stable(hcx, hasher);
175-
kind.hash_stable(hcx, hasher);
176-
span.hash_stable(hcx, hasher);
177-
});
178-
}
179-
}
180-
181-
impl<'a> HashStable<StableHashingContext<'a>> for hir::ImplItem<'_> {
182-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
183-
let hir::ImplItem {
184-
hir_id: _,
185-
ident,
186-
ref vis,
187-
defaultness,
188-
ref attrs,
189-
ref generics,
190-
ref kind,
191-
span,
192-
} = *self;
193-
194-
hcx.hash_hir_item_like(|hcx| {
195-
ident.name.hash_stable(hcx, hasher);
196-
vis.hash_stable(hcx, hasher);
197-
defaultness.hash_stable(hcx, hasher);
198-
attrs.hash_stable(hcx, hasher);
199-
generics.hash_stable(hcx, hasher);
200-
kind.hash_stable(hcx, hasher);
201-
span.hash_stable(hcx, hasher);
202-
});
203-
}
204-
}
205-
206-
impl<'a> HashStable<StableHashingContext<'a>> for hir::Item<'_> {
207-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
208-
let hir::Item { ident, ref attrs, hir_id: _, ref kind, ref vis, span } = *self;
209-
210-
hcx.hash_hir_item_like(|hcx| {
211-
ident.name.hash_stable(hcx, hasher);
212-
attrs.hash_stable(hcx, hasher);
213-
kind.hash_stable(hcx, hasher);
214-
vis.hash_stable(hcx, hasher);
215-
span.hash_stable(hcx, hasher);
216-
});
217-
}
218-
}
219-
220176
impl<'a> HashStable<StableHashingContext<'a>> for hir::Body<'_> {
221177
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
222178
let hir::Body { params, value, generator_kind } = self;

src/librustc/query/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ rustc_queries! {
5555
desc { "get the crate HIR" }
5656
}
5757

58+
query hir_owner(key: DefId) -> &'tcx HirOwner<'tcx> {
59+
eval_always
60+
}
61+
62+
query hir_owner_items(key: DefId) -> &'tcx HirOwnerItems<'tcx> {
63+
eval_always
64+
}
65+
5866
/// Records the type of every item.
5967
query type_of(key: DefId) -> Ty<'tcx> {
6068
cache_on_disk_if { key.is_local() }

src/librustc/ty/query/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::dep_graph::{self, DepNode};
22
use crate::hir::exports::Export;
3+
use crate::hir::{HirOwner, HirOwnerItems};
34
use crate::infer::canonical::{self, Canonical};
45
use crate::lint::LintLevelMap;
56
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;

src/librustc_hir/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2588,7 +2588,7 @@ pub type TraitMap = NodeMap<Vec<TraitCandidate>>;
25882588
// imported.
25892589
pub type GlobMap = NodeMap<FxHashSet<Name>>;
25902590

2591-
#[derive(Copy, Clone, Debug)]
2591+
#[derive(Copy, Clone, Debug, HashStable_Generic)]
25922592
pub enum Node<'hir> {
25932593
Param(&'hir Param<'hir>),
25942594
Item(&'hir Item<'hir>),

src/librustc_hir/stable_hash_impls.rs

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
22

33
use crate::def_id::DefId;
4-
use crate::hir::{BodyId, Expr, ImplItemId, ItemId, Mod, TraitItemId, Ty, VisibilityKind};
4+
use crate::hir::{
5+
BodyId, Expr, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem, TraitItemId, Ty,
6+
VisibilityKind,
7+
};
58
use crate::hir_id::HirId;
69

710
/// Requirements for a `StableHashingContext` to be used in this crate.
@@ -16,6 +19,7 @@ pub trait HashStableContext: syntax::HashStableContext + rustc_target::HashStabl
1619
fn hash_hir_expr(&mut self, _: &Expr<'_>, hasher: &mut StableHasher);
1720
fn hash_hir_ty(&mut self, _: &Ty<'_>, hasher: &mut StableHasher);
1821
fn hash_hir_visibility_kind(&mut self, _: &VisibilityKind<'_>, hasher: &mut StableHasher);
22+
fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F);
1923
}
2024

2125
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for HirId {
@@ -84,3 +88,56 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for VisibilityKind<'_>
8488
hcx.hash_hir_visibility_kind(self, hasher)
8589
}
8690
}
91+
92+
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
93+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
94+
let TraitItem { hir_id: _, ident, ref attrs, ref generics, ref kind, span } = *self;
95+
96+
hcx.hash_hir_item_like(|hcx| {
97+
ident.name.hash_stable(hcx, hasher);
98+
attrs.hash_stable(hcx, hasher);
99+
generics.hash_stable(hcx, hasher);
100+
kind.hash_stable(hcx, hasher);
101+
span.hash_stable(hcx, hasher);
102+
});
103+
}
104+
}
105+
106+
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
107+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
108+
let ImplItem {
109+
hir_id: _,
110+
ident,
111+
ref vis,
112+
defaultness,
113+
ref attrs,
114+
ref generics,
115+
ref kind,
116+
span,
117+
} = *self;
118+
119+
hcx.hash_hir_item_like(|hcx| {
120+
ident.name.hash_stable(hcx, hasher);
121+
vis.hash_stable(hcx, hasher);
122+
defaultness.hash_stable(hcx, hasher);
123+
attrs.hash_stable(hcx, hasher);
124+
generics.hash_stable(hcx, hasher);
125+
kind.hash_stable(hcx, hasher);
126+
span.hash_stable(hcx, hasher);
127+
});
128+
}
129+
}
130+
131+
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
132+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
133+
let Item { ident, ref attrs, hir_id: _, ref kind, ref vis, span } = *self;
134+
135+
hcx.hash_hir_item_like(|hcx| {
136+
ident.name.hash_stable(hcx, hasher);
137+
attrs.hash_stable(hcx, hasher);
138+
kind.hash_stable(hcx, hasher);
139+
vis.hash_stable(hcx, hasher);
140+
span.hash_stable(hcx, hasher);
141+
});
142+
}
143+
}

0 commit comments

Comments
 (0)