Skip to content

Commit cfa1d4e

Browse files
committed
Add HIR queries
1 parent 131772c commit cfa1d4e

File tree

7 files changed

+101
-64
lines changed

7 files changed

+101
-64
lines changed

src/librustc/hir/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,36 @@ pub mod map;
77

88
use crate::ty::query::Providers;
99
use crate::ty::TyCtxt;
10+
use rustc_data_structures::fx::FxHashMap;
1011
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1112
use rustc_hir::print;
13+
use rustc_hir::Body;
1214
use rustc_hir::Crate;
1315
use rustc_hir::HirId;
16+
use rustc_hir::ItemLocalId;
17+
use rustc_hir::Node;
18+
use rustc_index::vec::IndexVec;
1419
use std::ops::Deref;
1520

21+
#[derive(HashStable)]
22+
pub struct HirOwner<'tcx> {
23+
parent: HirId,
24+
node: Node<'tcx>,
25+
}
26+
27+
#[derive(HashStable)]
28+
pub struct HirItem<'tcx> {
29+
parent: ItemLocalId,
30+
node: Node<'tcx>,
31+
}
32+
33+
#[derive(HashStable)]
34+
pub struct HirOwnerItems<'tcx> {
35+
owner: &'tcx HirOwner<'tcx>,
36+
items: IndexVec<ItemLocalId, Option<HirItem<'tcx>>>,
37+
bodies: FxHashMap<ItemLocalId, &'tcx Body<'tcx>>,
38+
}
39+
1640
/// A wrapper type which allows you to access HIR.
1741
#[derive(Clone)]
1842
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
@@ -105,6 +105,15 @@ impl<'ctx> rustc_hir::HashStableContext for StableHashingContext<'ctx> {
105105
}
106106
}
107107
}
108+
109+
fn hash_hir_item_like<F: FnOnce(&mut Self)>(&mut self, f: F) {
110+
let prev_hash_node_ids = self.node_id_hashing_mode;
111+
self.node_id_hashing_mode = NodeIdHashingMode::Ignore;
112+
113+
f(self);
114+
115+
self.node_id_hashing_mode = prev_hash_node_ids;
116+
}
108117
}
109118

110119
impl<'a> ToStableHashKey<StableHashingContext<'a>> for DefId {
@@ -158,59 +167,6 @@ impl<'a> ToStableHashKey<StableHashingContext<'a>> for hir::ItemLocalId {
158167
}
159168
}
160169

161-
impl<'a> HashStable<StableHashingContext<'a>> for hir::TraitItem<'_> {
162-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
163-
let hir::TraitItem { hir_id: _, ident, ref attrs, ref generics, ref kind, span } = *self;
164-
165-
hcx.hash_hir_item_like(|hcx| {
166-
ident.name.hash_stable(hcx, hasher);
167-
attrs.hash_stable(hcx, hasher);
168-
generics.hash_stable(hcx, hasher);
169-
kind.hash_stable(hcx, hasher);
170-
span.hash_stable(hcx, hasher);
171-
});
172-
}
173-
}
174-
175-
impl<'a> HashStable<StableHashingContext<'a>> for hir::ImplItem<'_> {
176-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
177-
let hir::ImplItem {
178-
hir_id: _,
179-
ident,
180-
ref vis,
181-
defaultness,
182-
ref attrs,
183-
ref generics,
184-
ref kind,
185-
span,
186-
} = *self;
187-
188-
hcx.hash_hir_item_like(|hcx| {
189-
ident.name.hash_stable(hcx, hasher);
190-
vis.hash_stable(hcx, hasher);
191-
defaultness.hash_stable(hcx, hasher);
192-
attrs.hash_stable(hcx, hasher);
193-
generics.hash_stable(hcx, hasher);
194-
kind.hash_stable(hcx, hasher);
195-
span.hash_stable(hcx, hasher);
196-
});
197-
}
198-
}
199-
200-
impl<'a> HashStable<StableHashingContext<'a>> for hir::Item<'_> {
201-
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
202-
let hir::Item { ident, ref attrs, hir_id: _, ref kind, ref vis, span } = *self;
203-
204-
hcx.hash_hir_item_like(|hcx| {
205-
ident.name.hash_stable(hcx, hasher);
206-
attrs.hash_stable(hcx, hasher);
207-
kind.hash_stable(hcx, hasher);
208-
vis.hash_stable(hcx, hasher);
209-
span.hash_stable(hcx, hasher);
210-
});
211-
}
212-
}
213-
214170
impl<'a> HashStable<StableHashingContext<'a>> for hir::Body<'_> {
215171
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
216172
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, DepConstructor, DepNode, DepNodeParams};
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
@@ -2653,7 +2653,7 @@ pub type TraitMap<ID = HirId> = NodeMap<Vec<TraitCandidate<ID>>>;
26532653
// imported.
26542654
pub type GlobMap = NodeMap<FxHashSet<Name>>;
26552655

