2020-02-07 04:45:04 -06:00
|
|
|
use crate::arena::Arena;
|
2020-02-10 10:00:49 -06:00
|
|
|
use crate::hir::map::{Entry, HirOwnerData, Map};
|
2020-03-17 20:48:17 -05:00
|
|
|
use crate::hir::{Owner, OwnerNodes, ParentedNode};
|
2020-01-07 07:38:27 -06:00
|
|
|
use crate::ich::StableHashingContext;
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::middle::cstore::CrateStore;
|
2020-01-07 07:38:27 -06:00
|
|
|
use rustc_data_structures::fingerprint::Fingerprint;
|
2019-12-23 22:02:53 -06:00
|
|
|
use rustc_data_structures::fx::FxHashMap;
|
2020-01-07 07:38:27 -06:00
|
|
|
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
2019-12-22 16:42:04 -06:00
|
|
|
use rustc_data_structures::svh::Svh;
|
2020-01-04 19:37:57 -06:00
|
|
|
use rustc_hir as hir;
|
2020-01-07 07:38:27 -06:00
|
|
|
use rustc_hir::def_id::CRATE_DEF_INDEX;
|
2020-03-18 13:27:59 -05:00
|
|
|
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
|
2020-03-24 03:09:42 -05:00
|
|
|
use rustc_hir::definitions::{self, DefPathHash};
|
2020-01-07 11:12:06 -06:00
|
|
|
use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
|
2020-01-07 07:38:27 -06:00
|
|
|
use rustc_hir::*;
|
2020-02-10 10:00:49 -06:00
|
|
|
use rustc_index::vec::{Idx, IndexVec};
|
2019-12-24 16:43:44 -06:00
|
|
|
use rustc_session::{CrateDisambiguator, Session};
|
2020-01-01 12:25:28 -06:00
|
|
|
use rustc_span::source_map::SourceMap;
|
2020-01-07 07:38:27 -06:00
|
|
|
use rustc_span::{Span, Symbol, DUMMY_SP};
|
2015-09-10 14:53:08 -05:00
|
|
|
|
2020-01-07 07:38:27 -06:00
|
|
|
use std::iter::repeat;
|
2017-09-14 10:43:03 -05:00
|
|
|
|
2019-02-28 16:43:53 -06:00
|
|
|
/// A visitor that walks over the HIR and collects `Node`s into a HIR map.
|
2017-08-18 13:24:19 -05:00
|
|
|
pub(super) struct NodeCollector<'a, 'hir> {
|
2020-02-07 04:45:04 -06:00
|
|
|
arena: &'hir Arena<'hir>,
|
|
|
|
|
2015-12-21 15:24:15 -06:00
|
|
|
/// The crate
|
2019-11-28 04:49:29 -06:00
|
|
|
krate: &'hir Crate<'hir>,
|
2018-11-21 12:35:14 -06:00
|
|
|
|
|
|
|
/// Source map
|
|
|
|
source_map: &'a SourceMap,
|
|
|
|
|
2020-03-18 13:27:59 -05:00
|
|
|
map: IndexVec<LocalDefId, HirOwnerData<'hir>>,
|
2020-02-07 04:45:04 -06:00
|
|
|
|
2015-12-21 15:24:15 -06:00
|
|
|
/// The parent of this node
|
2019-02-15 08:21:56 -06:00
|
|
|
parent_node: hir::HirId,
|
2019-02-03 02:14:31 -06:00
|
|
|
|
2020-03-18 13:27:59 -05:00
|
|
|
current_dep_node_owner: LocalDefId,
|
2017-08-18 13:24:19 -05:00
|
|
|
|
|
|
|
definitions: &'a definitions::Definitions,
|
2017-09-14 10:43:03 -05:00
|
|
|
|
|
|
|
hcx: StableHashingContext<'a>,
|
|
|
|
|
2020-02-07 22:30:45 -06:00
|
|
|
// We are collecting HIR hashes here so we can compute the
|
2020-02-07 22:18:34 -06:00
|
|
|
// crate hash from them later on.
|
2018-12-22 05:40:23 -06:00
|
|
|
hir_body_nodes: Vec<(DefPathHash, Fingerprint)>,
|
|
|
|
}
|
|
|
|
|
2020-02-10 10:00:49 -06:00
|
|
|
fn insert_vec_map<K: Idx, V: Clone>(map: &mut IndexVec<K, Option<V>>, k: K, v: V) {
|
|
|
|
let i = k.index();
|
|
|
|
let len = map.len();
|
|
|
|
if i >= len {
|
|
|
|
map.extend(repeat(None).take(i - len + 1));
|
|
|
|
}
|
|
|
|
map[k] = Some(v);
|
|
|
|
}
|
|
|
|
|
2020-02-07 22:18:34 -06:00
|
|
|
fn hash(
|
2019-01-19 22:44:02 -06:00
|
|
|
hcx: &mut StableHashingContext<'_>,
|
2020-01-07 07:22:44 -06:00
|
|
|
input: impl for<'a> HashStable<StableHashingContext<'a>>,
|
2020-02-07 22:18:34 -06:00
|
|
|
) -> Fingerprint {
|
|
|
|
let mut stable_hasher = StableHasher::new();
|
|
|
|
input.hash_stable(hcx, &mut stable_hasher);
|
|
|
|
stable_hasher.finish()
|
2018-12-22 05:40:23 -06:00
|
|
|
}
|
|
|
|
|
2020-02-07 22:18:34 -06:00
|
|
|
fn hash_body(
|
2019-01-19 22:44:02 -06:00
|
|
|
hcx: &mut StableHashingContext<'_>,
|
2018-12-22 05:40:23 -06:00
|
|
|
def_path_hash: DefPathHash,
|
2020-01-07 07:22:44 -06:00
|
|
|
item_like: impl for<'a> HashStable<StableHashingContext<'a>>,
|
2018-12-22 05:40:23 -06:00
|
|
|
hir_body_nodes: &mut Vec<(DefPathHash, Fingerprint)>,
|
2020-02-10 07:29:21 -06:00
|
|
|
) -> Fingerprint {
|
2020-02-07 22:18:34 -06:00
|
|
|
let hash = hash(hcx, HirItemLike { item_like: &item_like });
|
2018-12-22 05:40:23 -06:00
|
|
|
hir_body_nodes.push((def_path_hash, hash));
|
2020-02-10 07:29:21 -06:00
|
|
|
hash
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
|
2020-01-07 07:19:35 -06:00
|
|
|
fn upstream_crates(cstore: &dyn CrateStore) -> Vec<(Symbol, Fingerprint, Svh)> {
|
|
|
|
let mut upstream_crates: Vec<_> = cstore
|
|
|
|
.crates_untracked()
|
|
|
|
.iter()
|
|
|
|
.map(|&cnum| {
|
|
|
|
let name = cstore.crate_name_untracked(cnum);
|
|
|
|
let disambiguator = cstore.crate_disambiguator_untracked(cnum).to_fingerprint();
|
|
|
|
let hash = cstore.crate_hash_untracked(cnum);
|
|
|
|
(name, disambiguator, hash)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
upstream_crates.sort_unstable_by_key(|&(name, dis, _)| (name.as_str(), dis));
|
|
|
|
upstream_crates
|
|
|
|
}
|
|
|
|
|
2017-08-18 13:24:19 -05:00
|
|
|
impl<'a, 'hir> NodeCollector<'a, 'hir> {
|
2019-12-22 16:42:04 -06:00
|
|
|
pub(super) fn root(
|
|
|
|
sess: &'a Session,
|
2020-02-07 04:45:04 -06:00
|
|
|
arena: &'hir Arena<'hir>,
|
2019-12-22 16:42:04 -06:00
|
|
|
krate: &'hir Crate<'hir>,
|
|
|
|
definitions: &'a definitions::Definitions,
|
|
|
|
mut hcx: StableHashingContext<'a>,
|
|
|
|
) -> NodeCollector<'a, 'hir> {
|
2019-11-03 06:36:59 -06:00
|
|
|
let root_mod_def_path_hash =
|
|
|
|
definitions.def_path_hash(LocalDefId { local_def_index: CRATE_DEF_INDEX });
|
2017-09-14 10:43:03 -05:00
|
|
|
|
2018-12-22 05:40:23 -06:00
|
|
|
let mut hir_body_nodes = Vec::new();
|
|
|
|
|
2020-02-10 07:29:21 -06:00
|
|
|
let hash = {
|
2017-09-14 10:43:03 -05:00
|
|
|
let Crate {
|
2020-02-07 09:43:36 -06:00
|
|
|
ref item,
|
2017-09-14 10:43:03 -05:00
|
|
|
// These fields are handled separately:
|
|
|
|
exported_macros: _,
|
2019-06-20 17:55:40 -05:00
|
|
|
non_exported_macro_attrs: _,
|
2017-09-14 10:43:03 -05:00
|
|
|
items: _,
|
|
|
|
trait_items: _,
|
|
|
|
impl_items: _,
|
|
|
|
bodies: _,
|
|
|
|
trait_impls: _,
|
|
|
|
body_ids: _,
|
2018-06-06 15:13:52 -05:00
|
|
|
modules: _,
|
2020-02-03 17:34:36 -06:00
|
|
|
proc_macros: _,
|
2020-06-12 12:13:10 -05:00
|
|
|
trait_map: _,
|
2017-09-14 10:43:03 -05:00
|
|
|
} = *krate;
|
|
|
|
|
2020-02-07 22:18:34 -06:00
|
|
|
hash_body(&mut hcx, root_mod_def_path_hash, item, &mut hir_body_nodes)
|
2018-12-22 05:40:23 -06:00
|
|
|
};
|
2017-09-14 10:43:03 -05:00
|
|
|
|
2016-04-13 18:55:34 -05:00
|
|
|
let mut collector = NodeCollector {
|
2020-02-07 04:45:04 -06:00
|
|
|
arena,
|
2017-07-03 13:19:51 -05:00
|
|
|
krate,
|
2018-06-06 15:13:52 -05:00
|
|
|
source_map: sess.source_map(),
|
2019-02-15 08:21:56 -06:00
|
|
|
parent_node: hir::CRATE_HIR_ID,
|
2020-03-18 13:27:59 -05:00
|
|
|
current_dep_node_owner: LocalDefId { local_def_index: CRATE_DEF_INDEX },
|
2017-08-18 13:24:19 -05:00
|
|
|
definitions,
|
2017-09-14 10:43:03 -05:00
|
|
|
hcx,
|
|
|
|
hir_body_nodes,
|
2020-02-10 10:00:49 -06:00
|
|
|
map: (0..definitions.def_index_count())
|
|
|
|
.map(|_| HirOwnerData { signature: None, with_bodies: None })
|
|
|
|
.collect(),
|
2016-04-13 18:55:34 -05:00
|
|
|
};
|
2019-12-22 16:42:04 -06:00
|
|
|
collector.insert_entry(
|
|
|
|
hir::CRATE_HIR_ID,
|
2020-02-07 22:18:34 -06:00
|
|
|
Entry { parent: hir::CRATE_HIR_ID, node: Node::Crate(&krate.item) },
|
2020-02-10 07:29:21 -06:00
|
|
|
hash,
|
2019-12-22 16:42:04 -06:00
|
|
|
);
|
2016-04-13 18:55:34 -05:00
|
|
|
|
|
|
|
collector
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
pub(super) fn finalize_and_compute_crate_hash(
|
|
|
|
mut self,
|
|
|
|
crate_disambiguator: CrateDisambiguator,
|
|
|
|
cstore: &dyn CrateStore,
|
|
|
|
commandline_args_hash: u64,
|
2020-03-18 13:27:59 -05:00
|
|
|
) -> (IndexVec<LocalDefId, HirOwnerData<'hir>>, Svh) {
|
2020-02-07 06:13:35 -06:00
|
|
|
// Insert bodies into the map
|
|
|
|
for (id, body) in self.krate.bodies.iter() {
|
2020-02-10 10:00:49 -06:00
|
|
|
let bodies = &mut self.map[id.hir_id.owner].with_bodies.as_mut().unwrap().bodies;
|
2020-02-07 06:13:35 -06:00
|
|
|
assert!(bodies.insert(id.hir_id.local_id, body).is_none());
|
|
|
|
}
|
|
|
|
|
2018-09-12 05:31:11 -05:00
|
|
|
self.hir_body_nodes.sort_unstable_by_key(|bn| bn.0);
|
2017-09-14 10:43:03 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
let node_hashes = self.hir_body_nodes.iter().fold(
|
|
|
|
Fingerprint::ZERO,
|
|
|
|
|combined_fingerprint, &(def_path_hash, fingerprint)| {
|
2018-12-31 02:14:09 -06:00
|
|
|
combined_fingerprint.combine(def_path_hash.0.combine(fingerprint))
|
2019-12-22 16:42:04 -06:00
|
|
|
},
|
|
|
|
);
|
2017-09-14 10:43:03 -05:00
|
|
|
|
2020-01-07 07:19:35 -06:00
|
|
|
let upstream_crates = upstream_crates(cstore);
|
2017-12-01 08:56:37 -06:00
|
|
|
|
2018-02-12 10:21:01 -06:00
|
|
|
// We hash the final, remapped names of all local source files so we
|
|
|
|
// don't have to include the path prefix remapping commandline args.
|
|
|
|
// If we included the full mapping in the SVH, we could only have
|
|
|
|
// reproducible builds by compiling from the same directory. So we just
|
|
|
|
// hash the result of the mapping instead of the mapping itself.
|
2018-11-21 12:35:14 -06:00
|
|
|
let mut source_file_names: Vec<_> = self
|
|
|
|
.source_map
|
2018-02-12 10:21:01 -06:00
|
|
|
.files()
|
|
|
|
.iter()
|
2020-02-07 13:02:24 -06:00
|
|
|
.filter(|source_file| source_file.cnum == LOCAL_CRATE)
|
2018-08-18 05:13:56 -05:00
|
|
|
.map(|source_file| source_file.name_hash)
|
2018-02-12 10:21:01 -06:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
source_file_names.sort_unstable();
|
|
|
|
|
2018-12-22 05:40:23 -06:00
|
|
|
let crate_hash_input = (
|
|
|
|
((node_hashes, upstream_crates), source_file_names),
|
2019-12-22 16:42:04 -06:00
|
|
|
(commandline_args_hash, crate_disambiguator.to_fingerprint()),
|
2018-12-22 05:40:23 -06:00
|
|
|
);
|
|
|
|
|
2020-02-06 05:46:26 -06:00
|
|
|
let mut stable_hasher = StableHasher::new();
|
|
|
|
crate_hash_input.hash_stable(&mut self.hcx, &mut stable_hasher);
|
|
|
|
let crate_hash: Fingerprint = stable_hasher.finish();
|
2018-12-22 05:40:23 -06:00
|
|
|
|
|
|
|
let svh = Svh::new(crate_hash.to_smaller_hash());
|
2020-02-10 10:00:49 -06:00
|
|
|
(self.map, svh)
|
2017-08-18 13:24:19 -05:00
|
|
|
}
|
|
|
|
|
2020-02-10 07:29:21 -06:00
|
|
|
fn insert_entry(&mut self, id: HirId, entry: Entry<'hir>, hash: Fingerprint) {
|
2020-02-07 04:45:04 -06:00
|
|
|
let i = id.local_id.as_u32() as usize;
|
|
|
|
|
|
|
|
let arena = self.arena;
|
|
|
|
|
2020-02-10 10:00:49 -06:00
|
|
|
let data = &mut self.map[id.owner];
|
|
|
|
|
|
|
|
if data.with_bodies.is_none() {
|
2020-03-17 20:48:17 -05:00
|
|
|
data.with_bodies = Some(arena.alloc(OwnerNodes {
|
2020-02-10 07:29:21 -06:00
|
|
|
hash,
|
2020-03-17 20:48:17 -05:00
|
|
|
nodes: IndexVec::new(),
|
2020-02-09 05:13:08 -06:00
|
|
|
bodies: FxHashMap::default(),
|
2020-02-10 10:00:49 -06:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
2020-03-17 20:48:17 -05:00
|
|
|
let nodes = data.with_bodies.as_mut().unwrap();
|
2020-02-07 04:45:04 -06:00
|
|
|
|
|
|
|
if i == 0 {
|
2020-02-10 07:29:21 -06:00
|
|
|
// Overwrite the dummy hash with the real HIR owner hash.
|
2020-03-17 20:48:17 -05:00
|
|
|
nodes.hash = hash;
|
2020-02-09 05:13:08 -06:00
|
|
|
|
2020-02-10 10:00:49 -06:00
|
|
|
// FIXME: feature(impl_trait_in_bindings) broken and trigger this assert
|
|
|
|
//assert!(data.signature.is_none());
|
|
|
|
|
|
|
|
data.signature =
|
2020-03-17 20:48:17 -05:00
|
|
|
Some(self.arena.alloc(Owner { parent: entry.parent, node: entry.node }));
|
2020-02-07 04:45:04 -06:00
|
|
|
} else {
|
|
|
|
assert_eq!(entry.parent.owner, id.owner);
|
2020-02-10 10:00:49 -06:00
|
|
|
insert_vec_map(
|
2020-03-17 20:48:17 -05:00
|
|
|
&mut nodes.nodes,
|
2020-02-10 10:00:49 -06:00
|
|
|
id.local_id,
|
2020-03-17 20:48:17 -05:00
|
|
|
ParentedNode { parent: entry.parent.local_id, node: entry.node },
|
2020-02-10 10:00:49 -06:00
|
|
|
);
|
2020-02-07 04:45:04 -06:00
|
|
|
}
|
2016-04-13 18:55:34 -05:00
|
|
|
}
|
|
|
|
|
2019-02-15 03:30:44 -06:00
|
|
|
fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
|
2020-02-10 07:29:21 -06:00
|
|
|
self.insert_with_hash(span, hir_id, node, Fingerprint::ZERO)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn insert_with_hash(&mut self, span: Span, hir_id: HirId, node: Node<'hir>, hash: Fingerprint) {
|
2020-02-07 22:18:34 -06:00
|
|
|
let entry = Entry { parent: self.parent_node, node };
|
2017-08-18 13:24:19 -05:00
|
|
|
|
|
|
|
// Make sure that the DepNode of some node coincides with the HirId
|
|
|
|
// owner of that node.
|
|
|
|
if cfg!(debug_assertions) {
|
2020-03-27 10:43:20 -05:00
|
|
|
let node_id = self.definitions.hir_id_to_node_id(hir_id);
|
2017-08-18 13:24:19 -05:00
|
|
|
|
2018-06-20 03:44:31 -05:00
|
|
|
if hir_id.owner != self.current_dep_node_owner {
|
2019-11-03 06:36:59 -06:00
|
|
|
let node_str = match self.definitions.opt_local_def_id(node_id) {
|
|
|
|
Some(def_id) => self.definitions.def_path(def_id).to_string_no_crate(),
|
2019-12-22 16:42:04 -06:00
|
|
|
None => format!("{:?}", node),
|
2017-08-18 13:24:19 -05:00
|
|
|
};
|
|
|
|
|
2018-11-21 12:35:14 -06:00
|
|
|
span_bug!(
|
|
|
|
span,
|
|
|
|
"inconsistent DepNode at `{:?}` for `{}`: \
|
2020-04-13 11:52:40 -05:00
|
|
|
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
|
2018-11-21 12:35:14 -06:00
|
|
|
self.source_map.span_to_string(span),
|
2017-08-18 13:24:19 -05:00
|
|
|
node_str,
|
2019-11-03 06:36:59 -06:00
|
|
|
self.definitions.def_path(self.current_dep_node_owner).to_string_no_crate(),
|
2018-10-11 03:15:18 -05:00
|
|
|
self.current_dep_node_owner,
|
2019-11-03 06:36:59 -06:00
|
|
|
self.definitions.def_path(hir_id.owner).to_string_no_crate(),
|
2018-10-11 03:15:18 -05:00
|
|
|
hir_id.owner,
|
2018-11-21 12:35:14 -06:00
|
|
|
)
|
2017-08-18 13:24:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 07:29:21 -06:00
|
|
|
self.insert_entry(hir_id, entry, hash);
|
2016-04-13 18:55:34 -05:00
|
|
|
}
|
2016-04-14 00:24:30 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_node_id: HirId, f: F) {
|
2019-02-15 08:21:56 -06:00
|
|
|
let parent_node = self.parent_node;
|
|
|
|
self.parent_node = parent_node_id;
|
2016-04-14 00:24:30 -05:00
|
|
|
f(self);
|
2019-02-15 08:21:56 -06:00
|
|
|
self.parent_node = parent_node;
|
2016-04-14 00:24:30 -05:00
|
|
|
}
|
2017-08-18 13:24:19 -05:00
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn with_dep_node_owner<
|
|
|
|
T: for<'b> HashStable<StableHashingContext<'b>>,
|
2020-02-10 07:29:21 -06:00
|
|
|
F: FnOnce(&mut Self, Fingerprint),
|
2019-12-22 16:42:04 -06:00
|
|
|
>(
|
|
|
|
&mut self,
|
2020-03-18 13:27:59 -05:00
|
|
|
dep_node_owner: LocalDefId,
|
2019-12-22 16:42:04 -06:00
|
|
|
item_like: &T,
|
|
|
|
f: F,
|
|
|
|
) {
|
2017-08-18 13:24:19 -05:00
|
|
|
let prev_owner = self.current_dep_node_owner;
|
2017-09-14 10:43:03 -05:00
|
|
|
|
2019-11-03 06:36:59 -06:00
|
|
|
let def_path_hash = self.definitions.def_path_hash(dep_node_owner);
|
2017-09-14 10:43:03 -05:00
|
|
|
|
2020-02-10 07:29:21 -06:00
|
|
|
let hash = hash_body(&mut self.hcx, def_path_hash, item_like, &mut self.hir_body_nodes);
|
2017-09-14 10:43:03 -05:00
|
|
|
|
2017-08-18 13:24:19 -05:00
|
|
|
self.current_dep_node_owner = dep_node_owner;
|
2020-02-10 07:29:21 -06:00
|
|
|
f(self, hash);
|
2017-08-18 13:24:19 -05:00
|
|
|
self.current_dep_node_owner = prev_owner;
|
|
|
|
}
|
2016-04-13 18:55:34 -05:00
|
|
|
}
|
|
|
|
|
2017-08-18 13:24:19 -05:00
|
|
|
impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
|
2020-01-07 10:25:33 -06:00
|
|
|
type Map = Map<'hir>;
|
|
|
|
|
2016-04-13 18:55:34 -05:00
|
|
|
/// Because we want to track parent items and so forth, enable
|
|
|
|
/// deep walking so that we walk nested items in the context of
|
|
|
|
/// their outer items.
|
2016-11-09 15:45:26 -06:00
|
|
|
|
2020-02-09 08:32:00 -06:00
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
2019-09-05 21:57:44 -05:00
|
|
|
panic!("`visit_nested_xxx` must be manually implemented in this visitor");
|
2016-11-09 15:45:26 -06:00
|
|
|
}
|
|
|
|
|
2016-04-13 18:55:34 -05:00
|
|
|
fn visit_nested_item(&mut self, item: ItemId) {
|
|
|
|
debug!("visit_nested_item: {:?}", item);
|
2017-01-03 20:01:58 -06:00
|
|
|
self.visit_item(self.krate.item(item.id));
|
2016-04-13 18:55:34 -05:00
|
|
|
}
|
|
|
|
|
2016-12-03 20:21:06 -06:00
|
|
|
fn visit_nested_trait_item(&mut self, item_id: TraitItemId) {
|
2017-01-03 20:01:58 -06:00
|
|
|
self.visit_trait_item(self.krate.trait_item(item_id));
|
2016-12-03 20:21:06 -06:00
|
|
|
}
|
|
|
|
|
2016-11-04 17:20:15 -05:00
|
|
|
fn visit_nested_impl_item(&mut self, item_id: ImplItemId) {
|
2017-01-03 20:01:58 -06:00
|
|
|
self.visit_impl_item(self.krate.impl_item(item_id));
|
2016-11-04 17:20:15 -05:00
|
|
|
}
|
|
|
|
|
2016-12-21 04:32:59 -06:00
|
|
|
fn visit_nested_body(&mut self, id: BodyId) {
|
2017-01-03 20:01:58 -06:00
|
|
|
self.visit_body(self.krate.body(id));
|
2016-10-28 15:58:32 -05:00
|
|
|
}
|
|
|
|
|
2019-11-29 06:43:03 -06:00
|
|
|
fn visit_param(&mut self, param: &'hir Param<'hir>) {
|
2019-08-27 06:24:32 -05:00
|
|
|
let node = Node::Param(param);
|
|
|
|
self.insert(param.pat.span, param.hir_id, node);
|
|
|
|
self.with_parent(param.hir_id, |this| {
|
|
|
|
intravisit::walk_param(this, param);
|
2019-07-26 17:52:37 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-28 12:28:50 -06:00
|
|
|
fn visit_item(&mut self, i: &'hir Item<'hir>) {
|
2016-04-13 18:55:34 -05:00
|
|
|
debug!("visit_item: {:?}", i);
|
2019-12-22 16:42:04 -06:00
|
|
|
debug_assert_eq!(
|
2019-11-03 06:36:59 -06:00
|
|
|
i.hir_id.owner,
|
2020-03-27 10:43:20 -05:00
|
|
|
self.definitions
|
|
|
|
.opt_local_def_id(self.definitions.hir_id_to_node_id(i.hir_id))
|
|
|
|
.unwrap()
|
2019-12-22 16:42:04 -06:00
|
|
|
);
|
2020-02-10 07:29:21 -06:00
|
|
|
self.with_dep_node_owner(i.hir_id.owner, i, |this, hash| {
|
|
|
|
this.insert_with_hash(i.span, i.hir_id, Node::Item(i), hash);
|
2019-02-15 05:14:36 -06:00
|
|
|
this.with_parent(i.hir_id, |this| {
|
2019-09-26 11:51:36 -05:00
|
|
|
if let ItemKind::Struct(ref struct_def, _) = i.kind {
|
2019-03-21 17:38:50 -05:00
|
|
|
// If this is a tuple or unit-like struct, register the constructor.
|
|
|
|
if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
|
2019-03-24 12:21:59 -05:00
|
|
|
this.insert(i.span, ctor_hir_id, Node::Ctor(struct_def));
|
2016-04-14 00:24:30 -05:00
|
|
|
}
|
2016-04-13 18:55:34 -05:00
|
|
|
}
|
2017-08-18 13:24:19 -05:00
|
|
|
intravisit::walk_item(this, i);
|
|
|
|
});
|
2016-04-14 00:24:30 -05:00
|
|
|
});
|
2016-04-13 18:55:34 -05:00
|
|
|
}
|
|
|
|
|
2019-11-28 13:18:29 -06:00
|
|
|
fn visit_foreign_item(&mut self, foreign_item: &'hir ForeignItem<'hir>) {
|
2019-02-15 03:30:44 -06:00
|
|
|
self.insert(foreign_item.span, foreign_item.hir_id, Node::ForeignItem(foreign_item));
|
2016-04-13 18:55:34 -05:00
|
|
|
|
2019-02-15 05:14:36 -06:00
|
|
|
self.with_parent(foreign_item.hir_id, |this| {
|
2016-04-14 00:24:30 -05:00
|
|
|
intravisit::walk_foreign_item(this, foreign_item);
|
|
|
|
});
|
2016-04-13 18:55:34 -05:00
|
|
|
}
|
|
|
|
|
2019-11-30 10:46:46 -06:00
|
|
|
fn visit_generic_param(&mut self, param: &'hir GenericParam<'hir>) {
|
2019-02-15 03:30:44 -06:00
|
|
|
self.insert(param.span, param.hir_id, Node::GenericParam(param));
|
2018-05-30 06:36:53 -05:00
|
|
|
intravisit::walk_generic_param(self, param);
|
2016-04-13 18:55:34 -05:00
|
|
|
}
|
|
|
|
|
2019-11-28 14:47:10 -06:00
|
|
|
fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
|
2019-12-22 16:42:04 -06:00
|
|
|
debug_assert_eq!(
|
2019-11-03 06:36:59 -06:00
|
|
|
ti.hir_id.owner,
|
2020-03-27 10:43:20 -05:00
|
|
|
self.definitions
|
|
|
|
.opt_local_def_id(self.definitions.hir_id_to_node_id(ti.hir_id))
|
|
|
|
.unwrap()
|
2019-12-22 16:42:04 -06:00
|
|
|
);
|
2020-02-10 07:29:21 -06:00
|
|
|
self.with_dep_node_owner(ti.hir_id.owner, ti, |this, hash| {
|
|
|
|
this.insert_with_hash(ti.span, ti.hir_id, Node::TraitItem(ti), hash);
|
2017-08-18 13:24:19 -05:00
|
|
|
|
2019-02-15 05:14:36 -06:00
|
|
|
this.with_parent(ti.hir_id, |this| {
|
2017-08-18 13:24:19 -05:00
|
|
|
intravisit::walk_trait_item(this, ti);
|
|
|
|
});
|
2016-04-14 00:24:30 -05:00
|
|
|
});
|
2016-04-13 18:55:34 -05:00
|
|
|
}
|
|
|
|
|
2019-11-28 15:16:44 -06:00
|
|
|
fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
|
2019-12-22 16:42:04 -06:00
|
|
|
debug_assert_eq!(
|
2019-11-03 06:36:59 -06:00
|
|
|
ii.hir_id.owner,
|
2020-03-27 10:43:20 -05:00
|
|
|
self.definitions
|
|
|
|
.opt_local_def_id(self.definitions.hir_id_to_node_id(ii.hir_id))
|
|
|
|
.unwrap()
|
2019-12-22 16:42:04 -06:00
|
|
|
);
|
2020-02-10 07:29:21 -06:00
|
|
|
self.with_dep_node_owner(ii.hir_id.owner, ii, |this, hash| {
|
|
|
|
this.insert_with_hash(ii.span, ii.hir_id, Node::ImplItem(ii), hash);
|
2017-08-18 13:24:19 -05:00
|
|
|
|
2019-02-15 05:14:36 -06:00
|
|
|
this.with_parent(ii.hir_id, |this| {
|
2017-08-18 13:24:19 -05:00
|
|
|
intravisit::walk_impl_item(this, ii);
|
|
|
|
});
|
2016-04-14 00:24:30 -05:00
|
|
|
});
|
2016-04-13 18:55:34 -05:00
|
|
|
}
|
|
|
|
|
2019-11-29 06:43:03 -06:00
|
|
|
fn visit_pat(&mut self, pat: &'hir Pat<'hir>) {
|
2019-12-22 16:42:04 -06:00
|
|
|
let node =
|
|
|
|
if let PatKind::Binding(..) = pat.kind { Node::Binding(pat) } else { Node::Pat(pat) };
|
2019-02-15 03:30:44 -06:00
|
|
|
self.insert(pat.span, pat.hir_id, node);
|
2015-09-10 14:53:08 -05:00
|
|
|
|
2019-02-15 05:14:36 -06:00
|
|
|
self.with_parent(pat.hir_id, |this| {
|
2016-04-14 00:24:30 -05:00
|
|
|
intravisit::walk_pat(this, pat);
|
|
|
|
});
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
|
2019-11-29 06:43:03 -06:00
|
|
|
fn visit_arm(&mut self, arm: &'hir Arm<'hir>) {
|
2019-03-30 17:54:29 -05:00
|
|
|
let node = Node::Arm(arm);
|
|
|
|
|
|
|
|
self.insert(arm.span, arm.hir_id, node);
|
|
|
|
|
|
|
|
self.with_parent(arm.hir_id, |this| {
|
|
|
|
intravisit::walk_arm(this, arm);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-17 13:28:50 -05:00
|
|
|
fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
|
2019-02-15 03:30:44 -06:00
|
|
|
self.insert(DUMMY_SP, constant.hir_id, Node::AnonConst(constant));
|
2018-05-17 13:28:50 -05:00
|
|
|
|
2019-02-15 05:14:36 -06:00
|
|
|
self.with_parent(constant.hir_id, |this| {
|
2018-05-17 13:28:50 -05:00
|
|
|
intravisit::walk_anon_const(this, constant);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-29 06:43:03 -06:00
|
|
|
fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
|
2019-02-15 03:30:44 -06:00
|
|
|
self.insert(expr.span, expr.hir_id, Node::Expr(expr));
|
2015-09-10 14:53:08 -05:00
|
|
|
|
2019-02-15 05:14:36 -06:00
|
|
|
self.with_parent(expr.hir_id, |this| {
|
2016-04-14 00:24:30 -05:00
|
|
|
intravisit::walk_expr(this, expr);
|
|
|
|
});
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
|
2019-11-29 06:43:03 -06:00
|
|
|
fn visit_stmt(&mut self, stmt: &'hir Stmt<'hir>) {
|
2019-02-15 03:30:44 -06:00
|
|
|
self.insert(stmt.span, stmt.hir_id, Node::Stmt(stmt));
|
2016-04-14 00:24:30 -05:00
|
|
|
|
2019-02-15 05:14:36 -06:00
|
|
|
self.with_parent(stmt.hir_id, |this| {
|
2016-04-14 00:24:30 -05:00
|
|
|
intravisit::walk_stmt(this, stmt);
|
|
|
|
});
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
|
2019-11-30 10:46:46 -06:00
|
|
|
fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment<'hir>) {
|
2019-02-15 08:21:56 -06:00
|
|
|
if let Some(hir_id) = path_segment.hir_id {
|
2019-02-15 03:30:44 -06:00
|
|
|
self.insert(path_span, hir_id, Node::PathSegment(path_segment));
|
2018-09-10 22:08:47 -05:00
|
|
|
}
|
|
|
|
intravisit::walk_path_segment(self, path_span, path_segment);
|
|
|
|
}
|
|
|
|
|
2019-11-30 10:46:46 -06:00
|
|
|
fn visit_ty(&mut self, ty: &'hir Ty<'hir>) {
|
2019-02-15 03:30:44 -06:00
|
|
|
self.insert(ty.span, ty.hir_id, Node::Ty(ty));
|
2016-08-05 19:12:20 -05:00
|
|
|
|
2019-02-15 05:14:36 -06:00
|
|
|
self.with_parent(ty.hir_id, |this| {
|
2016-08-05 19:12:20 -05:00
|
|
|
intravisit::walk_ty(this, ty);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-30 10:46:46 -06:00
|
|
|
fn visit_trait_ref(&mut self, tr: &'hir TraitRef<'hir>) {
|
2019-02-15 03:30:44 -06:00
|
|
|
self.insert(tr.path.span, tr.hir_ref_id, Node::TraitRef(tr));
|
2016-10-30 01:04:52 -05:00
|
|
|
|
2019-02-15 05:14:36 -06:00
|
|
|
self.with_parent(tr.hir_ref_id, |this| {
|
2016-10-30 01:04:52 -05:00
|
|
|
intravisit::walk_trait_ref(this, tr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-12-22 16:42:04 -06:00
|
|
|
fn visit_fn(
|
|
|
|
&mut self,
|
|
|
|
fk: intravisit::FnKind<'hir>,
|
2019-11-30 10:46:46 -06:00
|
|
|
fd: &'hir FnDecl<'hir>,
|
2019-12-22 16:42:04 -06:00
|
|
|
b: BodyId,
|
|
|
|
s: Span,
|
|
|
|
id: HirId,
|
|
|
|
) {
|
2019-02-15 08:21:56 -06:00
|
|
|
assert_eq!(self.parent_node, id);
|
2016-07-28 04:58:45 -05:00
|
|
|
intravisit::walk_fn(self, fk, fd, b, s, id);
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
|
2019-11-29 06:43:03 -06:00
|
|
|
fn visit_block(&mut self, block: &'hir Block<'hir>) {
|
2019-02-15 03:30:44 -06:00
|
|
|
self.insert(block.span, block.hir_id, Node::Block(block));
|
2019-02-15 05:14:36 -06:00
|
|
|
self.with_parent(block.hir_id, |this| {
|
2016-04-14 00:24:30 -05:00
|
|
|
intravisit::walk_block(this, block);
|
|
|
|
});
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
|
|
|
|
2019-11-29 06:43:03 -06:00
|
|
|
fn visit_local(&mut self, l: &'hir Local<'hir>) {
|
2019-02-15 03:30:44 -06:00
|
|
|
self.insert(l.span, l.hir_id, Node::Local(l));
|
2019-12-22 16:42:04 -06:00
|
|
|
self.with_parent(l.hir_id, |this| intravisit::walk_local(this, l))
|
2017-08-18 01:56:11 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
|
2019-02-15 03:30:44 -06:00
|
|
|
self.insert(lifetime.span, lifetime.hir_id, Node::Lifetime(lifetime));
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
2016-10-28 01:52:45 -05:00
|
|
|
|
2019-11-30 10:46:46 -06:00
|
|
|
fn visit_vis(&mut self, visibility: &'hir Visibility<'hir>) {
|
2018-06-30 22:34:18 -05:00
|
|
|
match visibility.node {
|
2019-12-22 16:42:04 -06:00
|
|
|
VisibilityKind::Public | VisibilityKind::Crate(_) | VisibilityKind::Inherited => {}
|
2019-02-15 05:14:36 -06:00
|
|
|
VisibilityKind::Restricted { hir_id, .. } => {
|
2019-02-15 03:30:44 -06:00
|
|
|
self.insert(visibility.span, hir_id, Node::Visibility(visibility));
|
2019-02-15 05:14:36 -06:00
|
|
|
self.with_parent(hir_id, |this| {
|
2016-10-30 01:04:52 -05:00
|
|
|
intravisit::walk_vis(this, visibility);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 16:50:47 -06:00
|
|
|
fn visit_macro_def(&mut self, macro_def: &'hir MacroDef<'hir>) {
|
2020-03-18 13:27:59 -05:00
|
|
|
self.with_dep_node_owner(macro_def.hir_id.owner, macro_def, |this, hash| {
|
2020-02-10 07:29:21 -06:00
|
|
|
this.insert_with_hash(
|
|
|
|
macro_def.span,
|
|
|
|
macro_def.hir_id,
|
|
|
|
Node::MacroDef(macro_def),
|
|
|
|
hash,
|
|
|
|
);
|
2017-09-14 10:43:03 -05:00
|
|
|
});
|
2016-10-28 01:52:45 -05:00
|
|
|
}
|
2016-11-09 12:57:48 -06:00
|
|
|
|
2019-11-30 10:46:46 -06:00
|
|
|
fn visit_variant(&mut self, v: &'hir Variant<'hir>, g: &'hir Generics<'hir>, item_id: HirId) {
|
2019-08-13 19:40:21 -05:00
|
|
|
self.insert(v.span, v.id, Node::Variant(v));
|
|
|
|
self.with_parent(v.id, |this| {
|
2019-03-21 17:38:50 -05:00
|
|
|
// Register the constructor of this variant.
|
2019-08-13 19:40:21 -05:00
|
|
|
if let Some(ctor_hir_id) = v.data.ctor_hir_id() {
|
|
|
|
this.insert(v.span, ctor_hir_id, Node::Ctor(&v.data));
|
2019-03-21 17:38:50 -05:00
|
|
|
}
|
2016-12-21 04:32:59 -06:00
|
|
|
intravisit::walk_variant(this, v, g, item_id);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-29 02:40:33 -06:00
|
|
|
fn visit_struct_field(&mut self, field: &'hir StructField<'hir>) {
|
2019-02-15 03:30:44 -06:00
|
|
|
self.insert(field.span, field.hir_id, Node::Field(field));
|
2019-02-15 05:14:36 -06:00
|
|
|
self.with_parent(field.hir_id, |this| {
|
2016-11-09 12:57:48 -06:00
|
|
|
intravisit::walk_struct_field(this, field);
|
|
|
|
});
|
|
|
|
}
|
2017-08-18 13:24:19 -05:00
|
|
|
|
|
|
|
fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
|
|
|
|
// Do not visit the duplicate information in TraitItemRef. We want to
|
|
|
|
// map the actual nodes, not the duplicate ones in the *Ref.
|
2019-12-22 16:42:04 -06:00
|
|
|
let TraitItemRef { id, ident: _, kind: _, span: _, defaultness: _ } = *ii;
|
2017-08-18 13:24:19 -05:00
|
|
|
|
|
|
|
self.visit_nested_trait_item(id);
|
|
|
|
}
|
|
|
|
|
2019-11-30 10:46:46 -06:00
|
|
|
fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef<'hir>) {
|
2017-08-18 13:24:19 -05:00
|
|
|
// Do not visit the duplicate information in ImplItemRef. We want to
|
|
|
|
// map the actual nodes, not the duplicate ones in the *Ref.
|
2019-12-22 16:42:04 -06:00
|
|
|
let ImplItemRef { id, ident: _, kind: _, span: _, vis: _, defaultness: _ } = *ii;
|
2017-08-18 13:24:19 -05:00
|
|
|
|
|
|
|
self.visit_nested_impl_item(id);
|
|
|
|
}
|
2015-09-10 14:53:08 -05:00
|
|
|
}
|
2017-09-14 10:43:03 -05:00
|
|
|
|
|
|
|
struct HirItemLike<T> {
|
|
|
|
item_like: T,
|
|
|
|
}
|
|
|
|
|
2019-06-11 04:21:38 -05:00
|
|
|
impl<'hir, T> HashStable<StableHashingContext<'hir>> for HirItemLike<T>
|
2019-06-11 05:20:33 -05:00
|
|
|
where
|
|
|
|
T: HashStable<StableHashingContext<'hir>>,
|
2017-09-14 10:43:03 -05:00
|
|
|
{
|
2019-09-26 17:54:39 -05:00
|
|
|
fn hash_stable(&self, hcx: &mut StableHashingContext<'hir>, hasher: &mut StableHasher) {
|
2020-02-07 22:18:34 -06:00
|
|
|
hcx.while_hashing_hir_bodies(true, |hcx| {
|
2017-09-14 10:43:03 -05:00
|
|
|
self.item_like.hash_stable(hcx, hasher);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|