From 2a57e5efed5b84a2eaedf5240df4b8d80f012409 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Sun, 3 Jul 2022 13:35:48 +0200 Subject: [PATCH 1/2] Use a bitset instead of a hash map in HIR ID validator --- compiler/rustc_index/src/bit_set.rs | 10 ++++++++++ compiler/rustc_passes/src/hir_id_validator.rs | 8 ++++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/compiler/rustc_index/src/bit_set.rs b/compiler/rustc_index/src/bit_set.rs index 976874c7cee83..5b664e19c18c0 100644 --- a/compiler/rustc_index/src/bit_set.rs +++ b/compiler/rustc_index/src/bit_set.rs @@ -1546,6 +1546,16 @@ impl GrowableBitSet { let (word_index, mask) = word_index_and_mask(elem); self.bit_set.words.get(word_index).map_or(false, |word| (word & mask) != 0) } + + #[inline] + pub fn iter(&self) -> BitIter<'_, T> { + self.bit_set.iter() + } + + #[inline] + pub fn len(&self) -> usize { + self.bit_set.count() + } } impl From> for GrowableBitSet { diff --git a/compiler/rustc_passes/src/hir_id_validator.rs b/compiler/rustc_passes/src/hir_id_validator.rs index 23ff0a9115970..b268cee96db93 100644 --- a/compiler/rustc_passes/src/hir_id_validator.rs +++ b/compiler/rustc_passes/src/hir_id_validator.rs @@ -1,9 +1,9 @@ -use rustc_data_structures::fx::FxHashSet; use rustc_data_structures::sync::Lock; use rustc_hir as hir; use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID}; use rustc_hir::intravisit; use rustc_hir::{HirId, ItemLocalId}; +use rustc_index::bit_set::GrowableBitSet; use rustc_middle::hir::map::Map; use rustc_middle::hir::nested_filter; use rustc_middle::ty::TyCtxt; @@ -40,7 +40,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) { struct HirIdValidator<'a, 'hir> { hir_map: Map<'hir>, owner: Option, - hir_ids_seen: FxHashSet, + hir_ids_seen: GrowableBitSet, errors: &'a Lock>, } @@ -80,7 +80,7 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> { if max != self.hir_ids_seen.len() - 1 { // Collect the missing ItemLocalIds let missing: Vec<_> = (0..=max as u32) - .filter(|&i| !self.hir_ids_seen.contains(&ItemLocalId::from_u32(i))) + .filter(|&i| !self.hir_ids_seen.contains(ItemLocalId::from_u32(i))) .collect(); // Try to map those to something more useful @@ -106,7 +106,7 @@ impl<'a, 'hir> HirIdValidator<'a, 'hir> { missing_items, self.hir_ids_seen .iter() - .map(|&local_id| HirId { owner, local_id }) + .map(|local_id| HirId { owner, local_id }) .map(|h| format!("({:?} {})", h, self.hir_map.node_to_string(h))) .collect::>() ) From 928c17203a4d6ca9c119787c7ebfd3d2a487eeab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Mon, 4 Jul 2022 11:38:11 +0200 Subject: [PATCH 2/2] Only validate HIR with `debug_assertions` on --- compiler/rustc_passes/src/hir_id_validator.rs | 41 ++++++++++--------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/compiler/rustc_passes/src/hir_id_validator.rs b/compiler/rustc_passes/src/hir_id_validator.rs index b268cee96db93..550c062f4de0a 100644 --- a/compiler/rustc_passes/src/hir_id_validator.rs +++ b/compiler/rustc_passes/src/hir_id_validator.rs @@ -15,25 +15,28 @@ pub fn check_crate(tcx: TyCtxt<'_>) { crate::hir_stats::print_hir_stats(tcx); } - 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, - }; - - tcx.hir().deep_visit_item_likes_in_module(module_id, &mut v); - }); - - 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); + #[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, + }; + + tcx.hir().deep_visit_item_likes_in_module(module_id, &mut v); + }); + + 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); + } } }