2656-
#[derive(Copy, Clone, Debug)]
2656+
#[derive(Copy, Clone, Debug, HashStable_Generic)]
26572657
pub enum Node<'hir> {
26582658
Param(&'hir Param<'hir>),
26592659
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,6 +1,9 @@
11
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
22

3-
use crate::hir::{BodyId, Expr, ImplItemId, ItemId, Mod, TraitItemId, Ty, VisibilityKind};
3+
use crate::hir::{
4+
BodyId, Expr, ImplItem, ImplItemId, Item, ItemId, Mod, TraitItem, TraitItemId, Ty,
5+
VisibilityKind,
6+
};
47
use crate::hir_id::HirId;
58

69
/// Requirements for a `StableHashingContext` to be used in this crate.
@@ -16,6 +19,7 @@ pub trait HashStableContext:
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 {
@@ -78,3 +82,56 @@ impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for VisibilityKind<'_>
7882
hcx.hash_hir_visibility_kind(self, hasher)
7983
}
8084
}
85+
86+
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for TraitItem<'_> {
87+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
88+
let TraitItem { hir_id: _, ident, ref attrs, ref generics, ref kind, span } = *self;
89+
90+
hcx.hash_hir_item_like(|hcx| {
91+
ident.name.hash_stable(hcx, hasher);
92+
attrs.hash_stable(hcx, hasher);
93+
generics.hash_stable(hcx, hasher);
94+
kind.hash_stable(hcx, hasher);
95+
span.hash_stable(hcx, hasher);
96+
});
97+
}
98+
}
99+
100+
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for ImplItem<'_> {
101+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
102+
let ImplItem {
103+
hir_id: _,
104+
ident,
105+
ref vis,
106+
defaultness,
107+
ref attrs,
108+
ref generics,
109+
ref kind,
110+
span,
111+
} = *self;
112+
113+
hcx.hash_hir_item_like(|hcx| {
114+
ident.name.hash_stable(hcx, hasher);
115+
vis.hash_stable(hcx, hasher);
116+
defaultness.hash_stable(hcx, hasher);
117+
attrs.hash_stable(hcx, hasher);
118+
generics.hash_stable(hcx, hasher);
119+
kind.hash_stable(hcx, hasher);
120+
span.hash_stable(hcx, hasher);
121+
});
122+
}
123+
}
124+
125+
impl<HirCtx: crate::HashStableContext> HashStable<HirCtx> for Item<'_> {
126+
fn hash_stable(&self, hcx: &mut HirCtx, hasher: &mut StableHasher) {
127+
let Item { ident, ref attrs, hir_id: _, ref kind, ref vis, span } = *self;
128+
129+
hcx.hash_hir_item_like(|hcx| {
130+
ident.name.hash_stable(hcx, hasher);
131+
attrs.hash_stable(hcx, hasher);
132+
kind.hash_stable(hcx, hasher);
133+
vis.hash_stable(hcx, hasher);
134+
span.hash_stable(hcx, hasher);
135+
});
136+
}
137+
}

0 commit comments

Comments
 (0)