2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::hir::map::Map;
|
|
|
|
use rustc_middle::ty::TyCtxt;
|
2020-02-09 17:11:02 +01:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2019-12-22 17:42:04 -05:00
|
|
|
use rustc_data_structures::sync::{par_iter, Lock, ParallelIterator};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2020-03-18 20:27:59 +02:00
|
|
|
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_INDEX};
|
2020-01-07 18:12:06 +01:00
|
|
|
use rustc_hir::intravisit;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::itemlikevisit::ItemLikeVisitor;
|
2020-02-09 17:11:02 +01:00
|
|
|
use rustc_hir::{HirId, ItemLocalId};
|
2017-03-14 15:50:40 +01:00
|
|
|
|
2020-02-09 17:11:02 +01:00
|
|
|
pub fn check_crate(tcx: TyCtxt<'_>) {
|
|
|
|
tcx.dep_graph.assert_ignored();
|
2020-02-08 05:18:34 +01:00
|
|
|
|
2018-06-06 22:13:52 +02:00
|
|
|
let errors = Lock::new(Vec::new());
|
2020-02-09 17:11:02 +01:00
|
|
|
let hir_map = tcx.hir();
|
2018-06-06 22:13:52 +02:00
|
|
|
|
2020-02-09 17:11:02 +01:00
|
|
|
par_iter(&hir_map.krate().modules).for_each(|(module_id, _)| {
|
2019-07-10 12:22:07 +02:00
|
|
|
let local_def_id = hir_map.local_def_id(*module_id);
|
2019-12-22 17:42:04 -05:00
|
|
|
hir_map.visit_item_likes_in_module(
|
|
|
|
local_def_id,
|
|
|
|
&mut OuterVisitor { hir_map, errors: &errors },
|
|
|
|
);
|
2018-06-06 22:13:52 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
let errors = errors.into_inner();
|
|
|
|
|
|
|
|
if !errors.is_empty() {
|
2019-12-22 17:42:04 -05:00
|
|
|
let message = errors.iter().fold(String::new(), |s1, s2| s1 + "\n" + s2);
|
2020-02-09 15:32:00 +01:00
|
|
|
tcx.sess.delay_span_bug(rustc_span::DUMMY_SP, &message);
|
2020-02-09 17:11:02 +01:00
|
|
|
}
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2020-02-09 17:11:02 +01:00
|
|
|
|
2019-06-14 19:39:39 +03:00
|
|
|
struct HirIdValidator<'a, 'hir> {
|
2020-02-09 17:11:02 +01:00
|
|
|
hir_map: Map<'hir>,
|
2020-03-18 20:27:59 +02:00
|
|
|
owner: Option<LocalDefId>,
|
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> {
|
2020-02-09 17:11:02 +01:00
|
|
|
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> {
|
2020-02-09 17:11:02 +01:00
|
|
|
fn new_inner_visitor(&self, hir_map: Map<'hir>) -> HirIdValidator<'a, 'hir> {
|
2017-03-14 15:50:40 +01:00
|
|
|
HirIdValidator {
|
2017-07-03 11:19:51 -07:00
|
|
|
hir_map,
|
2020-03-18 20:27:59 +02:00
|
|
|
owner: 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> {
|
2019-11-28 19:28:50 +01:00
|
|
|
fn visit_item(&mut self, i: &'hir hir::Item<'hir>) {
|
2017-03-14 15:50:40 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-28 21:47:10 +01:00
|
|
|
fn visit_trait_item(&mut self, i: &'hir hir::TraitItem<'hir>) {
|
2017-03-14 15:50:40 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-28 22:16:44 +01:00
|
|
|
fn visit_impl_item(&mut self, i: &'hir hir::ImplItem<'hir>) {
|
2017-03-14 15:50:40 +01:00
|
|
|
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
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
fn check<F: FnOnce(&mut HirIdValidator<'a, 'hir>)>(&mut self, hir_id: HirId, walk: F) {
|
2020-03-18 20:27:59 +02:00
|
|
|
assert!(self.owner.is_none());
|
|
|
|
let owner = self.hir_map.local_def_id(hir_id).expect_local();
|
|
|
|
self.owner = Some(owner);
|
2017-03-14 15:50:40 +01:00
|
|
|
walk(self);
|
|
|
|
|
2020-03-18 20:27:59 +02:00
|
|
|
if owner.local_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
|
2019-12-22 17:42:04 -05:00
|
|
|
let max = self
|
|
|
|
.hir_ids_seen
|
|
|
|
.iter()
|
|
|
|
.map(|local_id| local_id.as_usize())
|
|
|
|
.max()
|
|
|
|
.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
|
2019-12-22 17:42:04 -05:00
|
|
|
let missing: Vec<_> = (0..=max as u32)
|
|
|
|
.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 {
|
2020-03-18 20:27:59 +02:00
|
|
|
let hir_id = HirId { owner, 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);
|
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
missing_items.push(format!(
|
2020-02-20 19:24:44 +01:00
|
|
|
"[local_id: {}, owner: {}]",
|
2019-12-22 17:42:04 -05:00
|
|
|
local_id,
|
2020-03-18 20:27:59 +02:00
|
|
|
self.hir_map.def_path(owner).to_string_no_crate()
|
2019-12-22 17:42:04 -05:00
|
|
|
));
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
self.error(|| {
|
|
|
|
format!(
|
|
|
|
"ItemLocalIds not assigned densely in {}. \
|
2018-10-11 21:15:18 +13:00
|
|
|
Max ItemLocalId = {}, missing IDs = {:?}; seens IDs = {:?}",
|
2020-03-18 20:27:59 +02:00
|
|
|
self.hir_map.def_path(owner).to_string_no_crate(),
|
2019-12-22 17:42:04 -05:00
|
|
|
max,
|
|
|
|
missing_items,
|
|
|
|
self.hir_ids_seen
|
|
|
|
.iter()
|
2020-03-18 20:27:59 +02:00
|
|
|
.map(|&local_id| HirId { owner, local_id })
|
2019-12-22 17:42:04 -05:00
|
|
|
.map(|h| format!("({:?} {})", h, self.hir_map.node_to_string(h)))
|
|
|
|
.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> {
|
2020-02-09 17:11:02 +01:00
|
|
|
type Map = Map<'hir>;
|
2020-01-07 17:25:33 +01:00
|
|
|
|
2020-02-09 15:32:00 +01:00
|
|
|
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
|
2017-03-14 15:50:40 +01:00
|
|
|
intravisit::NestedVisitorMap::OnlyBodies(self.hir_map)
|
|
|
|
}
|
|
|
|
|
2019-02-06 14:16:11 +01:00
|
|
|
fn visit_id(&mut self, hir_id: HirId) {
|
2020-03-18 20:27:59 +02:00
|
|
|
let owner = self.owner.expect("no owner");
|
2017-03-14 15:50:40 +01:00
|
|
|
|
2019-02-06 14:16:11 +01:00
|
|
|
if hir_id == hir::DUMMY_HIR_ID {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.error(|| {
|
|
|
|
format!(
|
|
|
|
"HirIdValidator: HirId {:?} is invalid",
|
|
|
|
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 {
|
2019-12-22 17:42:04 -05:00
|
|
|
self.error(|| {
|
|
|
|
format!(
|
|
|
|
"HirIdValidator: The recorded owner of {} is {} instead of {}",
|
|
|
|
self.hir_map.node_to_string(hir_id),
|
2020-03-18 20:27:59 +02:00
|
|
|
self.hir_map.def_path(hir_id.owner).to_string_no_crate(),
|
|
|
|
self.hir_map.def_path(owner).to_string_no_crate()
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
|
|
|
});
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-30 17:46:46 +01:00
|
|
|
fn visit_impl_item_ref(&mut self, _: &'hir hir::ImplItemRef<'hir>) {
|
2017-03-14 15:50:40 +01:00
|
|
|
// 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.
|
|
|
|
}
|
|
|
|
}
|