2021-07-18 18:12:17 +02:00
|
|
|
use rustc_data_structures::sync::Lock;
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2022-04-15 19:27:53 +02:00
|
|
|
use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID};
|
2020-01-07 18:12:06 +01:00
|
|
|
use rustc_hir::intravisit;
|
2020-02-09 17:11:02 +01:00
|
|
|
use rustc_hir::{HirId, ItemLocalId};
|
2022-07-03 13:35:48 +02:00
|
|
|
use rustc_index::bit_set::GrowableBitSet;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::hir::map::Map;
|
2021-11-03 18:03:12 -05:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
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
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
if tcx.sess.opts.unstable_opts.hir_stats {
|
2021-08-26 20:27:06 +02:00
|
|
|
crate::hir_stats::print_hir_stats(tcx);
|
|
|
|
}
|
|
|
|
|
2022-07-04 11:38:11 +02:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
let errors = Lock::new(Vec::new());
|
|
|
|
let hir_map = tcx.hir();
|
|
|
|
|
|
|
|
hir_map.par_for_each_module(|module_id| {
|
|
|
|
let mut v = HirIdValidator {
|
|
|
|
hir_map,
|
|
|
|
owner: None,
|
|
|
|
hir_ids_seen: Default::default(),
|
|
|
|
errors: &errors,
|
|
|
|
};
|
|
|
|
|
2022-07-03 15:28:57 +02:00
|
|
|
tcx.hir().visit_item_likes_in_module(module_id, &mut v);
|
2022-07-04 11:38:11 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
let errors = errors.into_inner();
|
|
|
|
|
|
|
|
if !errors.is_empty() {
|
|
|
|
let message = errors.iter().fold(String::new(), |s1, s2| s1 + "\n" + s2);
|
|
|
|
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>,
|
2022-07-03 13:35:48 +02:00
|
|
|
hir_ids_seen: GrowableBitSet<ItemLocalId>,
|
2018-06-06 22:13:52 +02:00
|
|
|
errors: &'a Lock<Vec<String>>,
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
2022-05-04 13:16:33 -04:00
|
|
|
impl<'a, 'hir> HirIdValidator<'a, 'hir> {
|
|
|
|
fn new_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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-12-05 13:08:52 +01:00
|
|
|
fn check<F: FnOnce(&mut HirIdValidator<'a, 'hir>)>(&mut self, owner: LocalDefId, walk: F) {
|
2020-03-18 20:27:59 +02:00
|
|
|
assert!(self.owner.is_none());
|
|
|
|
self.owner = Some(owner);
|
2017-03-14 15:50:40 +01:00
|
|
|
walk(self);
|
|
|
|
|
2022-04-15 19:27:53 +02:00
|
|
|
if owner == CRATE_DEF_ID {
|
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)
|
2022-07-03 13:35:48 +02:00
|
|
|
.filter(|&i| !self.hir_ids_seen.contains(ItemLocalId::from_u32(i)))
|
2019-02-15 15:21:56 +01:00
|
|
|
.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);
|
|
|
|
|
2017-03-14 15:50:40 +01:00
|
|
|
missing_items.push(format!(
|
2020-02-20 19:24:44 +01:00
|
|
|
"[local_id: {}, owner: {}]",
|
2017-03-14 15:50:40 +01:00
|
|
|
local_id,
|
2020-09-23 23:38:38 +01:00
|
|
|
self.hir_map.def_path(owner).to_string_no_crate_verbose()
|
2019-06-16 17:30:02 +02:00
|
|
|
));
|
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 {}. \
|
2022-09-06 17:26:05 -03:00
|
|
|
Max ItemLocalId = {}, missing IDs = {:#?}; seens IDs = {:#?}",
|
2020-09-23 23:38:38 +01:00
|
|
|
self.hir_map.def_path(owner).to_string_no_crate_verbose(),
|
2017-03-14 15:50:40 +01:00
|
|
|
max,
|
2018-10-11 21:15:18 +13:00
|
|
|
missing_items,
|
|
|
|
self.hir_ids_seen
|
2019-02-15 10:46:28 +01:00
|
|
|
.iter()
|
2022-07-03 13:35:48 +02:00
|
|
|
.map(|local_id| HirId { owner, 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<_>>()
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
2018-10-11 21:15:18 +13:00
|
|
|
});
|
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> {
|
2021-11-03 18:03:12 -05:00
|
|
|
type NestedFilter = nested_filter::OnlyBodies;
|
2020-01-07 17:25:33 +01:00
|
|
|
|
2021-11-03 18:03:12 -05:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
|
|
|
self.hir_map
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
|
2022-05-04 13:16:33 -04:00
|
|
|
fn visit_item(&mut self, i: &'hir hir::Item<'hir>) {
|
|
|
|
let mut inner_visitor = self.new_visitor(self.hir_map);
|
|
|
|
inner_visitor.check(i.def_id, |this| intravisit::walk_item(this, i));
|
|
|
|
}
|
|
|
|
|
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 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),
|
2020-09-23 23:38:38 +01:00
|
|
|
self.hir_map.def_path(hir_id.owner).to_string_no_crate_verbose(),
|
|
|
|
self.hir_map.def_path(owner).to_string_no_crate_verbose()
|
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
|
|
|
}
|
|
|
|
|
2022-05-04 13:16:33 -04:00
|
|
|
fn visit_foreign_item(&mut self, i: &'hir hir::ForeignItem<'hir>) {
|
|
|
|
let mut inner_visitor = self.new_visitor(self.hir_map);
|
|
|
|
inner_visitor.check(i.def_id, |this| intravisit::walk_foreign_item(this, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, i: &'hir hir::TraitItem<'hir>) {
|
|
|
|
let mut inner_visitor = self.new_visitor(self.hir_map);
|
|
|
|
inner_visitor.check(i.def_id, |this| intravisit::walk_trait_item(this, i));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_impl_item(&mut self, i: &'hir hir::ImplItem<'hir>) {
|
|
|
|
let mut inner_visitor = self.new_visitor(self.hir_map);
|
|
|
|
inner_visitor.check(i.def_id, |this| intravisit::walk_impl_item(this, i));
|
2017-03-14 15:50:40 +01:00
|
|
|
}
|
|
|
|
}
|