]> git.proxmox.com Git - rustc.git/blob - src/librustc/hir/map/hir_id_validator.rs
New upstream version 1.34.2+dfsg1
[rustc.git] / src / librustc / hir / map / hir_id_validator.rs
1 use crate::hir::def_id::{DefId, DefIndex, CRATE_DEF_INDEX};
2 use crate::hir::{self, intravisit, HirId, ItemLocalId};
3 use syntax::ast::NodeId;
4 use crate::hir::itemlikevisit::ItemLikeVisitor;
5 use rustc_data_structures::fx::FxHashSet;
6 use rustc_data_structures::sync::{Lock, ParallelIterator, par_iter};
7
8 pub fn check_crate<'hir>(hir_map: &hir::map::Map<'hir>) {
9 hir_map.dep_graph.assert_ignored();
10
11 let errors = Lock::new(Vec::new());
12
13 par_iter(&hir_map.krate().modules).for_each(|(module_id, _)| {
14 hir_map.visit_item_likes_in_module(hir_map.local_def_id(*module_id), &mut OuterVisitor {
15 hir_map,
16 errors: &errors,
17 });
18 });
19
20 let errors = errors.into_inner();
21
22 if !errors.is_empty() {
23 let message = errors
24 .iter()
25 .fold(String::new(), |s1, s2| s1 + "\n" + s2);
26 bug!("{}", message);
27 }
28 }
29
30 struct HirIdValidator<'a, 'hir: 'a> {
31 hir_map: &'a hir::map::Map<'hir>,
32 owner_def_index: Option<DefIndex>,
33 hir_ids_seen: FxHashSet<ItemLocalId>,
34 errors: &'a Lock<Vec<String>>,
35 }
36
37 struct OuterVisitor<'a, 'hir: 'a> {
38 hir_map: &'a hir::map::Map<'hir>,
39 errors: &'a Lock<Vec<String>>,
40 }
41
42 impl<'a, 'hir: 'a> OuterVisitor<'a, 'hir> {
43 fn new_inner_visitor(&self,
44 hir_map: &'a hir::map::Map<'hir>)
45 -> HirIdValidator<'a, 'hir> {
46 HirIdValidator {
47 hir_map,
48 owner_def_index: None,
49 hir_ids_seen: Default::default(),
50 errors: self.errors,
51 }
52 }
53 }
54
55 impl<'a, 'hir: 'a> ItemLikeVisitor<'hir> for OuterVisitor<'a, 'hir> {
56 fn visit_item(&mut self, i: &'hir hir::Item) {
57 let mut inner_visitor = self.new_inner_visitor(self.hir_map);
58 inner_visitor.check(i.hir_id, |this| intravisit::walk_item(this, i));
59 }
60
61 fn visit_trait_item(&mut self, i: &'hir hir::TraitItem) {
62 let mut inner_visitor = self.new_inner_visitor(self.hir_map);
63 inner_visitor.check(i.hir_id, |this| intravisit::walk_trait_item(this, i));
64 }
65
66 fn visit_impl_item(&mut self, i: &'hir hir::ImplItem) {
67 let mut inner_visitor = self.new_inner_visitor(self.hir_map);
68 inner_visitor.check(i.hir_id, |this| intravisit::walk_impl_item(this, i));
69 }
70 }
71
72 impl<'a, 'hir: 'a> HirIdValidator<'a, 'hir> {
73 #[cold]
74 #[inline(never)]
75 fn error(&self, f: impl FnOnce() -> String) {
76 self.errors.lock().push(f());
77 }
78
79 fn check<F: FnOnce(&mut HirIdValidator<'a, 'hir>)>(&mut self,
80 hir_id: HirId,
81 walk: F) {
82 assert!(self.owner_def_index.is_none());
83 let owner_def_index = self.hir_map.local_def_id_from_hir_id(hir_id).index;
84 self.owner_def_index = Some(owner_def_index);
85 walk(self);
86
87 if owner_def_index == CRATE_DEF_INDEX {
88 return;
89 }
90
91 // There's always at least one entry for the owning item itself
92 let max = self.hir_ids_seen
93 .iter()
94 .map(|local_id| local_id.as_usize())
95 .max()
96 .expect("owning item has no entry");
97
98 if max != self.hir_ids_seen.len() - 1 {
99 // Collect the missing ItemLocalIds
100 let missing: Vec<_> = (0 ..= max as u32)
101 .filter(|&i| !self.hir_ids_seen.contains(&ItemLocalId::from_u32(i)))
102 .collect();
103
104 // Try to map those to something more useful
105 let mut missing_items = Vec::with_capacity(missing.len());
106
107 for local_id in missing {
108 let hir_id = HirId {
109 owner: owner_def_index,
110 local_id: ItemLocalId::from_u32(local_id),
111 };
112
113 trace!("missing hir id {:#?}", hir_id);
114
115 // We are already in ICE mode here, so doing a linear search
116 // should be fine.
117 let (node_id, _) = self.hir_map
118 .definitions()
119 .node_to_hir_id
120 .iter()
121 .enumerate()
122 .find(|&(_, &entry)| hir_id == entry)
123 .expect("no node_to_hir_id entry");
124 let node_id = NodeId::from_usize(node_id);
125 missing_items.push(format!("[local_id: {}, node:{}]",
126 local_id,
127 self.hir_map.node_to_string(node_id)));
128 }
129 self.error(|| format!(
130 "ItemLocalIds not assigned densely in {}. \
131 Max ItemLocalId = {}, missing IDs = {:?}; seens IDs = {:?}",
132 self.hir_map.def_path(DefId::local(owner_def_index)).to_string_no_crate(),
133 max,
134 missing_items,
135 self.hir_ids_seen
136 .iter()
137 .map(|&local_id| HirId {
138 owner: owner_def_index,
139 local_id,
140 })
141 .map(|h| format!("({:?} {})", h, self.hir_map.hir_to_string(h)))
142 .collect::<Vec<_>>()));
143 }
144 }
145 }
146
147 impl<'a, 'hir: 'a> intravisit::Visitor<'hir> for HirIdValidator<'a, 'hir> {
148
149 fn nested_visit_map<'this>(&'this mut self)
150 -> intravisit::NestedVisitorMap<'this, 'hir> {
151 intravisit::NestedVisitorMap::OnlyBodies(self.hir_map)
152 }
153
154 fn visit_id(&mut self, hir_id: HirId) {
155 let owner = self.owner_def_index.expect("no owner_def_index");
156
157 if hir_id == hir::DUMMY_HIR_ID {
158 self.error(|| format!("HirIdValidator: HirId {:?} is invalid",
159 self.hir_map.hir_to_string(hir_id)));
160 return;
161 }
162
163 if owner != hir_id.owner {
164 self.error(|| format!(
165 "HirIdValidator: The recorded owner of {} is {} instead of {}",
166 self.hir_map.hir_to_string(hir_id),
167 self.hir_map.def_path(DefId::local(hir_id.owner)).to_string_no_crate(),
168 self.hir_map.def_path(DefId::local(owner)).to_string_no_crate()));
169 }
170
171 self.hir_ids_seen.insert(hir_id.local_id);
172 }
173
174 fn visit_impl_item_ref(&mut self, _: &'hir hir::ImplItemRef) {
175 // Explicitly do nothing here. ImplItemRefs contain hir::Visibility
176 // values that actually belong to an ImplItem instead of the ItemKind::Impl
177 // we are currently in. So for those it's correct that they have a
178 // different owner.
179 }
180 }