]>
Commit | Line | Data |
---|---|---|
c295e0f8 | 1 | use rustc_data_structures::sync::Lock; |
dfeec247 | 2 | use rustc_hir as hir; |
487cf647 | 3 | use rustc_hir::def_id::LocalDefId; |
dfeec247 | 4 | use rustc_hir::intravisit; |
dfeec247 | 5 | use rustc_hir::{HirId, ItemLocalId}; |
064997fb | 6 | use rustc_index::bit_set::GrowableBitSet; |
5099ac24 | 7 | use rustc_middle::hir::nested_filter; |
487cf647 | 8 | use rustc_middle::ty::{DefIdTree, TyCtxt}; |
cc61c64b | 9 | |
ba9703b0 XL |
10 | pub fn check_crate(tcx: TyCtxt<'_>) { |
11 | tcx.dep_graph.assert_ignored(); | |
2c00a5a8 | 12 | |
064997fb | 13 | if tcx.sess.opts.unstable_opts.hir_stats { |
c295e0f8 XL |
14 | crate::hir_stats::print_hir_stats(tcx); |
15 | } | |
16 | ||
064997fb FG |
17 | #[cfg(debug_assertions)] |
18 | { | |
19 | let errors = Lock::new(Vec::new()); | |
0731742a | 20 | |
487cf647 | 21 | tcx.hir().par_for_each_module(|module_id| { |
064997fb | 22 | let mut v = HirIdValidator { |
487cf647 | 23 | tcx, |
064997fb FG |
24 | owner: None, |
25 | hir_ids_seen: Default::default(), | |
26 | errors: &errors, | |
27 | }; | |
923072b8 | 28 | |
064997fb FG |
29 | tcx.hir().visit_item_likes_in_module(module_id, &mut v); |
30 | }); | |
0731742a | 31 | |
064997fb | 32 | let errors = errors.into_inner(); |
0731742a | 33 | |
064997fb FG |
34 | if !errors.is_empty() { |
35 | let message = errors.iter().fold(String::new(), |s1, s2| s1 + "\n" + s2); | |
36 | tcx.sess.delay_span_bug(rustc_span::DUMMY_SP, &message); | |
37 | } | |
2c00a5a8 | 38 | } |
cc61c64b XL |
39 | } |
40 | ||
dc9dc135 | 41 | struct HirIdValidator<'a, 'hir> { |
487cf647 | 42 | tcx: TyCtxt<'hir>, |
2b03887a | 43 | owner: Option<hir::OwnerId>, |
064997fb | 44 | hir_ids_seen: GrowableBitSet<ItemLocalId>, |
0731742a | 45 | errors: &'a Lock<Vec<String>>, |
cc61c64b XL |
46 | } |
47 | ||
923072b8 | 48 | impl<'a, 'hir> HirIdValidator<'a, 'hir> { |
487cf647 FG |
49 | fn new_visitor(&self, tcx: TyCtxt<'hir>) -> HirIdValidator<'a, 'hir> { |
50 | HirIdValidator { tcx, owner: None, hir_ids_seen: Default::default(), errors: self.errors } | |
cc61c64b | 51 | } |
cc61c64b | 52 | |
0731742a XL |
53 | #[cold] |
54 | #[inline(never)] | |
55 | fn error(&self, f: impl FnOnce() -> String) { | |
56 | self.errors.lock().push(f()); | |
57 | } | |
cc61c64b | 58 | |
2b03887a | 59 | fn check<F: FnOnce(&mut HirIdValidator<'a, 'hir>)>(&mut self, owner: hir::OwnerId, walk: F) { |
ba9703b0 | 60 | assert!(self.owner.is_none()); |
ba9703b0 | 61 | self.owner = Some(owner); |
cc61c64b XL |
62 | walk(self); |
63 | ||
2b03887a | 64 | if owner == hir::CRATE_OWNER_ID { |
13cf67c4 | 65 | return; |
cc61c64b XL |
66 | } |
67 | ||
68 | // There's always at least one entry for the owning item itself | |
60c5eb7d XL |
69 | let max = self |
70 | .hir_ids_seen | |
71 | .iter() | |
72 | .map(|local_id| local_id.as_usize()) | |
73 | .max() | |
74 | .expect("owning item has no entry"); | |
cc61c64b XL |
75 | |
76 | if max != self.hir_ids_seen.len() - 1 { | |
77 | // Collect the missing ItemLocalIds | |
60c5eb7d | 78 | let missing: Vec<_> = (0..=max as u32) |
064997fb | 79 | .filter(|&i| !self.hir_ids_seen.contains(ItemLocalId::from_u32(i))) |
60c5eb7d | 80 | .collect(); |
cc61c64b XL |
81 | |
82 | // Try to map those to something more useful | |
8faf50e0 | 83 | let mut missing_items = Vec::with_capacity(missing.len()); |
cc61c64b XL |
84 | |
85 | for local_id in missing { | |
ba9703b0 | 86 | let hir_id = HirId { owner, local_id: ItemLocalId::from_u32(local_id) }; |
cc61c64b | 87 | |
94b46f34 XL |
88 | trace!("missing hir id {:#?}", hir_id); |
89 | ||
60c5eb7d | 90 | missing_items.push(format!( |
ba9703b0 | 91 | "[local_id: {}, owner: {}]", |
60c5eb7d | 92 | local_id, |
487cf647 | 93 | self.tcx.hir().def_path(owner.def_id).to_string_no_crate_verbose() |
60c5eb7d | 94 | )); |
cc61c64b | 95 | } |
60c5eb7d XL |
96 | self.error(|| { |
97 | format!( | |
98 | "ItemLocalIds not assigned densely in {}. \ | |
f2b60f7d | 99 | Max ItemLocalId = {}, missing IDs = {:#?}; seens IDs = {:#?}", |
487cf647 | 100 | self.tcx.hir().def_path(owner.def_id).to_string_no_crate_verbose(), |
60c5eb7d XL |
101 | max, |
102 | missing_items, | |
103 | self.hir_ids_seen | |
104 | .iter() | |
064997fb | 105 | .map(|local_id| HirId { owner, local_id }) |
487cf647 | 106 | .map(|h| format!("({:?} {})", h, self.tcx.hir().node_to_string(h))) |
60c5eb7d XL |
107 | .collect::<Vec<_>>() |
108 | ) | |
109 | }); | |
cc61c64b XL |
110 | } |
111 | } | |
487cf647 FG |
112 | |
113 | fn check_nested_id(&mut self, id: LocalDefId) { | |
114 | let Some(owner) = self.owner else { return }; | |
115 | let def_parent = self.tcx.local_parent(id); | |
116 | let def_parent_hir_id = self.tcx.local_def_id_to_hir_id(def_parent); | |
117 | if def_parent_hir_id.owner != owner { | |
118 | self.error(|| { | |
119 | format!( | |
120 | "inconsistent Def parent at `{:?}` for `{:?}`:\nexpected={:?}\nfound={:?}", | |
121 | self.tcx.def_span(id), | |
122 | id, | |
123 | owner, | |
124 | def_parent_hir_id | |
125 | ) | |
126 | }); | |
127 | } | |
128 | } | |
cc61c64b XL |
129 | } |
130 | ||
dc9dc135 | 131 | impl<'a, 'hir> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> { |
5099ac24 | 132 | type NestedFilter = nested_filter::OnlyBodies; |
dfeec247 | 133 | |
5099ac24 | 134 | fn nested_visit_map(&mut self) -> Self::Map { |
487cf647 FG |
135 | self.tcx.hir() |
136 | } | |
137 | ||
138 | fn visit_nested_item(&mut self, id: hir::ItemId) { | |
139 | self.check_nested_id(id.owner_id.def_id); | |
140 | } | |
141 | ||
142 | fn visit_nested_trait_item(&mut self, id: hir::TraitItemId) { | |
143 | self.check_nested_id(id.owner_id.def_id); | |
144 | } | |
145 | ||
146 | fn visit_nested_impl_item(&mut self, id: hir::ImplItemId) { | |
147 | self.check_nested_id(id.owner_id.def_id); | |
148 | } | |
149 | ||
150 | fn visit_nested_foreign_item(&mut self, id: hir::ForeignItemId) { | |
151 | self.check_nested_id(id.owner_id.def_id); | |
cc61c64b XL |
152 | } |
153 | ||
923072b8 | 154 | fn visit_item(&mut self, i: &'hir hir::Item<'hir>) { |
487cf647 | 155 | let mut inner_visitor = self.new_visitor(self.tcx); |
2b03887a | 156 | inner_visitor.check(i.owner_id, |this| intravisit::walk_item(this, i)); |
923072b8 FG |
157 | } |
158 | ||
9fa01778 | 159 | fn visit_id(&mut self, hir_id: HirId) { |
ba9703b0 | 160 | let owner = self.owner.expect("no owner"); |
cc61c64b | 161 | |
9fa01778 | 162 | if owner != hir_id.owner { |
60c5eb7d XL |
163 | self.error(|| { |
164 | format!( | |
165 | "HirIdValidator: The recorded owner of {} is {} instead of {}", | |
487cf647 FG |
166 | self.tcx.hir().node_to_string(hir_id), |
167 | self.tcx.hir().def_path(hir_id.owner.def_id).to_string_no_crate_verbose(), | |
168 | self.tcx.hir().def_path(owner.def_id).to_string_no_crate_verbose() | |
60c5eb7d XL |
169 | ) |
170 | }); | |
cc61c64b XL |
171 | } |
172 | ||
9fa01778 | 173 | self.hir_ids_seen.insert(hir_id.local_id); |
cc61c64b XL |
174 | } |
175 | ||
923072b8 | 176 | fn visit_foreign_item(&mut self, i: &'hir hir::ForeignItem<'hir>) { |
487cf647 | 177 | let mut inner_visitor = self.new_visitor(self.tcx); |
2b03887a | 178 | inner_visitor.check(i.owner_id, |this| intravisit::walk_foreign_item(this, i)); |
923072b8 FG |
179 | } |
180 | ||
181 | fn visit_trait_item(&mut self, i: &'hir hir::TraitItem<'hir>) { | |
487cf647 | 182 | let mut inner_visitor = self.new_visitor(self.tcx); |
2b03887a | 183 | inner_visitor.check(i.owner_id, |this| intravisit::walk_trait_item(this, i)); |
cc61c64b | 184 | } |
29967ef6 | 185 | |
923072b8 | 186 | fn visit_impl_item(&mut self, i: &'hir hir::ImplItem<'hir>) { |
487cf647 | 187 | let mut inner_visitor = self.new_visitor(self.tcx); |
2b03887a | 188 | inner_visitor.check(i.owner_id, |this| intravisit::walk_impl_item(this, i)); |
fc512014 | 189 | } |
cc61c64b | 190 | } |