]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_lowering/src/index.rs
Point at type parameter in plain path expr
[rust.git] / compiler / rustc_ast_lowering / src / index.rs
1 use rustc_data_structures::fx::FxHashMap;
2 use rustc_data_structures::sorted_map::SortedMap;
3 use rustc_hir as hir;
4 use rustc_hir::def_id::LocalDefId;
5 use rustc_hir::definitions;
6 use rustc_hir::intravisit::{self, Visitor};
7 use rustc_hir::*;
8 use rustc_index::vec::{Idx, IndexVec};
9 use rustc_middle::span_bug;
10 use rustc_session::Session;
11 use rustc_span::source_map::SourceMap;
12 use rustc_span::{Span, DUMMY_SP};
13
14 /// A visitor that walks over the HIR and collects `Node`s into a HIR map.
15 pub(super) struct NodeCollector<'a, 'hir> {
16     /// Source map
17     source_map: &'a SourceMap,
18     bodies: &'a SortedMap<ItemLocalId, &'hir Body<'hir>>,
19
20     /// Outputs
21     nodes: IndexVec<ItemLocalId, Option<ParentedNode<'hir>>>,
22     parenting: FxHashMap<LocalDefId, ItemLocalId>,
23
24     /// The parent of this node
25     parent_node: hir::ItemLocalId,
26
27     owner: LocalDefId,
28
29     definitions: &'a definitions::Definitions,
30 }
31
32 #[instrument(level = "debug", skip(sess, definitions, bodies))]
33 pub(super) fn index_hir<'hir>(
34     sess: &Session,
35     definitions: &definitions::Definitions,
36     item: hir::OwnerNode<'hir>,
37     bodies: &SortedMap<ItemLocalId, &'hir Body<'hir>>,
38 ) -> (IndexVec<ItemLocalId, Option<ParentedNode<'hir>>>, FxHashMap<LocalDefId, ItemLocalId>) {
39     let mut nodes = IndexVec::new();
40     // This node's parent should never be accessed: the owner's parent is computed by the
41     // hir_owner_parent query.  Make it invalid (= ItemLocalId::MAX) to force an ICE whenever it is
42     // used.
43     nodes.push(Some(ParentedNode { parent: ItemLocalId::INVALID, node: item.into() }));
44     let mut collector = NodeCollector {
45         source_map: sess.source_map(),
46         definitions,
47         owner: item.def_id(),
48         parent_node: ItemLocalId::new(0),
49         nodes,
50         bodies,
51         parenting: FxHashMap::default(),
52     };
53
54     match item {
55         OwnerNode::Crate(citem) => {
56             collector.visit_mod(&citem, citem.spans.inner_span, hir::CRATE_HIR_ID)
57         }
58         OwnerNode::Item(item) => collector.visit_item(item),
59         OwnerNode::TraitItem(item) => collector.visit_trait_item(item),
60         OwnerNode::ImplItem(item) => collector.visit_impl_item(item),
61         OwnerNode::ForeignItem(item) => collector.visit_foreign_item(item),
62     };
63
64     (collector.nodes, collector.parenting)
65 }
66
67 impl<'a, 'hir> NodeCollector<'a, 'hir> {
68     #[instrument(level = "debug", skip(self))]
69     fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
70         debug_assert_eq!(self.owner, hir_id.owner);
71         debug_assert_ne!(hir_id.local_id.as_u32(), 0);
72
73         // Make sure that the DepNode of some node coincides with the HirId
74         // owner of that node.
75         if cfg!(debug_assertions) {
76             if hir_id.owner != self.owner {
77                 span_bug!(
78                     span,
79                     "inconsistent DepNode at `{:?}` for `{:?}`: \
80                      current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
81                     self.source_map.span_to_diagnostic_string(span),
82                     node,
83                     self.definitions.def_path(self.owner).to_string_no_crate_verbose(),
84                     self.owner,
85                     self.definitions.def_path(hir_id.owner).to_string_no_crate_verbose(),
86                     hir_id.owner,
87                 )
88             }
89         }
90
91         self.nodes.insert(hir_id.local_id, ParentedNode { parent: self.parent_node, node: node });
92     }
93
94     fn with_parent<F: FnOnce(&mut Self)>(&mut self, parent_node_id: HirId, f: F) {
95         debug_assert_eq!(parent_node_id.owner, self.owner);
96         let parent_node = self.parent_node;
97         self.parent_node = parent_node_id.local_id;
98         f(self);
99         self.parent_node = parent_node;
100     }
101
102     fn insert_nested(&mut self, item: LocalDefId) {
103         self.parenting.insert(item, self.parent_node);
104     }
105 }
106
107 impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
108     /// Because we want to track parent items and so forth, enable
109     /// deep walking so that we walk nested items in the context of
110     /// their outer items.
111
112     fn visit_nested_item(&mut self, item: ItemId) {
113         debug!("visit_nested_item: {:?}", item);
114         self.insert_nested(item.def_id);
115     }
116
117     fn visit_nested_trait_item(&mut self, item_id: TraitItemId) {
118         self.insert_nested(item_id.def_id);
119     }
120
121     fn visit_nested_impl_item(&mut self, item_id: ImplItemId) {
122         self.insert_nested(item_id.def_id);
123     }
124
125     fn visit_nested_foreign_item(&mut self, foreign_id: ForeignItemId) {
126         self.insert_nested(foreign_id.def_id);
127     }
128
129     fn visit_nested_body(&mut self, id: BodyId) {
130         debug_assert_eq!(id.hir_id.owner, self.owner);
131         let body = self.bodies[&id.hir_id.local_id];
132         self.visit_body(body);
133     }
134
135     fn visit_param(&mut self, param: &'hir Param<'hir>) {
136         let node = Node::Param(param);
137         self.insert(param.pat.span, param.hir_id, node);
138         self.with_parent(param.hir_id, |this| {
139             intravisit::walk_param(this, param);
140         });
141     }
142
143     #[instrument(level = "debug", skip(self))]
144     fn visit_item(&mut self, i: &'hir Item<'hir>) {
145         debug_assert_eq!(i.def_id, self.owner);
146         self.with_parent(i.hir_id(), |this| {
147             if let ItemKind::Struct(ref struct_def, _) = i.kind {
148                 // If this is a tuple or unit-like struct, register the constructor.
149                 if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
150                     this.insert(i.span, ctor_hir_id, Node::Ctor(struct_def));
151                 }
152             }
153             intravisit::walk_item(this, i);
154         });
155     }
156
157     #[instrument(level = "debug", skip(self))]
158     fn visit_foreign_item(&mut self, fi: &'hir ForeignItem<'hir>) {
159         debug_assert_eq!(fi.def_id, self.owner);
160         self.with_parent(fi.hir_id(), |this| {
161             intravisit::walk_foreign_item(this, fi);
162         });
163     }
164
165     fn visit_generic_param(&mut self, param: &'hir GenericParam<'hir>) {
166         self.insert(param.span, param.hir_id, Node::GenericParam(param));
167         intravisit::walk_generic_param(self, param);
168     }
169
170     fn visit_const_param_default(&mut self, param: HirId, ct: &'hir AnonConst) {
171         self.with_parent(param, |this| {
172             intravisit::walk_const_param_default(this, ct);
173         })
174     }
175
176     #[instrument(level = "debug", skip(self))]
177     fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
178         debug_assert_eq!(ti.def_id, self.owner);
179         self.with_parent(ti.hir_id(), |this| {
180             intravisit::walk_trait_item(this, ti);
181         });
182     }
183
184     #[instrument(level = "debug", skip(self))]
185     fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
186         debug_assert_eq!(ii.def_id, self.owner);
187         self.with_parent(ii.hir_id(), |this| {
188             intravisit::walk_impl_item(this, ii);
189         });
190     }
191
192     fn visit_pat(&mut self, pat: &'hir Pat<'hir>) {
193         self.insert(pat.span, pat.hir_id, Node::Pat(pat));
194
195         self.with_parent(pat.hir_id, |this| {
196             intravisit::walk_pat(this, pat);
197         });
198     }
199
200     fn visit_pat_field(&mut self, field: &'hir PatField<'hir>) {
201         self.insert(field.span, field.hir_id, Node::PatField(field));
202         self.with_parent(field.hir_id, |this| {
203             intravisit::walk_pat_field(this, field);
204         });
205     }
206
207     fn visit_arm(&mut self, arm: &'hir Arm<'hir>) {
208         let node = Node::Arm(arm);
209
210         self.insert(arm.span, arm.hir_id, node);
211
212         self.with_parent(arm.hir_id, |this| {
213             intravisit::walk_arm(this, arm);
214         });
215     }
216
217     fn visit_anon_const(&mut self, constant: &'hir AnonConst) {
218         self.insert(DUMMY_SP, constant.hir_id, Node::AnonConst(constant));
219
220         self.with_parent(constant.hir_id, |this| {
221             intravisit::walk_anon_const(this, constant);
222         });
223     }
224
225     fn visit_expr(&mut self, expr: &'hir Expr<'hir>) {
226         self.insert(expr.span, expr.hir_id, Node::Expr(expr));
227
228         self.with_parent(expr.hir_id, |this| {
229             intravisit::walk_expr(this, expr);
230         });
231     }
232
233     fn visit_expr_field(&mut self, field: &'hir ExprField<'hir>) {
234         self.insert(field.span, field.hir_id, Node::ExprField(field));
235         self.with_parent(field.hir_id, |this| {
236             intravisit::walk_expr_field(this, field);
237         });
238     }
239
240     fn visit_stmt(&mut self, stmt: &'hir Stmt<'hir>) {
241         self.insert(stmt.span, stmt.hir_id, Node::Stmt(stmt));
242
243         self.with_parent(stmt.hir_id, |this| {
244             intravisit::walk_stmt(this, stmt);
245         });
246     }
247
248     fn visit_path_segment(&mut self, path_span: Span, path_segment: &'hir PathSegment<'hir>) {
249         if let Some(hir_id) = path_segment.hir_id {
250             self.insert(path_span, hir_id, Node::PathSegment(path_segment));
251         }
252         intravisit::walk_path_segment(self, path_span, path_segment);
253     }
254
255     fn visit_ty(&mut self, ty: &'hir Ty<'hir>) {
256         self.insert(ty.span, ty.hir_id, Node::Ty(ty));
257
258         self.with_parent(ty.hir_id, |this| {
259             intravisit::walk_ty(this, ty);
260         });
261     }
262
263     fn visit_infer(&mut self, inf: &'hir InferArg) {
264         self.insert(inf.span, inf.hir_id, Node::Infer(inf));
265
266         self.with_parent(inf.hir_id, |this| {
267             intravisit::walk_inf(this, inf);
268         });
269     }
270
271     fn visit_trait_ref(&mut self, tr: &'hir TraitRef<'hir>) {
272         self.insert(tr.path.span, tr.hir_ref_id, Node::TraitRef(tr));
273
274         self.with_parent(tr.hir_ref_id, |this| {
275             intravisit::walk_trait_ref(this, tr);
276         });
277     }
278
279     fn visit_fn(
280         &mut self,
281         fk: intravisit::FnKind<'hir>,
282         fd: &'hir FnDecl<'hir>,
283         b: BodyId,
284         s: Span,
285         id: HirId,
286     ) {
287         assert_eq!(self.owner, id.owner);
288         assert_eq!(self.parent_node, id.local_id);
289         intravisit::walk_fn(self, fk, fd, b, s, id);
290     }
291
292     fn visit_block(&mut self, block: &'hir Block<'hir>) {
293         self.insert(block.span, block.hir_id, Node::Block(block));
294         self.with_parent(block.hir_id, |this| {
295             intravisit::walk_block(this, block);
296         });
297     }
298
299     fn visit_local(&mut self, l: &'hir Local<'hir>) {
300         self.insert(l.span, l.hir_id, Node::Local(l));
301         self.with_parent(l.hir_id, |this| {
302             intravisit::walk_local(this, l);
303         })
304     }
305
306     fn visit_lifetime(&mut self, lifetime: &'hir Lifetime) {
307         self.insert(lifetime.span, lifetime.hir_id, Node::Lifetime(lifetime));
308     }
309
310     fn visit_variant(&mut self, v: &'hir Variant<'hir>) {
311         self.insert(v.span, v.id, Node::Variant(v));
312         self.with_parent(v.id, |this| {
313             // Register the constructor of this variant.
314             if let Some(ctor_hir_id) = v.data.ctor_hir_id() {
315                 this.insert(v.span, ctor_hir_id, Node::Ctor(&v.data));
316             }
317             intravisit::walk_variant(this, v);
318         });
319     }
320
321     fn visit_field_def(&mut self, field: &'hir FieldDef<'hir>) {
322         self.insert(field.span, field.hir_id, Node::Field(field));
323         self.with_parent(field.hir_id, |this| {
324             intravisit::walk_field_def(this, field);
325         });
326     }
327
328     fn visit_assoc_type_binding(&mut self, type_binding: &'hir TypeBinding<'hir>) {
329         self.insert(type_binding.span, type_binding.hir_id, Node::TypeBinding(type_binding));
330         self.with_parent(type_binding.hir_id, |this| {
331             intravisit::walk_assoc_type_binding(this, type_binding)
332         })
333     }
334
335     fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
336         // Do not visit the duplicate information in TraitItemRef. We want to
337         // map the actual nodes, not the duplicate ones in the *Ref.
338         let TraitItemRef { id, ident: _, kind: _, span: _ } = *ii;
339
340         self.visit_nested_trait_item(id);
341     }
342
343     fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
344         // Do not visit the duplicate information in ImplItemRef. We want to
345         // map the actual nodes, not the duplicate ones in the *Ref.
346         let ImplItemRef { id, ident: _, kind: _, span: _, trait_item_def_id: _ } = *ii;
347
348         self.visit_nested_impl_item(id);
349     }
350
351     fn visit_foreign_item_ref(&mut self, fi: &'hir ForeignItemRef) {
352         // Do not visit the duplicate information in ForeignItemRef. We want to
353         // map the actual nodes, not the duplicate ones in the *Ref.
354         let ForeignItemRef { id, ident: _, span: _ } = *fi;
355
356         self.visit_nested_foreign_item(id);
357     }
358 }