]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_save_analysis/src/dump_visitor.rs
Rollup merge of #87045 - jhpratt:fix-tracking-issue, r=jyn514
[rust.git] / compiler / rustc_save_analysis / src / dump_visitor.rs
1 //! Write the output of rustc's analysis to an implementor of Dump.
2 //!
3 //! Dumping the analysis is implemented by walking the AST and getting a bunch of
4 //! info out from all over the place. We use `DefId`s to identify objects. The
5 //! tricky part is getting syntactic (span, source text) and semantic (reference
6 //! `DefId`s) information for parts of expressions which the compiler has discarded.
7 //! E.g., in a path `foo::bar::baz`, the compiler only keeps a span for the whole
8 //! path and a reference to `baz`, but we want spans and references for all three
9 //! idents.
10 //!
11 //! SpanUtils is used to manipulate spans. In particular, to extract sub-spans
12 //! from spans (e.g., the span for `bar` from the above example path).
13 //! DumpVisitor walks the AST and processes it, and Dumper is used for
14 //! recording the output.
15
16 use rustc_ast as ast;
17 use rustc_ast::walk_list;
18 use rustc_data_structures::fx::FxHashSet;
19 use rustc_hir as hir;
20 use rustc_hir::def::{DefKind as HirDefKind, Res};
21 use rustc_hir::def_id::{DefId, LocalDefId};
22 use rustc_hir::intravisit::{self, Visitor};
23 use rustc_hir_pretty::{bounds_to_string, fn_to_string, generic_params_to_string, ty_to_string};
24 use rustc_middle::hir::map::Map;
25 use rustc_middle::span_bug;
26 use rustc_middle::ty::{self, DefIdTree, TyCtxt};
27 use rustc_session::config::Input;
28 use rustc_span::source_map::respan;
29 use rustc_span::symbol::Ident;
30 use rustc_span::*;
31
32 use std::env;
33 use std::path::Path;
34
35 use crate::dumper::{Access, Dumper};
36 use crate::sig;
37 use crate::span_utils::SpanUtils;
38 use crate::{
39     escape, generated_code, id_from_def_id, id_from_hir_id, lower_attributes, PathCollector,
40     SaveContext,
41 };
42
43 use rls_data::{
44     CompilationOptions, CratePreludeData, Def, DefKind, GlobalCrateId, Import, ImportKind, Ref,
45     RefKind, Relation, RelationKind, SpanData,
46 };
47
48 use tracing::{debug, error};
49
50 macro_rules! down_cast_data {
51     ($id:ident, $kind:ident, $sp:expr) => {
52         let $id = if let super::Data::$kind(data) = $id {
53             data
54         } else {
55             span_bug!($sp, "unexpected data kind: {:?}", $id);
56         };
57     };
58 }
59
60 macro_rules! access_from {
61     ($save_ctxt:expr, $item:expr, $id:expr) => {
62         Access {
63             public: $item.vis.node.is_pub(),
64             reachable: $save_ctxt.access_levels.is_reachable($id),
65         }
66     };
67 }
68
69 macro_rules! access_from_vis {
70     ($save_ctxt:expr, $vis:expr, $id:expr) => {
71         Access { public: $vis.node.is_pub(), reachable: $save_ctxt.access_levels.is_reachable($id) }
72     };
73 }
74
75 pub struct DumpVisitor<'tcx> {
76     pub save_ctxt: SaveContext<'tcx>,
77     tcx: TyCtxt<'tcx>,
78     dumper: Dumper,
79
80     span: SpanUtils<'tcx>,
81     // Set of macro definition (callee) spans, and the set
82     // of macro use (callsite) spans. We store these to ensure
83     // we only write one macro def per unique macro definition, and
84     // one macro use per unique callsite span.
85     // mac_defs: FxHashSet<Span>,
86     // macro_calls: FxHashSet<Span>,
87 }
88
89 impl<'tcx> DumpVisitor<'tcx> {
90     pub fn new(save_ctxt: SaveContext<'tcx>) -> DumpVisitor<'tcx> {
91         let span_utils = SpanUtils::new(&save_ctxt.tcx.sess);
92         let dumper = Dumper::new(save_ctxt.config.clone());
93         DumpVisitor {
94             tcx: save_ctxt.tcx,
95             save_ctxt,
96             dumper,
97             span: span_utils,
98             // mac_defs: FxHashSet::default(),
99             // macro_calls: FxHashSet::default(),
100         }
101     }
102
103     pub fn analysis(&self) -> &rls_data::Analysis {
104         self.dumper.analysis()
105     }
106
107     fn nest_typeck_results<F>(&mut self, item_def_id: LocalDefId, f: F)
108     where
109         F: FnOnce(&mut Self),
110     {
111         let typeck_results = if self.tcx.has_typeck_results(item_def_id) {
112             Some(self.tcx.typeck(item_def_id))
113         } else {
114             None
115         };
116
117         let old_maybe_typeck_results = self.save_ctxt.maybe_typeck_results;
118         self.save_ctxt.maybe_typeck_results = typeck_results;
119         f(self);
120         self.save_ctxt.maybe_typeck_results = old_maybe_typeck_results;
121     }
122
123     fn span_from_span(&self, span: Span) -> SpanData {
124         self.save_ctxt.span_from_span(span)
125     }
126
127     fn lookup_def_id(&self, ref_id: hir::HirId) -> Option<DefId> {
128         self.save_ctxt.lookup_def_id(ref_id)
129     }
130
131     pub fn dump_crate_info(&mut self, name: &str, krate: &hir::Crate<'_>) {
132         let source_file = self.tcx.sess.local_crate_source_file.as_ref();
133         let crate_root = source_file.map(|source_file| {
134             let source_file = Path::new(source_file);
135             match source_file.file_name() {
136                 Some(_) => source_file.parent().unwrap().display(),
137                 None => source_file.display(),
138             }
139             .to_string()
140         });
141
142         let data = CratePreludeData {
143             crate_id: GlobalCrateId {
144                 name: name.into(),
145                 disambiguator: (self.tcx.sess.local_stable_crate_id().to_u64(), 0),
146             },
147             crate_root: crate_root.unwrap_or_else(|| "<no source>".to_owned()),
148             external_crates: self.save_ctxt.get_external_crates(),
149             span: self.span_from_span(krate.item.inner),
150         };
151
152         self.dumper.crate_prelude(data);
153     }
154
155     pub fn dump_compilation_options(&mut self, input: &Input, crate_name: &str) {
156         // Apply possible `remap-path-prefix` remapping to the input source file
157         // (and don't include remapping args anymore)
158         let (program, arguments) = {
159             let remap_arg_indices = {
160                 let mut indices = FxHashSet::default();
161                 // Args are guaranteed to be valid UTF-8 (checked early)
162                 for (i, e) in env::args().enumerate() {
163                     if e.starts_with("--remap-path-prefix=") {
164                         indices.insert(i);
165                     } else if e == "--remap-path-prefix" {
166                         indices.insert(i);
167                         indices.insert(i + 1);
168                     }
169                 }
170                 indices
171             };
172
173             let mut args = env::args()
174                 .enumerate()
175                 .filter(|(i, _)| !remap_arg_indices.contains(i))
176                 .map(|(_, arg)| match input {
177                     Input::File(ref path) if path == Path::new(&arg) => {
178                         let mapped = &self.tcx.sess.local_crate_source_file;
179                         mapped.as_ref().unwrap().to_string_lossy().into()
180                     }
181                     _ => arg,
182                 });
183
184             (args.next().unwrap(), args.collect())
185         };
186
187         let data = CompilationOptions {
188             directory: self.tcx.sess.working_dir.remapped_path_if_available().into(),
189             program,
190             arguments,
191             output: self.save_ctxt.compilation_output(crate_name),
192         };
193
194         self.dumper.compilation_opts(data);
195     }
196
197     fn write_segments(&mut self, segments: impl IntoIterator<Item = &'tcx hir::PathSegment<'tcx>>) {
198         for seg in segments {
199             if let Some(data) = self.save_ctxt.get_path_segment_data(seg) {
200                 self.dumper.dump_ref(data);
201             }
202         }
203     }
204
205     fn write_sub_paths(&mut self, path: &'tcx hir::Path<'tcx>) {
206         self.write_segments(path.segments)
207     }
208
209     // As write_sub_paths, but does not process the last ident in the path (assuming it
210     // will be processed elsewhere). See note on write_sub_paths about global.
211     fn write_sub_paths_truncated(&mut self, path: &'tcx hir::Path<'tcx>) {
212         if let [segments @ .., _] = path.segments {
213             self.write_segments(segments)
214         }
215     }
216
217     fn process_formals(&mut self, formals: &'tcx [hir::Param<'tcx>], qualname: &str) {
218         for arg in formals {
219             self.visit_pat(&arg.pat);
220             let mut collector = PathCollector::new(self.tcx);
221             collector.visit_pat(&arg.pat);
222
223             for (hir_id, ident, ..) in collector.collected_idents {
224                 let typ = match self.save_ctxt.typeck_results().node_type_opt(hir_id) {
225                     Some(s) => s.to_string(),
226                     None => continue,
227                 };
228                 if !self.span.filter_generated(ident.span) {
229                     let id = id_from_hir_id(hir_id, &self.save_ctxt);
230                     let span = self.span_from_span(ident.span);
231
232                     self.dumper.dump_def(
233                         &Access { public: false, reachable: false },
234                         Def {
235                             kind: DefKind::Local,
236                             id,
237                             span,
238                             name: ident.to_string(),
239                             qualname: format!("{}::{}", qualname, ident.to_string()),
240                             value: typ,
241                             parent: None,
242                             children: vec![],
243                             decl_id: None,
244                             docs: String::new(),
245                             sig: None,
246                             attributes: vec![],
247                         },
248                     );
249                 }
250             }
251         }
252     }
253
254     fn process_method(
255         &mut self,
256         sig: &'tcx hir::FnSig<'tcx>,
257         body: Option<hir::BodyId>,
258         hir_id: hir::HirId,
259         ident: Ident,
260         generics: &'tcx hir::Generics<'tcx>,
261         vis: &hir::Visibility<'tcx>,
262         span: Span,
263     ) {
264         debug!("process_method: {}:{}", hir_id, ident);
265
266         let map = &self.tcx.hir();
267         self.nest_typeck_results(map.local_def_id(hir_id), |v| {
268             if let Some(mut method_data) = v.save_ctxt.get_method_data(hir_id, ident, span) {
269                 if let Some(body) = body {
270                     v.process_formals(map.body(body).params, &method_data.qualname);
271                 }
272                 v.process_generic_params(&generics, &method_data.qualname, hir_id);
273
274                 method_data.value =
275                     fn_to_string(sig.decl, sig.header, Some(ident.name), generics, vis, &[], None);
276                 method_data.sig = sig::method_signature(hir_id, ident, generics, sig, &v.save_ctxt);
277
278                 v.dumper.dump_def(&access_from_vis!(v.save_ctxt, vis, hir_id), method_data);
279             }
280
281             // walk arg and return types
282             for arg in sig.decl.inputs {
283                 v.visit_ty(arg);
284             }
285
286             if let hir::FnRetTy::Return(ref ret_ty) = sig.decl.output {
287                 v.visit_ty(ret_ty)
288             }
289
290             // walk the fn body
291             if let Some(body) = body {
292                 v.visit_expr(&map.body(body).value);
293             }
294         });
295     }
296
297     fn process_struct_field_def(
298         &mut self,
299         field: &'tcx hir::FieldDef<'tcx>,
300         parent_id: hir::HirId,
301     ) {
302         let field_data = self.save_ctxt.get_field_data(field, parent_id);
303         if let Some(field_data) = field_data {
304             self.dumper.dump_def(&access_from!(self.save_ctxt, field, field.hir_id), field_data);
305         }
306     }
307
308     // Dump generic params bindings, then visit_generics
309     fn process_generic_params(
310         &mut self,
311         generics: &'tcx hir::Generics<'tcx>,
312         prefix: &str,
313         id: hir::HirId,
314     ) {
315         for param in generics.params {
316             match param.kind {
317                 hir::GenericParamKind::Lifetime { .. } => {}
318                 hir::GenericParamKind::Type { .. } => {
319                     let param_ss = param.name.ident().span;
320                     let name = escape(self.span.snippet(param_ss));
321                     // Append $id to name to make sure each one is unique.
322                     let qualname = format!("{}::{}${}", prefix, name, id);
323                     if !self.span.filter_generated(param_ss) {
324                         let id = id_from_hir_id(param.hir_id, &self.save_ctxt);
325                         let span = self.span_from_span(param_ss);
326
327                         self.dumper.dump_def(
328                             &Access { public: false, reachable: false },
329                             Def {
330                                 kind: DefKind::Type,
331                                 id,
332                                 span,
333                                 name,
334                                 qualname,
335                                 value: String::new(),
336                                 parent: None,
337                                 children: vec![],
338                                 decl_id: None,
339                                 docs: String::new(),
340                                 sig: None,
341                                 attributes: vec![],
342                             },
343                         );
344                     }
345                 }
346                 hir::GenericParamKind::Const { .. } => {}
347             }
348         }
349
350         self.visit_generics(generics)
351     }
352
353     fn process_fn(
354         &mut self,
355         item: &'tcx hir::Item<'tcx>,
356         decl: &'tcx hir::FnDecl<'tcx>,
357         _header: &'tcx hir::FnHeader,
358         ty_params: &'tcx hir::Generics<'tcx>,
359         body: hir::BodyId,
360     ) {
361         let map = &self.tcx.hir();
362         self.nest_typeck_results(item.def_id, |v| {
363             let body = map.body(body);
364             if let Some(fn_data) = v.save_ctxt.get_item_data(item) {
365                 down_cast_data!(fn_data, DefData, item.span);
366                 v.process_formals(body.params, &fn_data.qualname);
367                 v.process_generic_params(ty_params, &fn_data.qualname, item.hir_id());
368
369                 v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.hir_id()), fn_data);
370             }
371
372             for arg in decl.inputs {
373                 v.visit_ty(arg)
374             }
375
376             if let hir::FnRetTy::Return(ref ret_ty) = decl.output {
377                 v.visit_ty(ret_ty)
378             }
379
380             v.visit_expr(&body.value);
381         });
382     }
383
384     fn process_static_or_const_item(
385         &mut self,
386         item: &'tcx hir::Item<'tcx>,
387         typ: &'tcx hir::Ty<'tcx>,
388         expr: &'tcx hir::Expr<'tcx>,
389     ) {
390         self.nest_typeck_results(item.def_id, |v| {
391             if let Some(var_data) = v.save_ctxt.get_item_data(item) {
392                 down_cast_data!(var_data, DefData, item.span);
393                 v.dumper.dump_def(&access_from!(v.save_ctxt, item, item.hir_id()), var_data);
394             }
395             v.visit_ty(&typ);
396             v.visit_expr(expr);
397         });
398     }
399
400     fn process_assoc_const(
401         &mut self,
402         hir_id: hir::HirId,
403         ident: Ident,
404         typ: &'tcx hir::Ty<'tcx>,
405         expr: Option<&'tcx hir::Expr<'tcx>>,
406         parent_id: DefId,
407         vis: &hir::Visibility<'tcx>,
408         attrs: &'tcx [ast::Attribute],
409     ) {
410         let qualname =
411             format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(hir_id).to_def_id()));
412
413         if !self.span.filter_generated(ident.span) {
414             let sig = sig::assoc_const_signature(hir_id, ident.name, typ, expr, &self.save_ctxt);
415             let span = self.span_from_span(ident.span);
416
417             self.dumper.dump_def(
418                 &access_from_vis!(self.save_ctxt, vis, hir_id),
419                 Def {
420                     kind: DefKind::Const,
421                     id: id_from_hir_id(hir_id, &self.save_ctxt),
422                     span,
423                     name: ident.name.to_string(),
424                     qualname,
425                     value: ty_to_string(&typ),
426                     parent: Some(id_from_def_id(parent_id)),
427                     children: vec![],
428                     decl_id: None,
429                     docs: self.save_ctxt.docs_for_attrs(attrs),
430                     sig,
431                     attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt),
432                 },
433             );
434         }
435
436         // walk type and init value
437         self.nest_typeck_results(self.tcx.hir().local_def_id(hir_id), |v| {
438             v.visit_ty(typ);
439             if let Some(expr) = expr {
440                 v.visit_expr(expr);
441             }
442         });
443     }
444
445     // FIXME tuple structs should generate tuple-specific data.
446     fn process_struct(
447         &mut self,
448         item: &'tcx hir::Item<'tcx>,
449         def: &'tcx hir::VariantData<'tcx>,
450         ty_params: &'tcx hir::Generics<'tcx>,
451     ) {
452         debug!("process_struct {:?} {:?}", item, item.span);
453         let name = item.ident.to_string();
454         let qualname = format!("::{}", self.tcx.def_path_str(item.def_id.to_def_id()));
455
456         let kind = match item.kind {
457             hir::ItemKind::Struct(_, _) => DefKind::Struct,
458             hir::ItemKind::Union(_, _) => DefKind::Union,
459             _ => unreachable!(),
460         };
461
462         let (value, fields) = match item.kind {
463             hir::ItemKind::Struct(hir::VariantData::Struct(ref fields, ..), ..)
464             | hir::ItemKind::Union(hir::VariantData::Struct(ref fields, ..), ..) => {
465                 let include_priv_fields = !self.save_ctxt.config.pub_only;
466                 let fields_str = fields
467                     .iter()
468                     .filter_map(|f| {
469                         if include_priv_fields || f.vis.node.is_pub() {
470                             Some(f.ident.to_string())
471                         } else {
472                             None
473                         }
474                     })
475                     .collect::<Vec<_>>()
476                     .join(", ");
477                 let value = format!("{} {{ {} }}", name, fields_str);
478                 (value, fields.iter().map(|f| id_from_hir_id(f.hir_id, &self.save_ctxt)).collect())
479             }
480             _ => (String::new(), vec![]),
481         };
482
483         if !self.span.filter_generated(item.ident.span) {
484             let span = self.span_from_span(item.ident.span);
485             let attrs = self.tcx.hir().attrs(item.hir_id());
486             self.dumper.dump_def(
487                 &access_from!(self.save_ctxt, item, item.hir_id()),
488                 Def {
489                     kind,
490                     id: id_from_def_id(item.def_id.to_def_id()),
491                     span,
492                     name,
493                     qualname: qualname.clone(),
494                     value,
495                     parent: None,
496                     children: fields,
497                     decl_id: None,
498                     docs: self.save_ctxt.docs_for_attrs(attrs),
499                     sig: sig::item_signature(item, &self.save_ctxt),
500                     attributes: lower_attributes(attrs.to_vec(), &self.save_ctxt),
501                 },
502             );
503         }
504
505         self.nest_typeck_results(item.def_id, |v| {
506             for field in def.fields() {
507                 v.process_struct_field_def(field, item.hir_id());
508                 v.visit_ty(&field.ty);
509             }
510
511             v.process_generic_params(ty_params, &qualname, item.hir_id());
512         });
513     }
514
515     fn process_enum(
516         &mut self,
517         item: &'tcx hir::Item<'tcx>,
518         enum_definition: &'tcx hir::EnumDef<'tcx>,
519         ty_params: &'tcx hir::Generics<'tcx>,
520     ) {
521         let enum_data = self.save_ctxt.get_item_data(item);
522         let enum_data = match enum_data {
523             None => return,
524             Some(data) => data,
525         };
526         down_cast_data!(enum_data, DefData, item.span);
527
528         let access = access_from!(self.save_ctxt, item, item.hir_id());
529
530         for variant in enum_definition.variants {
531             let name = variant.ident.name.to_string();
532             let qualname = format!("{}::{}", enum_data.qualname, name);
533             let name_span = variant.ident.span;
534
535             match variant.data {
536                 hir::VariantData::Struct(ref fields, ..) => {
537                     let fields_str =
538                         fields.iter().map(|f| f.ident.to_string()).collect::<Vec<_>>().join(", ");
539                     let value = format!("{}::{} {{ {} }}", enum_data.name, name, fields_str);
540                     if !self.span.filter_generated(name_span) {
541                         let span = self.span_from_span(name_span);
542                         let id = id_from_hir_id(variant.id, &self.save_ctxt);
543                         let parent = Some(id_from_def_id(item.def_id.to_def_id()));
544                         let attrs = self.tcx.hir().attrs(variant.id);
545
546                         self.dumper.dump_def(
547                             &access,
548                             Def {
549                                 kind: DefKind::StructVariant,
550                                 id,
551                                 span,
552                                 name,
553                                 qualname,
554                                 value,
555                                 parent,
556                                 children: vec![],
557                                 decl_id: None,
558                                 docs: self.save_ctxt.docs_for_attrs(attrs),
559                                 sig: sig::variant_signature(variant, &self.save_ctxt),
560                                 attributes: lower_attributes(attrs.to_vec(), &self.save_ctxt),
561                             },
562                         );
563                     }
564                 }
565                 ref v => {
566                     let mut value = format!("{}::{}", enum_data.name, name);
567                     if let hir::VariantData::Tuple(fields, _) = v {
568                         value.push('(');
569                         value.push_str(
570                             &fields
571                                 .iter()
572                                 .map(|f| ty_to_string(&f.ty))
573                                 .collect::<Vec<_>>()
574                                 .join(", "),
575                         );
576                         value.push(')');
577                     }
578                     if !self.span.filter_generated(name_span) {
579                         let span = self.span_from_span(name_span);
580                         let id = id_from_hir_id(variant.id, &self.save_ctxt);
581                         let parent = Some(id_from_def_id(item.def_id.to_def_id()));
582                         let attrs = self.tcx.hir().attrs(variant.id);
583
584                         self.dumper.dump_def(
585                             &access,
586                             Def {
587                                 kind: DefKind::TupleVariant,
588                                 id,
589                                 span,
590                                 name,
591                                 qualname,
592                                 value,
593                                 parent,
594                                 children: vec![],
595                                 decl_id: None,
596                                 docs: self.save_ctxt.docs_for_attrs(attrs),
597                                 sig: sig::variant_signature(variant, &self.save_ctxt),
598                                 attributes: lower_attributes(attrs.to_vec(), &self.save_ctxt),
599                             },
600                         );
601                     }
602                 }
603             }
604
605             for field in variant.data.fields() {
606                 self.process_struct_field_def(field, variant.id);
607                 self.visit_ty(field.ty);
608             }
609         }
610         self.process_generic_params(ty_params, &enum_data.qualname, item.hir_id());
611         self.dumper.dump_def(&access, enum_data);
612     }
613
614     fn process_impl(&mut self, item: &'tcx hir::Item<'tcx>, impl_: &'tcx hir::Impl<'tcx>) {
615         if let Some(impl_data) = self.save_ctxt.get_item_data(item) {
616             if !self.span.filter_generated(item.span) {
617                 if let super::Data::RelationData(rel, imp) = impl_data {
618                     self.dumper.dump_relation(rel);
619                     self.dumper.dump_impl(imp);
620                 } else {
621                     span_bug!(item.span, "unexpected data kind: {:?}", impl_data);
622                 }
623             }
624         }
625
626         let map = &self.tcx.hir();
627         self.nest_typeck_results(item.def_id, |v| {
628             v.visit_ty(&impl_.self_ty);
629             if let Some(trait_ref) = &impl_.of_trait {
630                 v.process_path(trait_ref.hir_ref_id, &hir::QPath::Resolved(None, &trait_ref.path));
631             }
632             v.process_generic_params(&impl_.generics, "", item.hir_id());
633             for impl_item in impl_.items {
634                 v.process_impl_item(map.impl_item(impl_item.id), item.def_id.to_def_id());
635             }
636         });
637     }
638
639     fn process_trait(
640         &mut self,
641         item: &'tcx hir::Item<'tcx>,
642         generics: &'tcx hir::Generics<'tcx>,
643         trait_refs: hir::GenericBounds<'tcx>,
644         methods: &'tcx [hir::TraitItemRef],
645     ) {
646         let name = item.ident.to_string();
647         let qualname = format!("::{}", self.tcx.def_path_str(item.def_id.to_def_id()));
648         let mut val = name.clone();
649         if !generics.params.is_empty() {
650             val.push_str(&generic_params_to_string(generics.params));
651         }
652         if !trait_refs.is_empty() {
653             val.push_str(": ");
654             val.push_str(&bounds_to_string(trait_refs));
655         }
656         if !self.span.filter_generated(item.ident.span) {
657             let id = id_from_def_id(item.def_id.to_def_id());
658             let span = self.span_from_span(item.ident.span);
659             let children =
660                 methods.iter().map(|i| id_from_def_id(i.id.def_id.to_def_id())).collect();
661             let attrs = self.tcx.hir().attrs(item.hir_id());
662             self.dumper.dump_def(
663                 &access_from!(self.save_ctxt, item, item.hir_id()),
664                 Def {
665                     kind: DefKind::Trait,
666                     id,
667                     span,
668                     name,
669                     qualname: qualname.clone(),
670                     value: val,
671                     parent: None,
672                     children,
673                     decl_id: None,
674                     docs: self.save_ctxt.docs_for_attrs(attrs),
675                     sig: sig::item_signature(item, &self.save_ctxt),
676                     attributes: lower_attributes(attrs.to_vec(), &self.save_ctxt),
677                 },
678             );
679         }
680
681         // super-traits
682         for super_bound in trait_refs.iter() {
683             let (def_id, sub_span) = match *super_bound {
684                 hir::GenericBound::Trait(ref trait_ref, _) => (
685                     self.lookup_def_id(trait_ref.trait_ref.hir_ref_id),
686                     trait_ref.trait_ref.path.segments.last().unwrap().ident.span,
687                 ),
688                 hir::GenericBound::LangItemTrait(lang_item, span, _, _) => {
689                     (Some(self.tcx.require_lang_item(lang_item, Some(span))), span)
690                 }
691                 hir::GenericBound::Outlives(..) => continue,
692             };
693
694             if let Some(id) = def_id {
695                 if !self.span.filter_generated(sub_span) {
696                     let span = self.span_from_span(sub_span);
697                     self.dumper.dump_ref(Ref {
698                         kind: RefKind::Type,
699                         span: span.clone(),
700                         ref_id: id_from_def_id(id),
701                     });
702
703                     self.dumper.dump_relation(Relation {
704                         kind: RelationKind::SuperTrait,
705                         span,
706                         from: id_from_def_id(id),
707                         to: id_from_def_id(item.def_id.to_def_id()),
708                     });
709                 }
710             }
711         }
712
713         // walk generics and methods
714         self.process_generic_params(generics, &qualname, item.hir_id());
715         for method in methods {
716             let map = &self.tcx.hir();
717             self.process_trait_item(map.trait_item(method.id), item.def_id.to_def_id())
718         }
719     }
720
721     // `item` is the module in question, represented as an( item.
722     fn process_mod(&mut self, item: &'tcx hir::Item<'tcx>) {
723         if let Some(mod_data) = self.save_ctxt.get_item_data(item) {
724             down_cast_data!(mod_data, DefData, item.span);
725             self.dumper.dump_def(&access_from!(self.save_ctxt, item, item.hir_id()), mod_data);
726         }
727     }
728
729     fn dump_path_ref(&mut self, id: hir::HirId, path: &hir::QPath<'tcx>) {
730         let path_data = self.save_ctxt.get_path_data(id, path);
731         if let Some(path_data) = path_data {
732             self.dumper.dump_ref(path_data);
733         }
734     }
735
736     fn dump_path_segment_ref(&mut self, id: hir::HirId, segment: &hir::PathSegment<'tcx>) {
737         let segment_data = self.save_ctxt.get_path_segment_data_with_id(segment, id);
738         if let Some(segment_data) = segment_data {
739             self.dumper.dump_ref(segment_data);
740         }
741     }
742
743     fn process_path(&mut self, id: hir::HirId, path: &hir::QPath<'tcx>) {
744         if self.span.filter_generated(path.span()) {
745             return;
746         }
747         self.dump_path_ref(id, path);
748
749         // Type arguments
750         let segments = match path {
751             hir::QPath::Resolved(ty, path) => {
752                 if let Some(ty) = ty {
753                     self.visit_ty(ty);
754                 }
755                 path.segments
756             }
757             hir::QPath::TypeRelative(ty, segment) => {
758                 self.visit_ty(ty);
759                 std::slice::from_ref(*segment)
760             }
761             hir::QPath::LangItem(..) => return,
762         };
763         for seg in segments {
764             if let Some(ref generic_args) = seg.args {
765                 for arg in generic_args.args {
766                     if let hir::GenericArg::Type(ref ty) = arg {
767                         self.visit_ty(ty);
768                     }
769                 }
770             }
771         }
772
773         if let hir::QPath::Resolved(_, path) = path {
774             self.write_sub_paths_truncated(path);
775         }
776     }
777
778     fn process_struct_lit(
779         &mut self,
780         ex: &'tcx hir::Expr<'tcx>,
781         path: &'tcx hir::QPath<'tcx>,
782         fields: &'tcx [hir::ExprField<'tcx>],
783         variant: &'tcx ty::VariantDef,
784         rest: Option<&'tcx hir::Expr<'tcx>>,
785     ) {
786         if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) {
787             if let hir::QPath::Resolved(_, path) = path {
788                 self.write_sub_paths_truncated(path);
789             }
790             down_cast_data!(struct_lit_data, RefData, ex.span);
791             if !generated_code(ex.span) {
792                 self.dumper.dump_ref(struct_lit_data);
793             }
794
795             for field in fields {
796                 if let Some(field_data) = self.save_ctxt.get_field_ref_data(field, variant) {
797                     self.dumper.dump_ref(field_data);
798                 }
799
800                 self.visit_expr(&field.expr)
801             }
802         }
803
804         if let Some(base) = rest {
805             self.visit_expr(&base);
806         }
807     }
808
809     fn process_method_call(
810         &mut self,
811         ex: &'tcx hir::Expr<'tcx>,
812         seg: &'tcx hir::PathSegment<'tcx>,
813         args: &'tcx [hir::Expr<'tcx>],
814     ) {
815         debug!("process_method_call {:?} {:?}", ex, ex.span);
816         if let Some(mcd) = self.save_ctxt.get_expr_data(ex) {
817             down_cast_data!(mcd, RefData, ex.span);
818             if !generated_code(ex.span) {
819                 self.dumper.dump_ref(mcd);
820             }
821         }
822
823         // Explicit types in the turbo-fish.
824         if let Some(generic_args) = seg.args {
825             for arg in generic_args.args {
826                 if let hir::GenericArg::Type(ty) = arg {
827                     self.visit_ty(&ty)
828                 };
829             }
830         }
831
832         // walk receiver and args
833         walk_list!(self, visit_expr, args);
834     }
835
836     fn process_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
837         match p.kind {
838             hir::PatKind::Struct(ref _path, fields, _) => {
839                 // FIXME do something with _path?
840                 let adt = match self.save_ctxt.typeck_results().node_type_opt(p.hir_id) {
841                     Some(ty) if ty.ty_adt_def().is_some() => ty.ty_adt_def().unwrap(),
842                     _ => {
843                         intravisit::walk_pat(self, p);
844                         return;
845                     }
846                 };
847                 let variant = adt.variant_of_res(self.save_ctxt.get_path_res(p.hir_id));
848
849                 for field in fields {
850                     if let Some(index) = self.tcx.find_field_index(field.ident, variant) {
851                         if !self.span.filter_generated(field.ident.span) {
852                             let span = self.span_from_span(field.ident.span);
853                             self.dumper.dump_ref(Ref {
854                                 kind: RefKind::Variable,
855                                 span,
856                                 ref_id: id_from_def_id(variant.fields[index].did),
857                             });
858                         }
859                     }
860                     self.visit_pat(&field.pat);
861                 }
862             }
863             _ => intravisit::walk_pat(self, p),
864         }
865     }
866
867     fn process_var_decl(&mut self, pat: &'tcx hir::Pat<'tcx>) {
868         // The pattern could declare multiple new vars,
869         // we must walk the pattern and collect them all.
870         let mut collector = PathCollector::new(self.tcx);
871         collector.visit_pat(&pat);
872         self.visit_pat(&pat);
873
874         // Process collected paths.
875         for (id, ident, _) in collector.collected_idents {
876             let res = self.save_ctxt.get_path_res(id);
877             match res {
878                 Res::Local(hir_id) => {
879                     let typ = self
880                         .save_ctxt
881                         .typeck_results()
882                         .node_type_opt(hir_id)
883                         .map(|t| t.to_string())
884                         .unwrap_or_default();
885
886                     // Rust uses the id of the pattern for var lookups, so we'll use it too.
887                     if !self.span.filter_generated(ident.span) {
888                         let qualname = format!("{}${}", ident.to_string(), hir_id);
889                         let id = id_from_hir_id(hir_id, &self.save_ctxt);
890                         let span = self.span_from_span(ident.span);
891
892                         self.dumper.dump_def(
893                             &Access { public: false, reachable: false },
894                             Def {
895                                 kind: DefKind::Local,
896                                 id,
897                                 span,
898                                 name: ident.to_string(),
899                                 qualname,
900                                 value: typ,
901                                 parent: None,
902                                 children: vec![],
903                                 decl_id: None,
904                                 docs: String::new(),
905                                 sig: None,
906                                 attributes: vec![],
907                             },
908                         );
909                     }
910                 }
911                 Res::Def(
912                     HirDefKind::Ctor(..)
913                     | HirDefKind::Const
914                     | HirDefKind::AssocConst
915                     | HirDefKind::Struct
916                     | HirDefKind::Variant
917                     | HirDefKind::TyAlias
918                     | HirDefKind::AssocTy,
919                     _,
920                 )
921                 | Res::SelfTy(..) => {
922                     self.dump_path_segment_ref(id, &hir::PathSegment::from_ident(ident));
923                 }
924                 def => {
925                     error!("unexpected definition kind when processing collected idents: {:?}", def)
926                 }
927             }
928         }
929
930         for (id, ref path) in collector.collected_paths {
931             self.process_path(id, path);
932         }
933     }
934
935     /// Extracts macro use and definition information from the AST node defined
936     /// by the given NodeId, using the expansion information from the node's
937     /// span.
938     ///
939     /// If the span is not macro-generated, do nothing, else use callee and
940     /// callsite spans to record macro definition and use data, using the
941     /// mac_uses and mac_defs sets to prevent multiples.
942     fn process_macro_use(&mut self, _span: Span) {
943         // FIXME if we're not dumping the defs (see below), there is no point
944         // dumping refs either.
945         // let source_span = span.source_callsite();
946         // if !self.macro_calls.insert(source_span) {
947         //     return;
948         // }
949
950         // let data = match self.save_ctxt.get_macro_use_data(span) {
951         //     None => return,
952         //     Some(data) => data,
953         // };
954
955         // self.dumper.macro_use(data);
956
957         // FIXME write the macro def
958         // let mut hasher = DefaultHasher::new();
959         // data.callee_span.hash(&mut hasher);
960         // let hash = hasher.finish();
961         // let qualname = format!("{}::{}", data.name, hash);
962         // Don't write macro definition for imported macros
963         // if !self.mac_defs.contains(&data.callee_span)
964         //     && !data.imported {
965         //     self.mac_defs.insert(data.callee_span);
966         //     if let Some(sub_span) = self.span.span_for_macro_def_name(data.callee_span) {
967         //         self.dumper.macro_data(MacroData {
968         //             span: sub_span,
969         //             name: data.name.clone(),
970         //             qualname: qualname.clone(),
971         //             // FIXME where do macro docs come from?
972         //             docs: String::new(),
973         //         }.lower(self.tcx));
974         //     }
975         // }
976     }
977
978     fn process_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>, trait_id: DefId) {
979         self.process_macro_use(trait_item.span);
980         let vis_span = trait_item.span.shrink_to_lo();
981         match trait_item.kind {
982             hir::TraitItemKind::Const(ref ty, body) => {
983                 let body = body.map(|b| &self.tcx.hir().body(b).value);
984                 let respan = respan(vis_span, hir::VisibilityKind::Public);
985                 let attrs = self.tcx.hir().attrs(trait_item.hir_id());
986                 self.process_assoc_const(
987                     trait_item.hir_id(),
988                     trait_item.ident,
989                     &ty,
990                     body,
991                     trait_id,
992                     &respan,
993                     attrs,
994                 );
995             }
996             hir::TraitItemKind::Fn(ref sig, ref trait_fn) => {
997                 let body =
998                     if let hir::TraitFn::Provided(body) = trait_fn { Some(*body) } else { None };
999                 let respan = respan(vis_span, hir::VisibilityKind::Public);
1000                 self.process_method(
1001                     sig,
1002                     body,
1003                     trait_item.hir_id(),
1004                     trait_item.ident,
1005                     &trait_item.generics,
1006                     &respan,
1007                     trait_item.span,
1008                 );
1009             }
1010             hir::TraitItemKind::Type(ref bounds, ref default_ty) => {
1011                 // FIXME do something with _bounds (for type refs)
1012                 let name = trait_item.ident.name.to_string();
1013                 let qualname =
1014                     format!("::{}", self.tcx.def_path_str(trait_item.def_id.to_def_id()));
1015
1016                 if !self.span.filter_generated(trait_item.ident.span) {
1017                     let span = self.span_from_span(trait_item.ident.span);
1018                     let id = id_from_def_id(trait_item.def_id.to_def_id());
1019                     let attrs = self.tcx.hir().attrs(trait_item.hir_id());
1020
1021                     self.dumper.dump_def(
1022                         &Access { public: true, reachable: true },
1023                         Def {
1024                             kind: DefKind::Type,
1025                             id,
1026                             span,
1027                             name,
1028                             qualname,
1029                             value: self.span.snippet(trait_item.span),
1030                             parent: Some(id_from_def_id(trait_id)),
1031                             children: vec![],
1032                             decl_id: None,
1033                             docs: self.save_ctxt.docs_for_attrs(attrs),
1034                             sig: sig::assoc_type_signature(
1035                                 trait_item.hir_id(),
1036                                 trait_item.ident,
1037                                 Some(bounds),
1038                                 default_ty.as_ref().map(|ty| &**ty),
1039                                 &self.save_ctxt,
1040                             ),
1041                             attributes: lower_attributes(attrs.to_vec(), &self.save_ctxt),
1042                         },
1043                     );
1044                 }
1045
1046                 if let Some(default_ty) = default_ty {
1047                     self.visit_ty(default_ty)
1048                 }
1049             }
1050         }
1051     }
1052
1053     fn process_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>, impl_id: DefId) {
1054         self.process_macro_use(impl_item.span);
1055         match impl_item.kind {
1056             hir::ImplItemKind::Const(ref ty, body) => {
1057                 let body = self.tcx.hir().body(body);
1058                 let attrs = self.tcx.hir().attrs(impl_item.hir_id());
1059                 self.process_assoc_const(
1060                     impl_item.hir_id(),
1061                     impl_item.ident,
1062                     &ty,
1063                     Some(&body.value),
1064                     impl_id,
1065                     &impl_item.vis,
1066                     attrs,
1067                 );
1068             }
1069             hir::ImplItemKind::Fn(ref sig, body) => {
1070                 self.process_method(
1071                     sig,
1072                     Some(body),
1073                     impl_item.hir_id(),
1074                     impl_item.ident,
1075                     &impl_item.generics,
1076                     &impl_item.vis,
1077                     impl_item.span,
1078                 );
1079             }
1080             hir::ImplItemKind::TyAlias(ref ty) => {
1081                 // FIXME: uses of the assoc type should ideally point to this
1082                 // 'def' and the name here should be a ref to the def in the
1083                 // trait.
1084                 self.visit_ty(ty)
1085             }
1086         }
1087     }
1088
1089     pub(crate) fn process_crate(&mut self, krate: &'tcx hir::Crate<'tcx>) {
1090         let id = hir::CRATE_HIR_ID;
1091         let qualname =
1092             format!("::{}", self.tcx.def_path_str(self.tcx.hir().local_def_id(id).to_def_id()));
1093
1094         let sm = self.tcx.sess.source_map();
1095         let filename = sm.span_to_filename(krate.item.inner);
1096         let data_id = id_from_hir_id(id, &self.save_ctxt);
1097         let children =
1098             krate.item.item_ids.iter().map(|i| id_from_def_id(i.def_id.to_def_id())).collect();
1099         let span = self.span_from_span(krate.item.inner);
1100         let attrs = self.tcx.hir().attrs(id);
1101
1102         self.dumper.dump_def(
1103             &Access { public: true, reachable: true },
1104             Def {
1105                 kind: DefKind::Mod,
1106                 id: data_id,
1107                 name: String::new(),
1108                 qualname,
1109                 span,
1110                 value: filename.prefer_remapped().to_string(),
1111                 children,
1112                 parent: None,
1113                 decl_id: None,
1114                 docs: self.save_ctxt.docs_for_attrs(attrs),
1115                 sig: None,
1116                 attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt),
1117             },
1118         );
1119         intravisit::walk_crate(self, krate);
1120     }
1121
1122     fn process_bounds(&mut self, bounds: hir::GenericBounds<'tcx>) {
1123         for bound in bounds {
1124             if let hir::GenericBound::Trait(ref trait_ref, _) = *bound {
1125                 self.process_path(
1126                     trait_ref.trait_ref.hir_ref_id,
1127                     &hir::QPath::Resolved(None, &trait_ref.trait_ref.path),
1128                 )
1129             }
1130         }
1131     }
1132 }
1133
1134 impl<'tcx> Visitor<'tcx> for DumpVisitor<'tcx> {
1135     type Map = Map<'tcx>;
1136
1137     fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
1138         intravisit::NestedVisitorMap::All(self.tcx.hir())
1139     }
1140
1141     fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
1142         self.process_macro_use(item.span);
1143         match item.kind {
1144             hir::ItemKind::Use(path, hir::UseKind::Single) => {
1145                 let sub_span = path.segments.last().unwrap().ident.span;
1146                 if !self.span.filter_generated(sub_span) {
1147                     let access = access_from!(self.save_ctxt, item, item.hir_id());
1148                     let ref_id = self.lookup_def_id(item.hir_id()).map(id_from_def_id);
1149                     let span = self.span_from_span(sub_span);
1150                     let parent =
1151                         self.save_ctxt.tcx.parent(item.def_id.to_def_id()).map(id_from_def_id);
1152                     self.dumper.import(
1153                         &access,
1154                         Import {
1155                             kind: ImportKind::Use,
1156                             ref_id,
1157                             span,
1158                             alias_span: None,
1159                             name: item.ident.to_string(),
1160                             value: String::new(),
1161                             parent,
1162                         },
1163                     );
1164                     self.write_sub_paths_truncated(&path);
1165                 }
1166             }
1167             hir::ItemKind::Use(path, hir::UseKind::Glob) => {
1168                 // Make a comma-separated list of names of imported modules.
1169                 let names = self.tcx.names_imported_by_glob_use(item.def_id);
1170                 let names: Vec<_> = names.iter().map(|n| n.to_string()).collect();
1171
1172                 // Otherwise it's a span with wrong macro expansion info, which
1173                 // we don't want to track anyway, since it's probably macro-internal `use`
1174                 if let Some(sub_span) = self.span.sub_span_of_star(item.span) {
1175                     if !self.span.filter_generated(item.span) {
1176                         let access = access_from!(self.save_ctxt, item, item.hir_id());
1177                         let span = self.span_from_span(sub_span);
1178                         let parent =
1179                             self.save_ctxt.tcx.parent(item.def_id.to_def_id()).map(id_from_def_id);
1180                         self.dumper.import(
1181                             &access,
1182                             Import {
1183                                 kind: ImportKind::GlobUse,
1184                                 ref_id: None,
1185                                 span,
1186                                 alias_span: None,
1187                                 name: "*".to_owned(),
1188                                 value: names.join(", "),
1189                                 parent,
1190                             },
1191                         );
1192                         self.write_sub_paths(&path);
1193                     }
1194                 }
1195             }
1196             hir::ItemKind::ExternCrate(_) => {
1197                 let name_span = item.ident.span;
1198                 if !self.span.filter_generated(name_span) {
1199                     let span = self.span_from_span(name_span);
1200                     let parent =
1201                         self.save_ctxt.tcx.parent(item.def_id.to_def_id()).map(id_from_def_id);
1202                     self.dumper.import(
1203                         &Access { public: false, reachable: false },
1204                         Import {
1205                             kind: ImportKind::ExternCrate,
1206                             ref_id: None,
1207                             span,
1208                             alias_span: None,
1209                             name: item.ident.to_string(),
1210                             value: String::new(),
1211                             parent,
1212                         },
1213                     );
1214                 }
1215             }
1216             hir::ItemKind::Fn(ref sig, ref ty_params, body) => {
1217                 self.process_fn(item, sig.decl, &sig.header, ty_params, body)
1218             }
1219             hir::ItemKind::Static(ref typ, _, body) => {
1220                 let body = self.tcx.hir().body(body);
1221                 self.process_static_or_const_item(item, typ, &body.value)
1222             }
1223             hir::ItemKind::Const(ref typ, body) => {
1224                 let body = self.tcx.hir().body(body);
1225                 self.process_static_or_const_item(item, typ, &body.value)
1226             }
1227             hir::ItemKind::Struct(ref def, ref ty_params)
1228             | hir::ItemKind::Union(ref def, ref ty_params) => {
1229                 self.process_struct(item, def, ty_params)
1230             }
1231             hir::ItemKind::Enum(ref def, ref ty_params) => self.process_enum(item, def, ty_params),
1232             hir::ItemKind::Impl(ref impl_) => self.process_impl(item, impl_),
1233             hir::ItemKind::Trait(_, _, ref generics, ref trait_refs, methods) => {
1234                 self.process_trait(item, generics, trait_refs, methods)
1235             }
1236             hir::ItemKind::Mod(ref m) => {
1237                 self.process_mod(item);
1238                 intravisit::walk_mod(self, m, item.hir_id());
1239             }
1240             hir::ItemKind::TyAlias(ty, ref generics) => {
1241                 let qualname = format!("::{}", self.tcx.def_path_str(item.def_id.to_def_id()));
1242                 let value = ty_to_string(&ty);
1243                 if !self.span.filter_generated(item.ident.span) {
1244                     let span = self.span_from_span(item.ident.span);
1245                     let id = id_from_def_id(item.def_id.to_def_id());
1246                     let attrs = self.tcx.hir().attrs(item.hir_id());
1247
1248                     self.dumper.dump_def(
1249                         &access_from!(self.save_ctxt, item, item.hir_id()),
1250                         Def {
1251                             kind: DefKind::Type,
1252                             id,
1253                             span,
1254                             name: item.ident.to_string(),
1255                             qualname: qualname.clone(),
1256                             value,
1257                             parent: None,
1258                             children: vec![],
1259                             decl_id: None,
1260                             docs: self.save_ctxt.docs_for_attrs(attrs),
1261                             sig: sig::item_signature(item, &self.save_ctxt),
1262                             attributes: lower_attributes(attrs.to_vec(), &self.save_ctxt),
1263                         },
1264                     );
1265                 }
1266
1267                 self.visit_ty(ty);
1268                 self.process_generic_params(generics, &qualname, item.hir_id());
1269             }
1270             _ => intravisit::walk_item(self, item),
1271         }
1272     }
1273
1274     fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
1275         for param in generics.params {
1276             match param.kind {
1277                 hir::GenericParamKind::Lifetime { .. } => {}
1278                 hir::GenericParamKind::Type { ref default, .. } => {
1279                     self.process_bounds(param.bounds);
1280                     if let Some(ref ty) = default {
1281                         self.visit_ty(ty);
1282                     }
1283                 }
1284                 hir::GenericParamKind::Const { ref ty, ref default } => {
1285                     self.process_bounds(param.bounds);
1286                     self.visit_ty(ty);
1287                     if let Some(default) = default {
1288                         self.visit_anon_const(default);
1289                     }
1290                 }
1291             }
1292         }
1293         for pred in generics.where_clause.predicates {
1294             if let hir::WherePredicate::BoundPredicate(ref wbp) = *pred {
1295                 self.process_bounds(wbp.bounds);
1296                 self.visit_ty(wbp.bounded_ty);
1297             }
1298         }
1299     }
1300
1301     fn visit_ty(&mut self, t: &'tcx hir::Ty<'tcx>) {
1302         self.process_macro_use(t.span);
1303         match t.kind {
1304             hir::TyKind::Path(ref path) => {
1305                 if generated_code(t.span) {
1306                     return;
1307                 }
1308
1309                 if let Some(id) = self.lookup_def_id(t.hir_id) {
1310                     let sub_span = path.last_segment_span();
1311                     let span = self.span_from_span(sub_span);
1312                     self.dumper.dump_ref(Ref {
1313                         kind: RefKind::Type,
1314                         span,
1315                         ref_id: id_from_def_id(id),
1316                     });
1317                 }
1318
1319                 if let hir::QPath::Resolved(_, path) = path {
1320                     self.write_sub_paths_truncated(path);
1321                 }
1322                 intravisit::walk_qpath(self, path, t.hir_id, t.span);
1323             }
1324             hir::TyKind::Array(ref ty, ref anon_const) => {
1325                 self.visit_ty(ty);
1326                 let map = self.tcx.hir();
1327                 self.nest_typeck_results(self.tcx.hir().local_def_id(anon_const.hir_id), |v| {
1328                     v.visit_expr(&map.body(anon_const.body).value)
1329                 });
1330             }
1331             hir::TyKind::OpaqueDef(item_id, _) => {
1332                 let item = self.tcx.hir().item(item_id);
1333                 self.nest_typeck_results(item_id.def_id, |v| v.visit_item(item));
1334             }
1335             _ => intravisit::walk_ty(self, t),
1336         }
1337     }
1338
1339     fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
1340         debug!("visit_expr {:?}", ex.kind);
1341         self.process_macro_use(ex.span);
1342         match ex.kind {
1343             hir::ExprKind::Struct(ref path, ref fields, ref rest) => {
1344                 let hir_expr = self.save_ctxt.tcx.hir().expect_expr(ex.hir_id);
1345                 let adt = match self.save_ctxt.typeck_results().expr_ty_opt(&hir_expr) {
1346                     Some(ty) if ty.ty_adt_def().is_some() => ty.ty_adt_def().unwrap(),
1347                     _ => {
1348                         intravisit::walk_expr(self, ex);
1349                         return;
1350                     }
1351                 };
1352                 let res = self.save_ctxt.get_path_res(hir_expr.hir_id);
1353                 self.process_struct_lit(ex, path, fields, adt.variant_of_res(res), *rest)
1354             }
1355             hir::ExprKind::MethodCall(ref seg, _, args, _) => {
1356                 self.process_method_call(ex, seg, args)
1357             }
1358             hir::ExprKind::Field(ref sub_ex, _) => {
1359                 self.visit_expr(&sub_ex);
1360
1361                 if let Some(field_data) = self.save_ctxt.get_expr_data(ex) {
1362                     down_cast_data!(field_data, RefData, ex.span);
1363                     if !generated_code(ex.span) {
1364                         self.dumper.dump_ref(field_data);
1365                     }
1366                 }
1367             }
1368             hir::ExprKind::Closure(_, ref decl, body, _fn_decl_span, _) => {
1369                 let id = format!("${}", ex.hir_id);
1370
1371                 // walk arg and return types
1372                 for ty in decl.inputs {
1373                     self.visit_ty(ty);
1374                 }
1375
1376                 if let hir::FnRetTy::Return(ref ret_ty) = decl.output {
1377                     self.visit_ty(ret_ty);
1378                 }
1379
1380                 // walk the body
1381                 let map = self.tcx.hir();
1382                 self.nest_typeck_results(self.tcx.hir().local_def_id(ex.hir_id), |v| {
1383                     let body = map.body(body);
1384                     v.process_formals(body.params, &id);
1385                     v.visit_expr(&body.value)
1386                 });
1387             }
1388             hir::ExprKind::Repeat(ref expr, ref anon_const) => {
1389                 self.visit_expr(expr);
1390                 let map = self.tcx.hir();
1391                 self.nest_typeck_results(self.tcx.hir().local_def_id(anon_const.hir_id), |v| {
1392                     v.visit_expr(&map.body(anon_const.body).value)
1393                 });
1394             }
1395             // In particular, we take this branch for call and path expressions,
1396             // where we'll index the idents involved just by continuing to walk.
1397             _ => intravisit::walk_expr(self, ex),
1398         }
1399     }
1400
1401     fn visit_pat(&mut self, p: &'tcx hir::Pat<'tcx>) {
1402         self.process_macro_use(p.span);
1403         self.process_pat(p);
1404     }
1405
1406     fn visit_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) {
1407         self.process_var_decl(&arm.pat);
1408         if let Some(hir::Guard::If(expr)) = &arm.guard {
1409             self.visit_expr(expr);
1410         }
1411         self.visit_expr(&arm.body);
1412     }
1413
1414     fn visit_qpath(&mut self, path: &'tcx hir::QPath<'tcx>, id: hir::HirId, _: Span) {
1415         self.process_path(id, path);
1416     }
1417
1418     fn visit_stmt(&mut self, s: &'tcx hir::Stmt<'tcx>) {
1419         self.process_macro_use(s.span);
1420         intravisit::walk_stmt(self, s)
1421     }
1422
1423     fn visit_local(&mut self, l: &'tcx hir::Local<'tcx>) {
1424         self.process_macro_use(l.span);
1425         self.process_var_decl(&l.pat);
1426
1427         // Just walk the initialiser and type (don't want to walk the pattern again).
1428         walk_list!(self, visit_ty, &l.ty);
1429         walk_list!(self, visit_expr, &l.init);
1430     }
1431
1432     fn visit_foreign_item(&mut self, item: &'tcx hir::ForeignItem<'tcx>) {
1433         let access = access_from!(self.save_ctxt, item, item.hir_id());
1434
1435         match item.kind {
1436             hir::ForeignItemKind::Fn(decl, _, ref generics) => {
1437                 if let Some(fn_data) = self.save_ctxt.get_extern_item_data(item) {
1438                     down_cast_data!(fn_data, DefData, item.span);
1439
1440                     self.process_generic_params(generics, &fn_data.qualname, item.hir_id());
1441                     self.dumper.dump_def(&access, fn_data);
1442                 }
1443
1444                 for ty in decl.inputs {
1445                     self.visit_ty(ty);
1446                 }
1447
1448                 if let hir::FnRetTy::Return(ref ret_ty) = decl.output {
1449                     self.visit_ty(ret_ty);
1450                 }
1451             }
1452             hir::ForeignItemKind::Static(ref ty, _) => {
1453                 if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
1454                     down_cast_data!(var_data, DefData, item.span);
1455                     self.dumper.dump_def(&access, var_data);
1456                 }
1457
1458                 self.visit_ty(ty);
1459             }
1460             hir::ForeignItemKind::Type => {
1461                 if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) {
1462                     down_cast_data!(var_data, DefData, item.span);
1463                     self.dumper.dump_def(&access, var_data);
1464                 }
1465             }
1466         }
1467     }
1468 }