2021-07-18 11:12:17 -05:00
|
|
|
use rustc_data_structures::sync::Lock;
|
2020-01-04 19:37:57 -06:00
|
|
|
use rustc_hir as hir;
|
2022-11-26 11:22:10 -06:00
|
|
|
use rustc_hir::def_id::LocalDefId;
|
2020-01-07 11:12:06 -06:00
|
|
|
use rustc_hir::intravisit;
|
2020-02-09 10:11:02 -06:00
|
|
|
use rustc_hir::{HirId, ItemLocalId};
|
2022-07-03 06:35:48 -05:00
|
|
|
use rustc_index::bit_set::GrowableBitSet;
|
2021-11-03 18:03:12 -05:00
|
|
|
use rustc_middle::hir::nested_filter;
|
2023-02-22 09:51:17 -06:00
|
|
|
use rustc_middle::ty::TyCtxt;
|
2017-03-14 09:50:40 -05:00
|
|
|
|
2020-02-09 10:11:02 -06:00
|
|
|
pub fn check_crate(tcx: TyCtxt<'_>) {
|
|
|
|
tcx.dep_graph.assert_ignored();
|
2020-02-07 22:18:34 -06:00
|
|
|
|
2022-07-06 07:44:47 -05:00
|
|
|
if tcx.sess.opts.unstable_opts.hir_stats {
|
2021-08-26 13:27:06 -05:00
|
|
|
crate::hir_stats::print_hir_stats(tcx);
|
|
|
|
}
|
|
|
|
|
2022-07-04 04:38:11 -05:00
|
|
|
#[cfg(debug_assertions)]
|
|
|
|
{
|
|
|
|
let errors = Lock::new(Vec::new());
|
|
|
|
|
2022-11-26 11:22:10 -06:00
|
|
|
tcx.hir().par_for_each_module(|module_id| {
|
2022-07-04 04:38:11 -05:00
|
|
|
let mut v = HirIdValidator {
|
2022-11-26 11:22:10 -06:00
|
|
|
tcx,
|
2022-07-04 04:38:11 -05:00
|
|
|
owner: None,
|
|
|
|
hir_ids_seen: Default::default(),
|
|
|
|
errors: &errors,
|
|
|
|
};
|
|
|
|
|
2022-07-03 08:28:57 -05:00
|
|
|
tcx.hir().visit_item_likes_in_module(module_id, &mut v);
|
2022-07-04 04:38:11 -05: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 10:11:02 -06:00
|
|
|
}
|
2017-03-14 09:50:40 -05:00
|
|
|
}
|
2020-02-09 10:11:02 -06:00
|
|
|
|
2019-06-14 11:39:39 -05:00
|
|
|
struct HirIdValidator<'a, 'hir> {
|
2022-11-26 11:22:10 -06:00
|
|
|
tcx: TyCtxt<'hir>,
|
2022-09-20 00:11:23 -05:00
|
|
|
owner: Option<hir::OwnerId>,
|
2022-07-03 06:35:48 -05:00
|
|
|
hir_ids_seen: GrowableBitSet<ItemLocalId>,
|
2018-06-06 15:13:52 -05:00
|
|
|
errors: &'a Lock<Vec<String>>,
|
2017-03-14 09:50:40 -05:00
|
|
|
}
|
|
|
|
|
2022-05-04 12:16:33 -05:00
|
|
|
impl<'a, 'hir> HirIdValidator<'a, 'hir> {
|
2022-11-26 11:22:10 -06:00
|
|
|
fn new_visitor(&self, tcx: TyCtxt<'hir>) -> HirIdValidator<'a, 'hir> {
|
|
|
|
HirIdValidator { tcx, owner: None, hir_ids_seen: Default::default(), errors: self.errors }
|
2017-03-14 09:50:40 -05:00
|
|
|
}
|
|
|
|
|
2018-06-06 15:13:52 -05:00
|
|
|
#[cold]
|
|
|
|
#[inline(never)]
|
|
|
|
fn error(&self, f: impl FnOnce() -> String) {
|
|
|
|
self.errors.lock().push(f());
|
|
|
|
}
|
2017-03-14 09:50:40 -05:00
|
|
|
|
2022-09-20 00:11:23 -05:00
|
|
|
fn check<F: FnOnce(&mut HirIdValidator<'a, 'hir>)>(&mut self, owner: hir::OwnerId, walk: F) {
|
2020-03-18 13:27:59 -05:00
|
|
|
assert!(self.owner.is_none());
|
|
|
|
self.owner = Some(owner);
|
2017-03-14 09:50:40 -05:00
|
|
|
walk(self);
|
|
|
|
|
2022-09-20 00:11:23 -05:00
|
|
|
if owner == hir::CRATE_OWNER_ID {
|
2018-10-11 03:15:18 -05:00
|
|
|
return;
|
2017-03-14 09:50:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// There's always at least one entry for the owning item itself
|
|
|
|
let max = self
|
|
|
|
.hir_ids_seen
|
2019-02-15 03:46:28 -06:00
|
|
|
.iter()
|
2017-03-14 09:50:40 -05:00
|
|
|
.map(|local_id| local_id.as_usize())
|
|
|
|
.max()
|
2018-05-22 07:31:56 -05:00
|
|
|
.expect("owning item has no entry");
|
2017-03-14 09:50:40 -05:00
|
|
|
|
|
|
|
if max != self.hir_ids_seen.len() - 1 {
|
2023-01-31 05:09:54 -06:00
|
|
|
let hir = self.tcx.hir();
|
|
|
|
let pretty_owner = hir.def_path(owner.def_id).to_string_no_crate_verbose();
|
2017-03-14 09:50:40 -05:00
|
|
|
|
2023-01-31 05:09:54 -06:00
|
|
|
let missing_items: Vec<_> = (0..=max as u32)
|
|
|
|
.map(|i| ItemLocalId::from_u32(i))
|
|
|
|
.filter(|&local_id| !self.hir_ids_seen.contains(local_id))
|
|
|
|
.map(|local_id| hir.node_to_string(HirId { owner, local_id }))
|
|
|
|
.collect();
|
2017-03-14 09:50:40 -05:00
|
|
|
|
2023-01-31 05:09:54 -06:00
|
|
|
let seen_items: Vec<_> = self
|
|
|
|
.hir_ids_seen
|
|
|
|
.iter()
|
|
|
|
.map(|local_id| hir.node_to_string(HirId { owner, local_id }))
|
|
|
|
.collect();
|
2018-05-22 07:31:56 -05:00
|
|
|
|
2018-06-06 15:13:52 -05:00
|
|
|
self.error(|| {
|
|
|
|
format!(
|
2017-03-14 09:50:40 -05:00
|
|
|
"ItemLocalIds not assigned densely in {}. \
|
2023-01-31 05:09:54 -06:00
|
|
|
Max ItemLocalId = {}, missing IDs = {:#?}; seen IDs = {:#?}",
|
|
|
|
pretty_owner, max, missing_items, seen_items
|
2019-12-22 16:42:04 -06:00
|
|
|
)
|
2018-10-11 03:15:18 -05:00
|
|
|
});
|
2017-03-14 09:50:40 -05:00
|
|
|
}
|
|
|
|
}
|
2022-11-26 11:22:10 -06:00
|
|
|
|
|
|
|
fn check_nested_id(&mut self, id: LocalDefId) {
|
|
|
|
let Some(owner) = self.owner else { return };
|
|
|
|
let def_parent = self.tcx.local_parent(id);
|
|
|
|
let def_parent_hir_id = self.tcx.local_def_id_to_hir_id(def_parent);
|
|
|
|
if def_parent_hir_id.owner != owner {
|
|
|
|
self.error(|| {
|
|
|
|
format!(
|
|
|
|
"inconsistent Def parent at `{:?}` for `{:?}`:\nexpected={:?}\nfound={:?}",
|
|
|
|
self.tcx.def_span(id),
|
|
|
|
id,
|
|
|
|
owner,
|
|
|
|
def_parent_hir_id
|
|
|
|
)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2017-03-14 09:50:40 -05:00
|
|
|
}
|
|
|
|
|
2019-06-14 11:39:39 -05: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 10:25:33 -06:00
|
|
|
|
2021-11-03 18:03:12 -05:00
|
|
|
fn nested_visit_map(&mut self) -> Self::Map {
|
2022-11-26 11:22:10 -06:00
|
|
|
self.tcx.hir()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_nested_item(&mut self, id: hir::ItemId) {
|
|
|
|
self.check_nested_id(id.owner_id.def_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_nested_trait_item(&mut self, id: hir::TraitItemId) {
|
|
|
|
self.check_nested_id(id.owner_id.def_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_nested_impl_item(&mut self, id: hir::ImplItemId) {
|
|
|
|
self.check_nested_id(id.owner_id.def_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_nested_foreign_item(&mut self, id: hir::ForeignItemId) {
|
|
|
|
self.check_nested_id(id.owner_id.def_id);
|
2017-03-14 09:50:40 -05:00
|
|
|
}
|
|
|
|
|
2022-05-04 12:16:33 -05:00
|
|
|
fn visit_item(&mut self, i: &'hir hir::Item<'hir>) {
|
2022-11-26 11:22:10 -06:00
|
|
|
let mut inner_visitor = self.new_visitor(self.tcx);
|
2022-10-26 22:02:18 -05:00
|
|
|
inner_visitor.check(i.owner_id, |this| intravisit::walk_item(this, i));
|
2022-05-04 12:16:33 -05:00
|
|
|
}
|
|
|
|
|
2019-02-06 07:16:11 -06:00
|
|
|
fn visit_id(&mut self, hir_id: HirId) {
|
2020-03-18 13:27:59 -05:00
|
|
|
let owner = self.owner.expect("no owner");
|
2017-03-14 09:50:40 -05:00
|
|
|
|
2019-02-06 07:16:11 -06:00
|
|
|
if owner != hir_id.owner {
|
2018-06-06 15:13:52 -05:00
|
|
|
self.error(|| {
|
|
|
|
format!(
|
2017-03-14 09:50:40 -05:00
|
|
|
"HirIdValidator: The recorded owner of {} is {} instead of {}",
|
2022-11-26 11:22:10 -06:00
|
|
|
self.tcx.hir().node_to_string(hir_id),
|
|
|
|
self.tcx.hir().def_path(hir_id.owner.def_id).to_string_no_crate_verbose(),
|
|
|
|
self.tcx.hir().def_path(owner.def_id).to_string_no_crate_verbose()
|
2019-12-22 16:42:04 -06:00
|
|
|
)
|
2017-03-14 09:50:40 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-15 03:46:28 -06:00
|
|
|
self.hir_ids_seen.insert(hir_id.local_id);
|
2017-03-14 09:50:40 -05:00
|
|
|
}
|
|
|
|
|
2022-05-04 12:16:33 -05:00
|
|
|
fn visit_foreign_item(&mut self, i: &'hir hir::ForeignItem<'hir>) {
|
2022-11-26 11:22:10 -06:00
|
|
|
let mut inner_visitor = self.new_visitor(self.tcx);
|
2022-10-26 22:02:18 -05:00
|
|
|
inner_visitor.check(i.owner_id, |this| intravisit::walk_foreign_item(this, i));
|
2022-05-04 12:16:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_trait_item(&mut self, i: &'hir hir::TraitItem<'hir>) {
|
2022-11-26 11:22:10 -06:00
|
|
|
let mut inner_visitor = self.new_visitor(self.tcx);
|
2022-10-26 22:02:18 -05:00
|
|
|
inner_visitor.check(i.owner_id, |this| intravisit::walk_trait_item(this, i));
|
2022-05-04 12:16:33 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_impl_item(&mut self, i: &'hir hir::ImplItem<'hir>) {
|
2022-11-26 11:22:10 -06:00
|
|
|
let mut inner_visitor = self.new_visitor(self.tcx);
|
2022-10-26 22:02:18 -05:00
|
|
|
inner_visitor.check(i.owner_id, |this| intravisit::walk_impl_item(this, i));
|
2017-03-14 09:50:40 -05:00
|
|
|
}
|
|
|
|
}
|