2019-02-05 11:20:45 -06:00
|
|
|
use crate::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
|
|
|
|
use crate::hir::{self, intravisit, HirId, ItemLocalId};
|
|
|
|
use crate::hir::itemlikevisit::ItemLikeVisitor;
|
2019-02-15 10:46:28 +01:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2018-06-06 22:13:52 +02:00
|
|
|
use rustc_data_structures::sync::{Lock, ParallelIterator, par_iter};
|
2017-03-14 15:50:40 +01:00
|
|
|
|
2019-06-21 18:12:39 +02:00
|
|
|
pub fn check_crate(hir_map: &hir::map::Map<'_>) {
|
2017-12-28 06:05:45 +01:00
|
|
|
hir_map.dep_graph.assert_ignored();
|
|
|
|
|
2018-06-06 22:13:52 +02:00
|
|
|
let errors = Lock::new(Vec::new());
|
|
|
|
|
|
|
|
par_iter(&hir_map.krate().modules).for_each(|(module_id, _)| {
|
2019-06-27 11:16:59 +02:00
|
|
|
let local_def_id = hir_map.local_def_id_from_node_id(*module_id);
|
|
|
|
hir_map.visit_item_likes_in_module(local_def_id, &mut OuterVisitor {
|
2018-06-06 22:13:52 +02:00
|
|
|
hir_map,
|
|
|
|
errors: &errors,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let errors = errors.into_inner();
|
|
|
|
|
|
|
|
if !errors.is_empty() {
|
|
|
|
let message = errors
|
2017-12-28 06:05:45 +01:00
|
|
|
.iter()
|
|
|
|
.fold(String::new(), |s1, s2| s1 + "\n" + s2);
|
|
|
|
bug!("{}", message);
|
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
struct HirIdValidator<'a, 'hir> {
|
2017-03-14 15:50:40 +01:00
|
|
|
hir_map: &'a hir::map::Map<'hir>,
|
|
|
|
owner_def_index: Option<DefIndex>,
|
2019-02-15 10:46:28 +01:00
|
|
|
hir_ids_seen: FxHashSet<ItemLocalId>,
|
2018-06-06 22:13:52 +02:00
|
|
|
errors: &'a Lock<Vec<String>>,
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
struct OuterVisitor<'a, 'hir> {
|
2017-03-14 15:50:40 +01:00
|
|
|
hir_map: &'a hir::map::Map<'hir>,
|
2018-06-06 22:13:52 +02:00
|
|
|
errors: &'a Lock<Vec<String>>,
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
impl<'a, 'hir> OuterVisitor<'a, 'hir> {
|
2017-03-14 15:50:40 +01:00
|
|
|
fn new_inner_visitor(&self,
|
|
|
|
hir_map: &'a hir::map::Map<'hir>)
|
|
|
|
-> HirIdValidator<'a, 'hir> {
|
|
|
|
HirIdValidator {
|
2017-07-03 11:19:51 -07:00
|
|
|
hir_map,
|
2017-03-14 15:50:40 +01:00
|
|
|
owner_def_index: None,
|
2018-10-16 16:57:53 +02:00
|
|
|
hir_ids_seen: Default::default(),
|
2018-06-06 22:13:52 +02:00
|
|
|
errors: self.errors,
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
impl<'a, 'hir> ItemLikeVisitor<'hir> for OuterVisitor<'a, 'hir> {
|
2017-03-14 15:50:40 +01:00
|
|
|
fn visit_item(&mut self, i: &'hir hir::Item) {
|
|
|
|
let mut inner_visitor = self.new_inner_visitor(self.hir_map);
|
2019-02-06 14:16:11 +01:00
|
|
|
inner_visitor.check(i.hir_id, |this| intravisit::walk_item(this, i));
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, i: &'hir hir::TraitItem) {
|
|
|
|
let mut inner_visitor = self.new_inner_visitor(self.hir_map);
|
2019-02-06 14:16:11 +01:00
|
|
|
inner_visitor.check(i.hir_id, |this| intravisit::walk_trait_item(this, i));
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_impl_item(&mut self, i: &'hir hir::ImplItem) {
|
|
|
|
let mut inner_visitor = self.new_inner_visitor(self.hir_map);
|
2019-02-06 14:16:11 +01:00
|
|
|
inner_visitor.check(i.hir_id, |this| intravisit::walk_impl_item(this, i));
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
impl<'a, 'hir> HirIdValidator<'a, 'hir> {
|
2018-06-06 22:13:52 +02:00
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
|
|
|
fn error(&self, f: impl FnOnce() -> String) {
|
|
|
|
self.errors.lock().push(f());
|
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
fn check<F: FnOnce(&mut HirIdValidator<'a, 'hir>)>(&mut self,
|
2019-02-06 14:16:11 +01:00
|
|
|
hir_id: HirId,
|
2017-03-14 15:50:40 +01:00
|
|
|
walk: F) {
|
|
|
|
assert!(self.owner_def_index.is_none());
|
2019-06-27 11:28:14 +02:00
|
|
|
let owner_def_index = self.hir_map.local_def_id(hir_id).index;
|
2017-03-14 15:50:40 +01:00
|
|
|
self.owner_def_index = Some(owner_def_index);
|
|
|
|
walk(self);
|
|
|
|
|
|
|
|
if owner_def_index == CRATE_DEF_INDEX {
|
2018-10-11 21:15:18 +13:00
|
|
|
return;
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// There's always at least one entry for the owning item itself
|
|
|
|
let max = self.hir_ids_seen
|
2019-02-15 10:46:28 +01:00
|
|
|
.iter()
|
2017-03-14 15:50:40 +01:00
|
|
|
.map(|local_id| local_id.as_usize())
|
|
|
|
.max()
|
2018-05-22 14:31:56 +02:00
|
|
|
.expect("owning item has no entry");
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
if max != self.hir_ids_seen.len() - 1 {
|
|
|
|
// Collect the missing ItemLocalIds
|
2018-12-04 11:17:58 -08:00
|
|
|
let missing: Vec<_> = (0 ..= max as u32)
|
2019-02-15 15:21:56 +01:00
|
|
|
.filter(|&i| !self.hir_ids_seen.contains(&ItemLocalId::from_u32(i)))
|
|
|
|
.collect();
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
// Try to map those to something more useful
|
2018-07-25 13:20:53 +02:00
|
|
|
let mut missing_items = Vec::with_capacity(missing.len());
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
for local_id in missing {
|
|
|
|
let hir_id = HirId {
|
|
|
|
owner: owner_def_index,
|
2018-11-07 11:01:18 +01:00
|
|
|
local_id: ItemLocalId::from_u32(local_id),
|
2017-03-14 15:50:40 +01:00
|
|
|
};
|
|
|
|
|
2018-05-22 14:31:56 +02:00
|
|
|
trace!("missing hir id {:#?}", hir_id);
|
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
missing_items.push(format!("[local_id: {}, node:{}]",
|
|
|
|
local_id,
|
2019-06-16 17:30:02 +02:00
|
|
|
self.hir_map.node_to_string(hir_id)));
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2018-06-06 22:13:52 +02:00
|
|
|
self.error(|| format!(
|
2017-03-14 15:50:40 +01:00
|
|
|
"ItemLocalIds not assigned densely in {}. \
|
2018-10-11 21:15:18 +13:00
|
|
|
Max ItemLocalId = {}, missing IDs = {:?}; seens IDs = {:?}",
|
2017-03-14 15:50:40 +01:00
|
|
|
self.hir_map.def_path(DefId::local(owner_def_index)).to_string_no_crate(),
|
|
|
|
max,
|
2018-10-11 21:15:18 +13:00
|
|
|
missing_items,
|
|
|
|
self.hir_ids_seen
|
2019-02-15 10:46:28 +01:00
|
|
|
.iter()
|
|
|
|
.map(|&local_id| HirId {
|
|
|
|
owner: owner_def_index,
|
|
|
|
local_id,
|
|
|
|
})
|
2019-06-16 17:30:02 +02:00
|
|
|
.map(|h| format!("({:?} {})", h, self.hir_map.node_to_string(h)))
|
2018-10-11 21:15:18 +13:00
|
|
|
.collect::<Vec<_>>()));
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
|
2017-03-14 15:50:40 +01:00
|
|
|
|
|
|
|
fn nested_visit_map<'this>(&'this mut self)
|
|
|
|
-> intravisit::NestedVisitorMap<'this, 'hir> {
|
|
|
|
intravisit::NestedVisitorMap::OnlyBodies(self.hir_map)
|
|
|
|
}
|
|
|
|
|
2019-02-06 14:16:11 +01:00
|
|
|
fn visit_id(&mut self, hir_id: HirId) {
|
2018-05-22 14:31:56 +02:00
|
|
|
let owner = self.owner_def_index.expect("no owner_def_index");
|
2017-03-14 15:50:40 +01:00
|
|
|
|
2019-02-06 14:16:11 +01:00
|
|
|
if hir_id == hir::DUMMY_HIR_ID {
|
|
|
|
self.error(|| format!("HirIdValidator: HirId {:?} is invalid",
|
2019-06-16 17:30:02 +02:00
|
|
|
self.hir_map.node_to_string(hir_id)));
|
2018-10-11 21:15:18 +13:00
|
|
|
return;
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
2019-02-06 14:16:11 +01:00
|
|
|
if owner != hir_id.owner {
|
2018-06-06 22:13:52 +02:00
|
|
|
self.error(|| format!(
|
2017-03-14 15:50:40 +01:00
|
|
|
"HirIdValidator: The recorded owner of {} is {} instead of {}",
|
2019-06-16 17:30:02 +02:00
|
|
|
self.hir_map.node_to_string(hir_id),
|
2019-02-06 14:16:11 +01:00
|
|
|
self.hir_map.def_path(DefId::local(hir_id.owner)).to_string_no_crate(),
|
2017-03-14 15:50:40 +01:00
|
|
|
self.hir_map.def_path(DefId::local(owner)).to_string_no_crate()));
|
|
|
|
}
|
|
|
|
|
2019-02-15 10:46:28 +01:00
|
|
|
self.hir_ids_seen.insert(hir_id.local_id);
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_impl_item_ref(&mut self, _: &'hir hir::ImplItemRef) {
|
|
|
|
// Explicitly do nothing here. ImplItemRefs contain hir::Visibility
|
2018-07-11 23:36:06 +08:00
|
|
|
// values that actually belong to an ImplItem instead of the ItemKind::Impl
|
2017-03-14 15:50:40 +01:00
|
|
|
// we are currently in. So for those it's correct that they have a
|
|
|
|
// different owner.
|
|
|
|
}
|
|
|
|
}
|