From 95937990c5e772721e83c603dcca7c5aed8cdd0e Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Wed, 8 Nov 2017 09:46:06 +1300 Subject: [PATCH 1/3] save-analysis: fix regression from #45709 closes https://github.com/nrc/rls-analysis/issues/117 --- src/librustc_save_analysis/dump_visitor.rs | 27 +++++++++++++--------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index d190ae1431f..fdd7a8e8d74 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -785,21 +785,19 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { } } + fn dump_path_ref(&mut self, id: NodeId, path: &ast::Path) { + let path_data = self.save_ctxt.get_path_data(id, path); + if let Some(path_data) = path_data { + self.dumper.dump_ref(path_data); + } + } + fn process_path(&mut self, id: NodeId, path: &'l ast::Path) { debug!("process_path {:?}", path); - let path_data = self.save_ctxt.get_path_data(id, path); - if generated_code(path.span) && path_data.is_none() { + if generated_code(path.span) { return; } - - let path_data = match path_data { - Some(pd) => pd, - None => { - return; - } - }; - - self.dumper.dump_ref(path_data); + self.dump_path_ref(id, path); // Type parameters for seg in &path.segments { @@ -1508,6 +1506,13 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc }); } } + HirDef::StructCtor(..) | HirDef::VariantCtor(..) | + HirDef::Const(..) | HirDef::AssociatedConst(..) | + HirDef::Struct(..) | HirDef::Variant(..) | + HirDef::TyAlias(..) | HirDef::AssociatedTy(..) | + HirDef::SelfTy(..) => { + self.dump_path_ref(id, &ast::Path::from_ident(sp, i)); + } def => error!("unexpected definition kind when processing collected idents: {:?}", def), } From 1328c29d4ae91fe0f932a6036e38374740179213 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Wed, 8 Nov 2017 10:08:19 +1300 Subject: [PATCH 2/3] save-analysis: fix bug with method ids This just handles a missing entry, doesn't try to recover, because I couldn't actually find a test case. cc https://github.com/rust-lang-nursery/rls/issues/558 --- src/librustc_save_analysis/lib.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index edb51ae59e1..5d97dbf728b 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -548,7 +548,13 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { } ast::ExprKind::MethodCall(ref seg, ..) => { let expr_hir_id = self.tcx.hir.definitions().node_to_hir_id(expr.id); - let method_id = self.tables.type_dependent_defs()[expr_hir_id].def_id(); + let method_id = match self.tables.type_dependent_defs().get(expr_hir_id) { + Some(id) => id.def_id(), + None => { + debug!("Could not resolve method id for {:?}", expr); + return None; + } + }; let (def_id, decl_id) = match self.tcx.associated_item(method_id).container { ty::ImplContainer(_) => (Some(method_id), None), ty::TraitContainer(_) => (None, Some(method_id)), From b98290cb1f1cb22abda0dd97567d9aad95ae3078 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Wed, 8 Nov 2017 10:43:05 +1300 Subject: [PATCH 3/3] save-analysis: run rustfmt --- src/librustc_save_analysis/dump_visitor.rs | 920 ++++++++++++--------- src/librustc_save_analysis/json_dumper.rs | 13 +- src/librustc_save_analysis/lib.rs | 416 ++++++---- src/librustc_save_analysis/sig.rs | 325 ++++---- src/librustc_save_analysis/span_utils.rs | 38 +- 5 files changed, 945 insertions(+), 767 deletions(-) diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index fdd7a8e8d74..272b1cb8d35 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -32,22 +32,22 @@ use rustc_data_structures::fx::FxHashSet; use std::path::Path; -use syntax::ast::{self, NodeId, PatKind, Attribute, CRATE_NODE_ID}; +use syntax::ast::{self, Attribute, NodeId, PatKind, CRATE_NODE_ID}; use syntax::parse::token; use syntax::symbol::keywords; use syntax::visit::{self, Visitor}; -use syntax::print::pprust::{path_to_string, ty_to_string, bounds_to_string, generics_to_string}; +use syntax::print::pprust::{bounds_to_string, generics_to_string, path_to_string, ty_to_string}; use syntax::ptr::P; use syntax::codemap::Spanned; use syntax_pos::*; -use {escape, generated_code, SaveContext, PathCollector, lower_attributes}; -use json_dumper::{JsonDumper, DumpOutput}; +use {escape, generated_code, lower_attributes, PathCollector, SaveContext}; +use json_dumper::{DumpOutput, JsonDumper}; use span_utils::SpanUtils; use sig; -use rls_data::{CratePreludeData, GlobalCrateId, Import, ImportKind, SpanData, - Ref, RefKind, Def, DefKind, Relation, RelationKind}; +use rls_data::{CratePreludeData, Def, DefKind, GlobalCrateId, Import, ImportKind, Ref, RefKind, + Relation, RelationKind, SpanData}; macro_rules! down_cast_data { ($id:ident, $kind:ident, $sp:expr) => { @@ -77,9 +77,10 @@ pub struct DumpVisitor<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> { } impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { - pub fn new(save_ctxt: SaveContext<'l, 'tcx>, - dumper: &'ll mut JsonDumper) - -> DumpVisitor<'l, 'tcx, 'll, O> { + pub fn new( + save_ctxt: SaveContext<'l, 'tcx>, + dumper: &'ll mut JsonDumper, + ) -> DumpVisitor<'l, 'tcx, 'll, O> { let span_utils = SpanUtils::new(&save_ctxt.tcx.sess); DumpVisitor { tcx: save_ctxt.tcx, @@ -93,7 +94,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { } fn nest_scope(&mut self, scope_id: NodeId, f: F) - where F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, O>) + where + F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, O>), { let parent_scope = self.cur_scope; self.cur_scope = scope_id; @@ -102,7 +104,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { } fn nest_tables(&mut self, item_id: NodeId, f: F) - where F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, O>) + where + F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, O>), { let item_def_id = self.tcx.hir.local_def_id(item_id); if self.tcx.has_typeck_tables(item_def_id) { @@ -133,8 +136,11 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { let data = CratePreludeData { crate_id: GlobalCrateId { name: name.into(), - disambiguator: self.tcx.sess.local_crate_disambiguator() - .to_fingerprint().as_value(), + disambiguator: self.tcx + .sess + .local_crate_disambiguator() + .to_fingerprint() + .as_value(), }, crate_root: crate_root.unwrap_or("".to_owned()), external_crates: self.save_ctxt.get_external_crates(), @@ -210,10 +216,10 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { if len <= 1 { return; } - let sub_paths = &sub_paths[.. (len-1)]; + let sub_paths = &sub_paths[..(len - 1)]; // write the trait part of the sub-path - let (ref span, _) = sub_paths[len-2]; + let (ref span, _) = sub_paths[len - 2]; let span = self.span_from_span(*span); self.dumper.dump_ref(Ref { kind: RefKind::Type, @@ -225,7 +231,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { if len <= 2 { return; } - let sub_paths = &sub_paths[..len-2]; + let sub_paths = &sub_paths[..len - 2]; for &(ref span, _) in sub_paths { let span = self.span_from_span(*span); self.dumper.dump_ref(Ref { @@ -243,11 +249,13 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { } } - fn process_def_kind(&mut self, - ref_id: NodeId, - span: Span, - sub_span: Option, - def_id: DefId) { + fn process_def_kind( + &mut self, + ref_id: NodeId, + span: Span, + sub_span: Option, + def_id: DefId, + ) { if self.span.filter_generated(sub_span, span) { return; } @@ -309,9 +317,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { HirDef::PrimTy(_) | HirDef::GlobalAsm(_) | HirDef::Err => { - span_bug!(span, - "process_def_kind for unexpected item: {:?}", - def); + span_bug!(span, "process_def_kind for unexpected item: {:?}", def); } } } @@ -334,49 +340,55 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { let id = ::id_from_node_id(id, &self.save_ctxt); let span = self.span_from_span(sub_span.expect("No span found for variable")); - self.dumper.dump_def(false, Def { - kind: DefKind::Local, - id, - span, - name: i.to_string(), - qualname: format!("{}::{}", qualname, i.to_string()), - value: typ, - parent: None, - children: vec![], - decl_id: None, - docs: String::new(), - sig: None, - attributes:vec![], - }); + self.dumper.dump_def( + false, + Def { + kind: DefKind::Local, + id, + span, + name: i.to_string(), + qualname: format!("{}::{}", qualname, i.to_string()), + value: typ, + parent: None, + children: vec![], + decl_id: None, + docs: String::new(), + sig: None, + attributes: vec![], + }, + ); } } } } - fn process_method(&mut self, - sig: &'l ast::MethodSig, - body: Option<&'l ast::Block>, - id: ast::NodeId, - name: ast::Ident, - generics: &'l ast::Generics, - vis: ast::Visibility, - span: Span) { + fn process_method( + &mut self, + sig: &'l ast::MethodSig, + body: Option<&'l ast::Block>, + id: ast::NodeId, + name: ast::Ident, + generics: &'l ast::Generics, + vis: ast::Visibility, + span: Span, + ) { debug!("process_method: {}:{}", id, name); if let Some(mut method_data) = self.save_ctxt.get_method_data(id, name.name, span) { - let sig_str = ::make_signature(&sig.decl, &generics); if body.is_some() { - self.nest_tables(id, |v| { - v.process_formals(&sig.decl.inputs, &method_data.qualname) - }); + self.nest_tables( + id, + |v| v.process_formals(&sig.decl.inputs, &method_data.qualname), + ); } self.process_generic_params(&generics, span, &method_data.qualname, id); method_data.value = sig_str; method_data.sig = sig::method_signature(id, name, generics, sig, &self.save_ctxt); - self.dumper.dump_def(vis == ast::Visibility::Public, method_data); + self.dumper + .dump_def(vis == ast::Visibility::Public, method_data); } // walk arg and return types @@ -397,57 +409,66 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { fn process_struct_field_def(&mut self, field: &ast::StructField, parent_id: NodeId) { let field_data = self.save_ctxt.get_field_data(field, parent_id); if let Some(field_data) = field_data { - self.dumper.dump_def(field.vis == ast::Visibility::Public, field_data); + self.dumper + .dump_def(field.vis == ast::Visibility::Public, field_data); } } // Dump generic params bindings, then visit_generics - fn process_generic_params(&mut self, - generics: &'l ast::Generics, - full_span: Span, - prefix: &str, - id: NodeId) { + fn process_generic_params( + &mut self, + generics: &'l ast::Generics, + full_span: Span, + prefix: &str, + id: NodeId, + ) { for param in &generics.ty_params { let param_ss = param.span; let name = escape(self.span.snippet(param_ss)); // Append $id to name to make sure each one is unique - let qualname = format!("{}::{}${}", - prefix, - name, - id); + let qualname = format!("{}::{}${}", prefix, name, id); if !self.span.filter_generated(Some(param_ss), full_span) { let id = ::id_from_node_id(param.id, &self.save_ctxt); let span = self.span_from_span(param_ss); - self.dumper.dump_def(false, Def { - kind: DefKind::Type, - id, - span, - name, - qualname, - value: String::new(), - parent: None, - children: vec![], - decl_id: None, - docs: String::new(), - sig: None, - attributes: vec![], - }); + self.dumper.dump_def( + false, + Def { + kind: DefKind::Type, + id, + span, + name, + qualname, + value: String::new(), + parent: None, + children: vec![], + decl_id: None, + docs: String::new(), + sig: None, + attributes: vec![], + }, + ); } } self.visit_generics(generics); } - fn process_fn(&mut self, - item: &'l ast::Item, - decl: &'l ast::FnDecl, - ty_params: &'l ast::Generics, - body: &'l ast::Block) { + fn process_fn( + &mut self, + item: &'l ast::Item, + decl: &'l ast::FnDecl, + ty_params: &'l ast::Generics, + body: &'l ast::Block, + ) { if let Some(fn_data) = self.save_ctxt.get_item_data(item) { down_cast_data!(fn_data, DefData, item.span); - self.nest_tables(item.id, |v| v.process_formals(&decl.inputs, &fn_data.qualname)); + self.nest_tables( + item.id, + |v| v.process_formals(&decl.inputs, &fn_data.qualname), + ); self.process_generic_params(ty_params, item.span, &fn_data.qualname, item.id); - self.dumper.dump_def(item.vis == ast::Visibility::Public, fn_data); + self.dumper + .dump_def(item.vis == ast::Visibility::Public, fn_data); } for arg in &decl.inputs { @@ -461,29 +482,34 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { self.nest_tables(item.id, |v| v.nest_scope(item.id, |v| v.visit_block(&body))); } - fn process_static_or_const_item(&mut self, - item: &'l ast::Item, - typ: &'l ast::Ty, - expr: &'l ast::Expr) { + fn process_static_or_const_item( + &mut self, + item: &'l ast::Item, + typ: &'l ast::Ty, + expr: &'l ast::Expr, + ) { self.nest_tables(item.id, |v| { if let Some(var_data) = v.save_ctxt.get_item_data(item) { down_cast_data!(var_data, DefData, item.span); - v.dumper.dump_def(item.vis == ast::Visibility::Public, var_data); + v.dumper + .dump_def(item.vis == ast::Visibility::Public, var_data); } v.visit_ty(&typ); v.visit_expr(expr); }); } - fn process_assoc_const(&mut self, - id: ast::NodeId, - name: ast::Name, - span: Span, - typ: &'l ast::Ty, - expr: Option<&'l ast::Expr>, - parent_id: DefId, - vis: ast::Visibility, - attrs: &'l [Attribute]) { + fn process_assoc_const( + &mut self, + id: ast::NodeId, + name: ast::Name, + span: Span, + typ: &'l ast::Ty, + expr: Option<&'l ast::Expr>, + parent_id: DefId, + vis: ast::Visibility, + attrs: &'l [Attribute], + ) { let qualname = format!("::{}", self.tcx.node_path_str(id)); let sub_span = self.span.sub_span_after_keyword(span, keywords::Const); @@ -493,20 +519,23 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { let id = ::id_from_node_id(id, &self.save_ctxt); let span = self.span_from_span(sub_span.expect("No span found for variable")); - self.dumper.dump_def(vis == ast::Visibility::Public, Def { - kind: DefKind::Const, - id, - span, - name: name.to_string(), - qualname, - value: ty_to_string(&typ), - parent: Some(::id_from_def_id(parent_id)), - children: vec![], - decl_id: None, - docs: self.save_ctxt.docs_for_attrs(attrs), - sig, - attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt), - }); + self.dumper.dump_def( + vis == ast::Visibility::Public, + Def { + kind: DefKind::Const, + id, + span, + name: name.to_string(), + qualname, + value: ty_to_string(&typ), + parent: Some(::id_from_def_id(parent_id)), + children: vec![], + decl_id: None, + docs: self.save_ctxt.docs_for_attrs(attrs), + sig, + attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt), + }, + ); } // walk type and init value @@ -517,10 +546,12 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { } // FIXME tuple structs should generate tuple-specific data. - fn process_struct(&mut self, - item: &'l ast::Item, - def: &'l ast::VariantData, - ty_params: &'l ast::Generics) { + fn process_struct( + &mut self, + item: &'l ast::Item, + def: &'l ast::VariantData, + ty_params: &'l ast::Generics, + ) { debug!("process_struct {:?} {:?}", item, item.span); let name = item.ident.to_string(); let qualname = format!("::{}", self.tcx.node_path_str(item.id)); @@ -540,36 +571,47 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { .iter() .enumerate() .filter_map(|(i, f)| { - if include_priv_fields || f.vis == ast::Visibility::Public { - f.ident.map(|i| i.to_string()).or_else(|| Some(i.to_string())) - } else { - None - } + if include_priv_fields || f.vis == ast::Visibility::Public { + f.ident + .map(|i| i.to_string()) + .or_else(|| Some(i.to_string())) + } else { + None + } }) .collect::>() .join(", "); let value = format!("{} {{ {} }}", name, fields_str); - (value, fields.iter().map(|f| ::id_from_node_id(f.id, &self.save_ctxt)).collect()) + ( + value, + fields + .iter() + .map(|f| ::id_from_node_id(f.id, &self.save_ctxt)) + .collect(), + ) } - _ => (String::new(), vec![]) + _ => (String::new(), vec![]), }; if !self.span.filter_generated(sub_span, item.span) { let span = self.span_from_span(sub_span.expect("No span found for struct")); - self.dumper.dump_def(item.vis == ast::Visibility::Public, Def { - kind, - id: ::id_from_node_id(item.id, &self.save_ctxt), - span, - name, - qualname: qualname.clone(), - value, - parent: None, - children: fields, - decl_id: None, - docs: self.save_ctxt.docs_for_attrs(&item.attrs), - sig: sig::item_signature(item, &self.save_ctxt), - attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt), - }); + self.dumper.dump_def( + item.vis == ast::Visibility::Public, + Def { + kind, + id: ::id_from_node_id(item.id, &self.save_ctxt), + span, + name, + qualname: qualname.clone(), + value, + parent: None, + children: fields, + decl_id: None, + docs: self.save_ctxt.docs_for_attrs(&item.attrs), + sig: sig::item_signature(item, &self.save_ctxt), + attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt), + }, + ); } for field in def.fields() { @@ -580,10 +622,12 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { self.process_generic_params(ty_params, item.span, &qualname, item.id); } - fn process_enum(&mut self, - item: &'l ast::Item, - enum_definition: &'l ast::EnumDef, - ty_params: &'l ast::Generics) { + fn process_enum( + &mut self, + item: &'l ast::Item, + enum_definition: &'l ast::EnumDef, + ty_params: &'l ast::Generics, + ) { let enum_data = self.save_ctxt.get_item_data(item); let enum_data = match enum_data { None => return, @@ -600,34 +644,41 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { match variant.node.data { ast::VariantData::Struct(ref fields, _) => { let sub_span = self.span.span_for_first_ident(variant.span); - let fields_str = fields.iter() - .enumerate() - .map(|(i, f)| f.ident.map(|i| i.to_string()) - .unwrap_or(i.to_string())) - .collect::>() - .join(", "); + let fields_str = fields + .iter() + .enumerate() + .map(|(i, f)| { + f.ident.map(|i| i.to_string()).unwrap_or(i.to_string()) + }) + .collect::>() + .join(", "); let value = format!("{}::{} {{ {} }}", enum_data.name, name, fields_str); if !self.span.filter_generated(sub_span, variant.span) { - let span = self.span_from_span( - sub_span.expect("No span found for struct variant")); + let span = self + .span_from_span(sub_span.expect("No span found for struct variant")); let id = ::id_from_node_id(variant.node.data.id(), &self.save_ctxt); let parent = Some(::id_from_node_id(item.id, &self.save_ctxt)); - self.dumper.dump_def(item.vis == ast::Visibility::Public, Def { - kind: DefKind::StructVariant, - id, - span, - name, - qualname, - value, - parent, - children: vec![], - decl_id: None, - docs: self.save_ctxt.docs_for_attrs(&variant.node.attrs), - sig: sig::variant_signature(variant, &self.save_ctxt), - attributes: lower_attributes(variant.node.attrs.clone(), - &self.save_ctxt), - }); + self.dumper.dump_def( + item.vis == ast::Visibility::Public, + Def { + kind: DefKind::StructVariant, + id, + span, + name, + qualname, + value, + parent, + children: vec![], + decl_id: None, + docs: self.save_ctxt.docs_for_attrs(&variant.node.attrs), + sig: sig::variant_signature(variant, &self.save_ctxt), + attributes: lower_attributes( + variant.node.attrs.clone(), + &self.save_ctxt, + ), + }, + ); } } ref v => { @@ -635,10 +686,11 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { let mut value = format!("{}::{}", enum_data.name, name); if let &ast::VariantData::Tuple(ref fields, _) = v { value.push('('); - value.push_str(&fields.iter() - .map(|f| ty_to_string(&f.ty)) - .collect::>() - .join(", ")); + value.push_str(&fields + .iter() + .map(|f| ty_to_string(&f.ty)) + .collect::>() + .join(", ")); value.push(')'); } if !self.span.filter_generated(sub_span, variant.span) { @@ -647,21 +699,26 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { let id = ::id_from_node_id(variant.node.data.id(), &self.save_ctxt); let parent = Some(::id_from_node_id(item.id, &self.save_ctxt)); - self.dumper.dump_def(item.vis == ast::Visibility::Public, Def { - kind: DefKind::TupleVariant, - id, - span, - name, - qualname, - value, - parent, - children: vec![], - decl_id: None, - docs: self.save_ctxt.docs_for_attrs(&variant.node.attrs), - sig: sig::variant_signature(variant, &self.save_ctxt), - attributes: lower_attributes(variant.node.attrs.clone(), - &self.save_ctxt), - }); + self.dumper.dump_def( + item.vis == ast::Visibility::Public, + Def { + kind: DefKind::TupleVariant, + id, + span, + name, + qualname, + value, + parent, + children: vec![], + decl_id: None, + docs: self.save_ctxt.docs_for_attrs(&variant.node.attrs), + sig: sig::variant_signature(variant, &self.save_ctxt), + attributes: lower_attributes( + variant.node.attrs.clone(), + &self.save_ctxt, + ), + }, + ); } } } @@ -673,15 +730,18 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { } } self.process_generic_params(ty_params, item.span, &enum_data.qualname, item.id); - self.dumper.dump_def(item.vis == ast::Visibility::Public, enum_data); + self.dumper + .dump_def(item.vis == ast::Visibility::Public, enum_data); } - fn process_impl(&mut self, - item: &'l ast::Item, - type_parameters: &'l ast::Generics, - trait_ref: &'l Option, - typ: &'l ast::Ty, - impl_items: &'l [ast::ImplItem]) { + fn process_impl( + &mut self, + item: &'l ast::Item, + type_parameters: &'l ast::Generics, + trait_ref: &'l Option, + typ: &'l ast::Ty, + impl_items: &'l [ast::ImplItem], + ) { if let Some(impl_data) = self.save_ctxt.get_item_data(item) { down_cast_data!(impl_data, RelationData, item.span); self.dumper.dump_relation(impl_data); @@ -697,11 +757,13 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { } } - fn process_trait(&mut self, - item: &'l ast::Item, - generics: &'l ast::Generics, - trait_refs: &'l ast::TyParamBounds, - methods: &'l [ast::TraitItem]) { + fn process_trait( + &mut self, + item: &'l ast::Item, + generics: &'l ast::Generics, + trait_refs: &'l ast::TyParamBounds, + methods: &'l [ast::TraitItem], + ) { let name = item.ident.to_string(); let qualname = format!("::{}", self.tcx.node_path_str(item.id)); let mut val = name.clone(); @@ -716,30 +778,33 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { if !self.span.filter_generated(sub_span, item.span) { let id = ::id_from_node_id(item.id, &self.save_ctxt); let span = self.span_from_span(sub_span.expect("No span found for trait")); - let children = - methods.iter().map(|i| ::id_from_node_id(i.id, &self.save_ctxt)).collect(); - self.dumper.dump_def(item.vis == ast::Visibility::Public, Def { - kind: DefKind::Trait, - id, - span, - name, - qualname: qualname.clone(), - value: val, - parent: None, - children, - decl_id: None, - docs: self.save_ctxt.docs_for_attrs(&item.attrs), - sig: sig::item_signature(item, &self.save_ctxt), - attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt), - }); + let children = methods + .iter() + .map(|i| ::id_from_node_id(i.id, &self.save_ctxt)) + .collect(); + self.dumper.dump_def( + item.vis == ast::Visibility::Public, + Def { + kind: DefKind::Trait, + id, + span, + name, + qualname: qualname.clone(), + value: val, + parent: None, + children, + decl_id: None, + docs: self.save_ctxt.docs_for_attrs(&item.attrs), + sig: sig::item_signature(item, &self.save_ctxt), + attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt), + }, + ); } // super-traits for super_bound in trait_refs.iter() { let trait_ref = match *super_bound { - ast::TraitTyParamBound(ref trait_ref, _) => { - trait_ref - } + ast::TraitTyParamBound(ref trait_ref, _) => trait_ref, ast::RegionTyParamBound(..) => { continue; } @@ -781,7 +846,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { fn process_mod(&mut self, item: &ast::Item) { if let Some(mod_data) = self.save_ctxt.get_item_data(item) { down_cast_data!(mod_data, DefData, item.span); - self.dumper.dump_def(item.vis == ast::Visibility::Public, mod_data); + self.dumper + .dump_def(item.vis == ast::Visibility::Public, mod_data); } } @@ -803,11 +869,9 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { for seg in &path.segments { if let Some(ref params) = seg.parameters { match **params { - ast::PathParameters::AngleBracketed(ref data) => { - for t in &data.types { - self.visit_ty(t); - } - } + ast::PathParameters::AngleBracketed(ref data) => for t in &data.types { + self.visit_ty(t); + }, ast::PathParameters::Parenthesized(ref data) => { for t in &data.inputs { self.visit_ty(t); @@ -845,12 +909,14 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { } } - fn process_struct_lit(&mut self, - ex: &'l ast::Expr, - path: &'l ast::Path, - fields: &'l [ast::Field], - variant: &'l ty::VariantDef, - base: &'l Option>) { + fn process_struct_lit( + &mut self, + ex: &'l ast::Expr, + path: &'l ast::Path, + fields: &'l [ast::Field], + variant: &'l ty::VariantDef, + base: &'l Option>, + ) { self.write_sub_paths_truncated(path); if let Some(struct_lit_data) = self.save_ctxt.get_expr_data(ex) { @@ -860,8 +926,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { } for field in fields { - if let Some(field_data) = self.save_ctxt - .get_field_ref_data(field, variant) { + if let Some(field_data) = self.save_ctxt.get_field_ref_data(field, variant) { self.dumper.dump_ref(field_data); } @@ -872,10 +937,12 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { walk_list!(self, visit_expr, base); } - fn process_method_call(&mut self, - ex: &'l ast::Expr, - seg: &'l ast::PathSegment, - args: &'l [P]) { + fn process_method_call( + &mut self, + ex: &'l ast::Expr, + seg: &'l ast::PathSegment, + args: &'l [P], + ) { debug!("process_method_call {:?} {:?}", ex, ex.span); if let Some(mcd) = self.save_ctxt.get_expr_data(ex) { down_cast_data!(mcd, RefData, ex.span); @@ -911,7 +978,11 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { }; let variant = adt.variant_of_def(self.save_ctxt.get_path_def(p.id)); - for &Spanned { node: ref field, span } in fields { + for &Spanned { + node: ref field, + span, + } in fields + { let sub_span = self.span.span_for_first_ident(span); if let Some(f) = variant.find_field_named(field.ident.name) { if !self.span.filter_generated(sub_span, span) { @@ -966,20 +1037,23 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { let id = ::id_from_node_id(id, &self.save_ctxt); let span = self.span_from_span(sub_span.expect("No span found for variable")); - self.dumper.dump_def(false, Def { - kind: DefKind::Local, - id, - span, - name: i.to_string(), - qualname, - value: typ, - parent: None, - children: vec![], - decl_id: None, - docs: String::new(), - sig: None, - attributes:vec![], - }); + self.dumper.dump_def( + false, + Def { + kind: DefKind::Local, + id, + span, + name: i.to_string(), + qualname, + value: typ, + parent: None, + children: vec![], + decl_id: None, + docs: String::new(), + sig: None, + attributes: vec![], + }, + ); } } } @@ -1030,52 +1104,62 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { self.process_macro_use(trait_item.span); match trait_item.node { ast::TraitItemKind::Const(ref ty, ref expr) => { - self.process_assoc_const(trait_item.id, - trait_item.ident.name, - trait_item.span, - &ty, - expr.as_ref().map(|e| &**e), - trait_id, - ast::Visibility::Public, - &trait_item.attrs); + self.process_assoc_const( + trait_item.id, + trait_item.ident.name, + trait_item.span, + &ty, + expr.as_ref().map(|e| &**e), + trait_id, + ast::Visibility::Public, + &trait_item.attrs, + ); } ast::TraitItemKind::Method(ref sig, ref body) => { - self.process_method(sig, - body.as_ref().map(|x| &**x), - trait_item.id, - trait_item.ident, - &trait_item.generics, - ast::Visibility::Public, - trait_item.span); + self.process_method( + sig, + body.as_ref().map(|x| &**x), + trait_item.id, + trait_item.ident, + &trait_item.generics, + ast::Visibility::Public, + trait_item.span, + ); } ast::TraitItemKind::Type(ref bounds, ref default_ty) => { // FIXME do something with _bounds (for type refs) let name = trait_item.ident.name.to_string(); let qualname = format!("::{}", self.tcx.node_path_str(trait_item.id)); - let sub_span = self.span.sub_span_after_keyword(trait_item.span, keywords::Type); + let sub_span = self.span + .sub_span_after_keyword(trait_item.span, keywords::Type); if !self.span.filter_generated(sub_span, trait_item.span) { let span = self.span_from_span(sub_span.expect("No span found for assoc type")); let id = ::id_from_node_id(trait_item.id, &self.save_ctxt); - self.dumper.dump_def(true, Def { - kind: DefKind::Type, - id, - span, - name, - qualname, - value: self.span.snippet(trait_item.span), - parent: Some(::id_from_def_id(trait_id)), - children: vec![], - decl_id: None, - docs: self.save_ctxt.docs_for_attrs(&trait_item.attrs), - sig: sig::assoc_type_signature(trait_item.id, - trait_item.ident, - Some(bounds), - default_ty.as_ref().map(|ty| &**ty), - &self.save_ctxt), - attributes: lower_attributes(trait_item.attrs.clone(), &self.save_ctxt), - }); + self.dumper.dump_def( + true, + Def { + kind: DefKind::Type, + id, + span, + name, + qualname, + value: self.span.snippet(trait_item.span), + parent: Some(::id_from_def_id(trait_id)), + children: vec![], + decl_id: None, + docs: self.save_ctxt.docs_for_attrs(&trait_item.attrs), + sig: sig::assoc_type_signature( + trait_item.id, + trait_item.ident, + Some(bounds), + default_ty.as_ref().map(|ty| &**ty), + &self.save_ctxt, + ), + attributes: lower_attributes(trait_item.attrs.clone(), &self.save_ctxt), + }, + ); } if let &Some(ref default_ty) = default_ty { @@ -1090,23 +1174,27 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> { self.process_macro_use(impl_item.span); match impl_item.node { ast::ImplItemKind::Const(ref ty, ref expr) => { - self.process_assoc_const(impl_item.id, - impl_item.ident.name, - impl_item.span, - &ty, - Some(expr), - impl_id, - impl_item.vis.clone(), - &impl_item.attrs); + self.process_assoc_const( + impl_item.id, + impl_item.ident.name, + impl_item.span, + &ty, + Some(expr), + impl_id, + impl_item.vis.clone(), + &impl_item.attrs, + ); } ast::ImplItemKind::Method(ref sig, ref body) => { - self.process_method(sig, - Some(body), - impl_item.id, - impl_item.ident, - &impl_item.generics, - impl_item.vis.clone(), - impl_item.span); + self.process_method( + sig, + Some(body), + impl_item.id, + impl_item.ident, + &impl_item.generics, + impl_item.vis.clone(), + impl_item.span, + ); } ast::ImplItemKind::Type(ref ty) => { // FIXME uses of the assoc type should ideally point to this @@ -1130,23 +1218,29 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc let cm = self.tcx.sess.codemap(); let filename = cm.span_to_filename(span); let data_id = ::id_from_node_id(id, &self.save_ctxt); - let children = m.items.iter().map(|i| ::id_from_node_id(i.id, &self.save_ctxt)).collect(); + let children = m.items + .iter() + .map(|i| ::id_from_node_id(i.id, &self.save_ctxt)) + .collect(); let span = self.span_from_span(span); - self.dumper.dump_def(true, Def { - kind: DefKind::Mod, - id: data_id, - name: String::new(), - qualname, - span, - value: filename, - children, - parent: None, - decl_id: None, - docs: self.save_ctxt.docs_for_attrs(attrs), - sig: None, - attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt), - }); + self.dumper.dump_def( + true, + Def { + kind: DefKind::Mod, + id: data_id, + name: String::new(), + qualname, + span, + value: filename, + children, + parent: None, + decl_id: None, + docs: self.save_ctxt.docs_for_attrs(attrs), + sig: None, + attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt), + }, + ); self.nest_scope(id, |v| visit::walk_mod(v, m)); } @@ -1168,8 +1262,9 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc // 'use' always introduces an alias, if there is not an explicit // one, there is an implicit one. - let sub_span = match self.span.sub_span_after_keyword(use_item.span, - keywords::As) { + let sub_span = match self.span + .sub_span_after_keyword(use_item.span, keywords::As) + { Some(sub_span) => Some(sub_span), None => sub_span, }; @@ -1177,13 +1272,16 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc if !self.span.filter_generated(sub_span, path.span) { let span = self.span_from_span(sub_span.expect("No span found for use")); - self.dumper.import(item.vis == ast::Visibility::Public, Import { - kind: ImportKind::Use, - ref_id: mod_id.map(|id| ::id_from_def_id(id)), - span, - name: ident.to_string(), - value: String::new(), - }); + self.dumper.import( + item.vis == ast::Visibility::Public, + Import { + kind: ImportKind::Use, + ref_id: mod_id.map(|id| ::id_from_def_id(id)), + span, + name: ident.to_string(), + value: String::new(), + }, + ); } self.write_sub_paths_truncated(path); } @@ -1199,17 +1297,20 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc } let sub_span = self.span - .sub_span_of_token(item.span, token::BinOp(token::Star)); + .sub_span_of_token(item.span, token::BinOp(token::Star)); if !self.span.filter_generated(sub_span, item.span) { let span = self.span_from_span(sub_span.expect("No span found for use glob")); - self.dumper.import(item.vis == ast::Visibility::Public, Import { - kind: ImportKind::GlobUse, - ref_id: None, - span, - name: "*".to_owned(), - value: names.join(", "), - }); + self.dumper.import( + item.vis == ast::Visibility::Public, + Import { + kind: ImportKind::GlobUse, + ref_id: None, + span, + name: "*".to_owned(), + value: names.join(", "), + }, + ); } self.write_sub_paths(path); } @@ -1232,34 +1333,33 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc if !self.span.filter_generated(alias_span, item.span) { let span = self.span_from_span(alias_span.expect("No span found for extern crate")); - self.dumper.import(false, Import { - kind: ImportKind::ExternCrate, - ref_id: None, - span, - name: item.ident.to_string(), - value: String::new(), - }); + self.dumper.import( + false, + Import { + kind: ImportKind::ExternCrate, + ref_id: None, + span, + name: item.ident.to_string(), + value: String::new(), + }, + ); } } - Fn(ref decl, .., ref ty_params, ref body) => - self.process_fn(item, &decl, ty_params, &body), - Static(ref typ, _, ref expr) => - self.process_static_or_const_item(item, typ, expr), - Const(ref typ, ref expr) => - self.process_static_or_const_item(item, &typ, &expr), + Fn(ref decl, .., ref ty_params, ref body) => { + self.process_fn(item, &decl, ty_params, &body) + } + Static(ref typ, _, ref expr) => self.process_static_or_const_item(item, typ, expr), + Const(ref typ, ref expr) => self.process_static_or_const_item(item, &typ, &expr), Struct(ref def, ref ty_params) | Union(ref def, ref ty_params) => { self.process_struct(item, def, ty_params) } Enum(ref def, ref ty_params) => self.process_enum(item, def, ty_params), - Impl(.., - ref ty_params, - ref trait_ref, - ref typ, - ref impl_items) => { + Impl(.., ref ty_params, ref trait_ref, ref typ, ref impl_items) => { self.process_impl(item, ty_params, trait_ref, &typ, impl_items) } - Trait(_, _, ref generics, ref trait_refs, ref methods) => - self.process_trait(item, generics, trait_refs, methods), + Trait(_, _, ref generics, ref trait_refs, ref methods) => { + self.process_trait(item, generics, trait_refs, methods) + } Mod(ref m) => { self.process_mod(item); self.nest_scope(item.id, |v| visit::walk_mod(v, m)); @@ -1272,20 +1372,23 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc let span = self.span_from_span(sub_span.expect("No span found for typedef")); let id = ::id_from_node_id(item.id, &self.save_ctxt); - self.dumper.dump_def(item.vis == ast::Visibility::Public, Def { - kind: DefKind::Type, - id, - span, - name: item.ident.to_string(), - qualname: qualname.clone(), - value, - parent: None, - children: vec![], - decl_id: None, - docs: self.save_ctxt.docs_for_attrs(&item.attrs), - sig: sig::item_signature(item, &self.save_ctxt), - attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt), - }); + self.dumper.dump_def( + item.vis == ast::Visibility::Public, + Def { + kind: DefKind::Type, + id, + span, + name: item.ident.to_string(), + qualname: qualname.clone(), + value, + parent: None, + children: vec![], + decl_id: None, + docs: self.save_ctxt.docs_for_attrs(&item.attrs), + sig: sig::item_signature(item, &self.save_ctxt), + attributes: lower_attributes(item.attrs.clone(), &self.save_ctxt), + }, + ); } self.visit_ty(&ty); @@ -1372,8 +1475,11 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc let hir_node = match self.save_ctxt.tcx.hir.find(sub_ex.id) { Some(Node::NodeExpr(expr)) => expr, _ => { - debug!("Missing or weird node for sub-expression {} in {:?}", - sub_ex.id, ex); + debug!( + "Missing or weird node for sub-expression {} in {:?}", + sub_ex.id, + ex + ); return; } }; @@ -1398,9 +1504,7 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc } } ty::TyTuple(..) => {} - _ => span_bug!(ex.span, - "Expected struct or tuple type, found {:?}", - ty), + _ => span_bug!(ex.span, "Expected struct or tuple type, found {:?}", ty), } } ast::ExprKind::Closure(_, ref decl, ref body, _fn_decl_span) => { @@ -1443,15 +1547,16 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc } // In particular, we take this branch for call and path expressions, // where we'll index the idents involved just by continuing to walk. - _ => { - visit::walk_expr(self, ex) - } + _ => visit::walk_expr(self, ex), } } fn visit_mac(&mut self, mac: &'l ast::Mac) { // These shouldn't exist in the AST at this point, log a span bug. - span_bug!(mac.span, "macro invocation should have been expanded out of AST"); + span_bug!( + mac.span, + "macro invocation should have been expanded out of AST" + ); } fn visit_pat(&mut self, p: &'l ast::Pat) { @@ -1478,10 +1583,10 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc }; let hir_id = self.tcx.hir.node_to_hir_id(id); let typ = self.save_ctxt - .tables - .node_id_to_type_opt(hir_id) - .map(|t| t.to_string()) - .unwrap_or(String::new()); + .tables + .node_id_to_type_opt(hir_id) + .map(|t| t.to_string()) + .unwrap_or(String::new()); value.push_str(": "); value.push_str(&typ); @@ -1490,31 +1595,40 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc let id = ::id_from_node_id(id, &self.save_ctxt); let span = self.span_from_span(sp); - self.dumper.dump_def(false, Def { - kind: DefKind::Local, - id, - span, - name: i.to_string(), - qualname, - value: typ, - parent: None, - children: vec![], - decl_id: None, - docs: String::new(), - sig: None, - attributes:vec![], - }); + self.dumper.dump_def( + false, + Def { + kind: DefKind::Local, + id, + span, + name: i.to_string(), + qualname, + value: typ, + parent: None, + children: vec![], + decl_id: None, + docs: String::new(), + sig: None, + attributes: vec![], + }, + ); } } - HirDef::StructCtor(..) | HirDef::VariantCtor(..) | - HirDef::Const(..) | HirDef::AssociatedConst(..) | - HirDef::Struct(..) | HirDef::Variant(..) | - HirDef::TyAlias(..) | HirDef::AssociatedTy(..) | + HirDef::StructCtor(..) | + HirDef::VariantCtor(..) | + HirDef::Const(..) | + HirDef::AssociatedConst(..) | + HirDef::Struct(..) | + HirDef::Variant(..) | + HirDef::TyAlias(..) | + HirDef::AssociatedTy(..) | HirDef::SelfTy(..) => { self.dump_path_ref(id, &ast::Path::from_ident(sp, i)); } - def => error!("unexpected definition kind when processing collected idents: {:?}", - def), + def => error!( + "unexpected definition kind when processing collected idents: {:?}", + def + ), } } @@ -1536,7 +1650,10 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc fn visit_local(&mut self, l: &'l ast::Local) { self.process_macro_use(l.span); - let value = l.init.as_ref().map(|i| self.span.snippet(i.span)).unwrap_or(String::new()); + let value = l.init + .as_ref() + .map(|i| self.span.snippet(i.span)) + .unwrap_or(String::new()); self.process_var_decl(&l.pat, value); // Just walk the initialiser and type (don't want to walk the pattern again). @@ -1550,10 +1667,13 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc if let Some(fn_data) = self.save_ctxt.get_extern_item_data(item) { down_cast_data!(fn_data, DefData, item.span); - self.nest_tables(item.id, |v| v.process_formals(&decl.inputs, - &fn_data.qualname)); + self.nest_tables( + item.id, + |v| v.process_formals(&decl.inputs, &fn_data.qualname), + ); self.process_generic_params(generics, item.span, &fn_data.qualname, item.id); - self.dumper.dump_def(item.vis == ast::Visibility::Public, fn_data); + self.dumper + .dump_def(item.vis == ast::Visibility::Public, fn_data); } for arg in &decl.inputs { @@ -1567,7 +1687,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc ast::ForeignItemKind::Static(ref ty, _) => { if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) { down_cast_data!(var_data, DefData, item.span); - self.dumper.dump_def(item.vis == ast::Visibility::Public, var_data); + self.dumper + .dump_def(item.vis == ast::Visibility::Public, var_data); } self.visit_ty(ty); @@ -1575,7 +1696,8 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tc ast::ForeignItemKind::Ty => { if let Some(var_data) = self.save_ctxt.get_extern_item_data(item) { down_cast_data!(var_data, DefData, item.span); - self.dumper.dump_def(item.vis == ast::Visibility::Public, var_data); + self.dumper + .dump_def(item.vis == ast::Visibility::Public, var_data); } } } diff --git a/src/librustc_save_analysis/json_dumper.rs b/src/librustc_save_analysis/json_dumper.rs index 8dd191f4375..14b624b9338 100644 --- a/src/librustc_save_analysis/json_dumper.rs +++ b/src/librustc_save_analysis/json_dumper.rs @@ -12,8 +12,8 @@ use std::io::Write; use rustc_serialize::json::as_json; -use rls_data::{self, Analysis, Import, Def, DefKind, Ref, RefKind, MacroRef, - Relation, CratePreludeData}; +use rls_data::{self, Analysis, CratePreludeData, Def, DefKind, Import, MacroRef, Ref, RefKind, + Relation}; use rls_data::config::Config; use rls_span::{Column, Row}; @@ -54,15 +54,16 @@ impl<'b, W: Write> JsonDumper> { JsonDumper { output: WriteOutput { output: writer }, config: config.clone(), - result: Analysis::new(config) + result: Analysis::new(config), } } } impl<'b> JsonDumper> { - pub fn with_callback(callback: &'b mut FnMut(&Analysis), - config: Config) - -> JsonDumper> { + pub fn with_callback( + callback: &'b mut FnMut(&Analysis), + config: Config, + ) -> JsonDumper> { JsonDumper { output: CallbackOutput { callback: callback }, config: config.clone(), diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index 5d97dbf728b..fddafed11f3 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -9,20 +9,22 @@ // except according to those terms. #![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", - html_favicon_url = "https://doc.rust-lang.org/favicon.ico", - html_root_url = "https://doc.rust-lang.org/nightly/")] + html_favicon_url = "https://doc.rust-lang.org/favicon.ico", + html_root_url = "https://doc.rust-lang.org/nightly/")] #![deny(warnings)] - #![feature(custom_attribute)] #![allow(unused_attributes)] -#[macro_use] extern crate rustc; +#[macro_use] +extern crate rustc; -#[macro_use] extern crate log; -#[macro_use] extern crate syntax; +#[macro_use] +extern crate log; extern crate rustc_data_structures; extern crate rustc_serialize; extern crate rustc_typeck; +#[macro_use] +extern crate syntax; extern crate syntax_pos; extern crate rls_data; @@ -38,7 +40,7 @@ mod sig; use rustc::hir; use rustc::hir::def::Def as HirDef; use rustc::hir::map::{Node, NodeItem}; -use rustc::hir::def_id::{LOCAL_CRATE, DefId}; +use rustc::hir::def_id::{DefId, LOCAL_CRATE}; use rustc::session::config::CrateType::CrateTypeExecutable; use rustc::ty::{self, TyCtxt}; use rustc_typeck::hir_ty_to_ty; @@ -48,13 +50,13 @@ use std::env; use std::fs::File; use std::path::{Path, PathBuf}; -use syntax::ast::{self, NodeId, PatKind, Attribute}; +use syntax::ast::{self, Attribute, NodeId, PatKind}; use syntax::parse::lexer::comments::strip_doc_comment_decoration; use syntax::parse::token; use syntax::print::pprust; use syntax::symbol::keywords; use syntax::visit::{self, Visitor}; -use syntax::print::pprust::{ty_to_string, arg_to_string}; +use syntax::print::pprust::{arg_to_string, ty_to_string}; use syntax::codemap::MacroAttribute; use syntax_pos::*; @@ -62,8 +64,8 @@ use json_dumper::JsonDumper; use dump_visitor::DumpVisitor; use span_utils::SpanUtils; -use rls_data::{Ref, RefKind, SpanData, MacroRef, Def, DefKind, Relation, RelationKind, - ExternalCrateData, GlobalCrateId}; +use rls_data::{Def, DefKind, ExternalCrateData, GlobalCrateId, MacroRef, Ref, RefKind, Relation, + RelationKind, SpanData}; use rls_data::config::Config; @@ -84,7 +86,7 @@ pub enum Data { impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { fn span_from_span(&self, span: Span) -> SpanData { - use rls_span::{Row, Column}; + use rls_span::{Column, Row}; let cm = self.tcx.sess.codemap(); let start = cm.lookup_char_pos(span.lo()); @@ -119,8 +121,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { num: n.as_u32(), id: GlobalCrateId { name: self.tcx.crate_name(n).to_string(), - disambiguator: self.tcx.crate_disambiguator(n) - .to_fingerprint().as_value(), + disambiguator: self.tcx.crate_disambiguator(n).to_fingerprint().as_value(), }, }); } @@ -132,7 +133,8 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { let qualname = format!("::{}", self.tcx.node_path_str(item.id)); match item.node { ast::ForeignItemKind::Fn(ref decl, ref generics) => { - let sub_span = self.span_utils.sub_span_after_keyword(item.span, keywords::Fn); + let sub_span = self.span_utils + .sub_span_after_keyword(item.span, keywords::Fn); filter!(self.span_utils, sub_span, item.span, None); Some(Data::DefData(Def { @@ -182,7 +184,8 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { match item.node { ast::ItemKind::Fn(ref decl, .., ref generics, _) => { let qualname = format!("::{}", self.tcx.node_path_str(item.id)); - let sub_span = self.span_utils.sub_span_after_keyword(item.span, keywords::Fn); + let sub_span = self.span_utils + .sub_span_after_keyword(item.span, keywords::Fn); filter!(self.span_utils, sub_span, item.span, None); Some(Data::DefData(Def { kind: DefKind::Function, @@ -230,7 +233,8 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { } ast::ItemKind::Const(ref typ, _) => { let qualname = format!("::{}", self.tcx.node_path_str(item.id)); - let sub_span = self.span_utils.sub_span_after_keyword(item.span, keywords::Const); + let sub_span = self.span_utils + .sub_span_after_keyword(item.span, keywords::Const); filter!(self.span_utils, sub_span, item.span, None); let id = id_from_node_id(item.id, self); @@ -257,7 +261,8 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { let cm = self.tcx.sess.codemap(); let filename = cm.span_to_filename(m.inner); - let sub_span = self.span_utils.sub_span_after_keyword(item.span, keywords::Mod); + let sub_span = self.span_utils + .sub_span_after_keyword(item.span, keywords::Mod); filter!(self.span_utils, sub_span, item.span, None); Some(Data::DefData(Def { @@ -268,7 +273,10 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { span: self.span_from_span(sub_span.unwrap()), value: filename, parent: None, - children: m.items.iter().map(|i| id_from_node_id(i.id, self)).collect(), + children: m.items + .iter() + .map(|i| id_from_node_id(i.id, self)) + .collect(), decl_id: None, docs: self.docs_for_attrs(&item.attrs), sig: sig::item_signature(item, self), @@ -278,12 +286,14 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { ast::ItemKind::Enum(ref def, _) => { let name = item.ident.to_string(); let qualname = format!("::{}", self.tcx.node_path_str(item.id)); - let sub_span = self.span_utils.sub_span_after_keyword(item.span, keywords::Enum); + let sub_span = self.span_utils + .sub_span_after_keyword(item.span, keywords::Enum); filter!(self.span_utils, sub_span, item.span, None); - let variants_str = def.variants.iter() - .map(|v| v.node.name.to_string()) - .collect::>() - .join(", "); + let variants_str = def.variants + .iter() + .map(|v| v.node.name.to_string()) + .collect::>() + .join(", "); let value = format!("{}::{{{}}}", name, variants_str); Some(Data::DefData(Def { kind: DefKind::Enum, @@ -294,9 +304,9 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { value, parent: None, children: def.variants - .iter() - .map(|v| id_from_node_id(v.node.data.id(), self)) - .collect(), + .iter() + .map(|v| id_from_node_id(v.node.data.id(), self)) + .collect(), decl_id: None, docs: self.docs_for_attrs(&item.attrs), sig: sig::item_signature(item, self), @@ -313,15 +323,18 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { filter!(self.span_utils, sub_span, typ.span, None); let type_data = self.lookup_ref_id(typ.id); - type_data.map(|type_data| Data::RelationData(Relation { - kind: RelationKind::Impl, - span: self.span_from_span(sub_span.unwrap()), - from: id_from_def_id(type_data), - to: trait_ref.as_ref() - .and_then(|t| self.lookup_ref_id(t.ref_id)) - .map(id_from_def_id) - .unwrap_or(null_id()), - })) + type_data.map(|type_data| { + Data::RelationData(Relation { + kind: RelationKind::Impl, + span: self.span_from_span(sub_span.unwrap()), + from: id_from_def_id(type_data), + to: trait_ref + .as_ref() + .and_then(|t| self.lookup_ref_id(t.ref_id)) + .map(id_from_def_id) + .unwrap_or(null_id()), + }) + }) } else { None } @@ -333,14 +346,12 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { } } - pub fn get_field_data(&self, - field: &ast::StructField, - scope: NodeId) - -> Option { + pub fn get_field_data(&self, field: &ast::StructField, scope: NodeId) -> Option { if let Some(ident) = field.ident { let name = ident.to_string(); let qualname = format!("::{}::{}", self.tcx.node_path_str(scope), ident); - let sub_span = self.span_utils.sub_span_before_token(field.span, token::Colon); + let sub_span = self.span_utils + .sub_span_before_token(field.span, token::Colon); filter!(self.span_utils, sub_span, field.span, None); let def_id = self.tcx.hir.local_def_id(field.id); let typ = self.tcx.type_of(def_id).to_string(); @@ -370,18 +381,13 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { // FIXME would be nice to take a MethodItem here, but the ast provides both // trait and impl flavours, so the caller must do the disassembly. - pub fn get_method_data(&self, - id: ast::NodeId, - name: ast::Name, - span: Span) - -> Option { + pub fn get_method_data(&self, id: ast::NodeId, name: ast::Name, span: Span) -> Option { // The qualname for a method is the trait name or name of the struct in an impl in // which the method is declared in, followed by the method's name. let (qualname, parent_scope, decl_id, docs, attributes) = - match self.tcx.impl_of_method(self.tcx.hir.local_def_id(id)) { - Some(impl_id) => match self.tcx.hir.get_if_local(impl_id) { - Some(Node::NodeItem(item)) => { - match item.node { + match self.tcx.impl_of_method(self.tcx.hir.local_def_id(id)) { + Some(impl_id) => match self.tcx.hir.get_if_local(impl_id) { + Some(Node::NodeItem(item)) => match item.node { hir::ItemImpl(.., ref ty, _) => { let mut result = String::from("<"); result.push_str(&self.tcx.hir.node_to_pretty_string(ty.id)); @@ -391,7 +397,8 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { if let Some(def_id) = trait_id { result.push_str(" as "); result.push_str(&self.tcx.item_path_str(def_id)); - self.tcx.associated_items(def_id) + self.tcx + .associated_items(def_id) .find(|item| item.name == name) .map(|item| decl_id = Some(item.def_id)); } else { @@ -403,53 +410,61 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { } result.push_str(">"); - (result, trait_id, decl_id, - self.docs_for_attrs(&item.attrs), - item.attrs.to_vec()) + ( + result, + trait_id, + decl_id, + self.docs_for_attrs(&item.attrs), + item.attrs.to_vec(), + ) } _ => { - span_bug!(span, - "Container {:?} for method {} not an impl?", - impl_id, - id); + span_bug!( + span, + "Container {:?} for method {} not an impl?", + impl_id, + id + ); } + }, + r => { + span_bug!( + span, + "Container {:?} for method {} is not a node item {:?}", + impl_id, + id, + r + ); } - } - r => { - span_bug!(span, - "Container {:?} for method {} is not a node item {:?}", - impl_id, - id, - r); - } - }, - None => match self.tcx.trait_of_item(self.tcx.hir.local_def_id(id)) { - Some(def_id) => { - match self.tcx.hir.get_if_local(def_id) { - Some(Node::NodeItem(item)) => { - (format!("::{}", self.tcx.item_path_str(def_id)), - Some(def_id), None, - self.docs_for_attrs(&item.attrs), - item.attrs.to_vec()) - } + }, + None => match self.tcx.trait_of_item(self.tcx.hir.local_def_id(id)) { + Some(def_id) => match self.tcx.hir.get_if_local(def_id) { + Some(Node::NodeItem(item)) => ( + format!("::{}", self.tcx.item_path_str(def_id)), + Some(def_id), + None, + self.docs_for_attrs(&item.attrs), + item.attrs.to_vec(), + ), r => { - span_bug!(span, - "Could not find container {:?} for \ - method {}, got {:?}", - def_id, - id, - r); + span_bug!( + span, + "Could not find container {:?} for \ + method {}, got {:?}", + def_id, + id, + r + ); } + }, + None => { + debug!("Could not find container for method {} at {:?}", id, span); + // This is not necessarily a bug, if there was a compilation error, + // the tables we need might not exist. + return None; } - } - None => { - debug!("Could not find container for method {} at {:?}", id, span); - // This is not necessarily a bug, if there was a compilation error, the tables - // we need might not exist. - return None; - } - }, - }; + }, + }; let qualname = format!("{}::{}", qualname, name); @@ -473,9 +488,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { }) } - pub fn get_trait_ref_data(&self, - trait_ref: &ast::TraitRef) - -> Option { + pub fn get_trait_ref_data(&self, trait_ref: &ast::TraitRef) -> Option { self.lookup_ref_id(trait_ref.ref_id).and_then(|def_id| { let span = trait_ref.path.span; if generated_code(span) { @@ -503,8 +516,11 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { let hir_node = match self.tcx.hir.find(sub_ex.id) { Some(Node::NodeExpr(expr)) => expr, _ => { - debug!("Missing or weird node for sub-expression {} in {:?}", - sub_ex.id, expr); + debug!( + "Missing or weird node for sub-expression {} in {:?}", + sub_ex.id, + expr + ); return None; } }; @@ -565,7 +581,10 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { Some(Data::RefData(Ref { kind: RefKind::Function, span, - ref_id: def_id.or(decl_id).map(|id| id_from_def_id(id)).unwrap_or(null_id()), + ref_id: def_id + .or(decl_id) + .map(|id| id_from_def_id(id)) + .unwrap_or(null_id()), })) } ast::ExprKind::Path(_, ref path) => { @@ -582,40 +601,61 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { match self.tcx.hir.get(id) { Node::NodeTraitRef(tr) => tr.path.def, - Node::NodeItem(&hir::Item { node: hir::ItemUse(ref path, _), .. }) | + Node::NodeItem(&hir::Item { + node: hir::ItemUse(ref path, _), + .. + }) | Node::NodeVisibility(&hir::Visibility::Restricted { ref path, .. }) => path.def, - Node::NodeExpr(&hir::Expr { node: hir::ExprStruct(ref qpath, ..), .. }) | - Node::NodeExpr(&hir::Expr { node: hir::ExprPath(ref qpath), .. }) | - Node::NodePat(&hir::Pat { node: hir::PatKind::Path(ref qpath), .. }) | - Node::NodePat(&hir::Pat { node: hir::PatKind::Struct(ref qpath, ..), .. }) | - Node::NodePat(&hir::Pat { node: hir::PatKind::TupleStruct(ref qpath, ..), .. }) => { + Node::NodeExpr(&hir::Expr { + node: hir::ExprStruct(ref qpath, ..), + .. + }) | + Node::NodeExpr(&hir::Expr { + node: hir::ExprPath(ref qpath), + .. + }) | + Node::NodePat(&hir::Pat { + node: hir::PatKind::Path(ref qpath), + .. + }) | + Node::NodePat(&hir::Pat { + node: hir::PatKind::Struct(ref qpath, ..), + .. + }) | + Node::NodePat(&hir::Pat { + node: hir::PatKind::TupleStruct(ref qpath, ..), + .. + }) => { let hir_id = self.tcx.hir.node_to_hir_id(id); self.tables.qpath_def(qpath, hir_id) } Node::NodeBinding(&hir::Pat { - node: hir::PatKind::Binding(_, canonical_id, ..), .. + node: hir::PatKind::Binding(_, canonical_id, ..), + .. }) => HirDef::Local(canonical_id), - Node::NodeTy(ty) => { - if let hir::Ty { node: hir::TyPath(ref qpath), .. } = *ty { - match *qpath { - hir::QPath::Resolved(_, ref path) => path.def, - hir::QPath::TypeRelative(..) => { - let ty = hir_ty_to_ty(self.tcx, ty); - if let ty::TyProjection(proj) = ty.sty { - return HirDef::AssociatedTy(proj.item_def_id); - } - HirDef::Err + Node::NodeTy(ty) => if let hir::Ty { + node: hir::TyPath(ref qpath), + .. + } = *ty + { + match *qpath { + hir::QPath::Resolved(_, ref path) => path.def, + hir::QPath::TypeRelative(..) => { + let ty = hir_ty_to_ty(self.tcx, ty); + if let ty::TyProjection(proj) = ty.sty { + return HirDef::AssociatedTy(proj.item_def_id); } + HirDef::Err } - } else { - HirDef::Err } - } + } else { + HirDef::Err + }, - _ => HirDef::Err + _ => HirDef::Err, } } @@ -642,8 +682,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { let sub_span = last_seg.span; filter!(self.span_utils, Some(sub_span), path.span, None); match def { - HirDef::Upvar(id, ..) | - HirDef::Local(id) => { + HirDef::Upvar(id, ..) | HirDef::Local(id) => { let span = self.span_from_span(sub_span); Some(Ref { kind: RefKind::Variable, @@ -666,10 +705,12 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { // Function type bounds are desugared in the parser, so we have to // special case them here. let fn_span = self.span_utils.span_for_first_ident(path.span); - fn_span.map(|span| Ref { - kind: RefKind::Type, - span: self.span_from_span(span), - ref_id: id_from_def_id(def_id), + fn_span.map(|span| { + Ref { + kind: RefKind::Type, + span: self.span_from_span(span), + ref_id: id_from_def_id(def_id), + } }) } HirDef::Struct(def_id) | @@ -703,7 +744,8 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { HirDef::Method(decl_id) => { let def_id = if decl_id.is_local() { let ti = self.tcx.associated_item(decl_id); - self.tcx.associated_items(ti.container.id()) + self.tcx + .associated_items(ti.container.id()) .find(|item| item.name == ti.name && item.defaultness.has_value()) .map(|item| item.def_id) } else { @@ -741,10 +783,11 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { } } - pub fn get_field_ref_data(&self, - field_ref: &ast::Field, - variant: &ty::VariantDef) - -> Option { + pub fn get_field_ref_data( + &self, + field_ref: &ast::Field, + variant: &ty::VariantDef, + ) -> Option { let f = variant.field_named(field_ref.ident.node.name); // We don't really need a sub-span here, but no harm done let sub_span = self.span_utils.span_for_last_ident(field_ref.ident.span); @@ -782,7 +825,12 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { // If the callee is an imported macro from an external crate, need to get // the source span and name from the session, as their spans are localized // when read in, and no longer correspond to the source. - if let Some(mac) = self.tcx.sess.imported_macro_spans.borrow().get(&callee_span) { + if let Some(mac) = self.tcx + .sess + .imported_macro_spans + .borrow() + .get(&callee_span) + { let &(ref mac_name, mac_span) = mac; let mac_span = self.span_from_span(mac_span); return Some(MacroRef { @@ -837,21 +885,29 @@ fn make_signature(decl: &ast::FnDecl, generics: &ast::Generics) -> String { let mut sig = "fn ".to_owned(); if !generics.lifetimes.is_empty() || !generics.ty_params.is_empty() { sig.push('<'); - sig.push_str(&generics.lifetimes.iter() - .map(|l| l.lifetime.ident.name.to_string()) - .collect::>() - .join(", ")); + sig.push_str(&generics + .lifetimes + .iter() + .map(|l| l.lifetime.ident.name.to_string()) + .collect::>() + .join(", ")); if !generics.lifetimes.is_empty() { sig.push_str(", "); } - sig.push_str(&generics.ty_params.iter() - .map(|l| l.ident.to_string()) - .collect::>() - .join(", ")); + sig.push_str(&generics + .ty_params + .iter() + .map(|l| l.ident.to_string()) + .collect::>() + .join(", ")); sig.push_str("> "); } sig.push('('); - sig.push_str(&decl.inputs.iter().map(arg_to_string).collect::>().join(", ")); + sig.push_str(&decl.inputs + .iter() + .map(arg_to_string) + .collect::>() + .join(", ")); sig.push(')'); match decl.output { ast::FunctionRetTy::Default(_) => sig.push_str(" -> ()"), @@ -883,15 +939,16 @@ impl<'l, 'a: 'l> Visitor<'a> for PathCollector<'l> { PatKind::Struct(ref path, ..) => { self.collected_paths.push((p.id, path)); } - PatKind::TupleStruct(ref path, ..) | - PatKind::Path(_, ref path) => { + PatKind::TupleStruct(ref path, ..) | PatKind::Path(_, ref path) => { self.collected_paths.push((p.id, path)); } PatKind::Ident(bm, ref path1, _) => { - debug!("PathCollector, visit ident in pat {}: {:?} {:?}", - path1.node, - p.span, - path1.span); + debug!( + "PathCollector, visit ident in pat {}: {:?} {:?}", + path1.node, + p.span, + path1.span + ); let immut = match bm { // Even if the ref is mut, you can't change the ref, only // the data pointed at, so showing the initialising expression @@ -899,7 +956,8 @@ impl<'l, 'a: 'l> Visitor<'a> for PathCollector<'l> { ast::BindingMode::ByRef(_) => ast::Mutability::Immutable, ast::BindingMode::ByValue(mt) => mt, }; - self.collected_idents.push((p.id, path1.node, path1.span, immut)); + self.collected_idents + .push((p.id, path1.node, path1.span, immut)); } _ => {} } @@ -909,23 +967,25 @@ impl<'l, 'a: 'l> Visitor<'a> for PathCollector<'l> { /// Defines what to do with the results of saving the analysis. pub trait SaveHandler { - fn save<'l, 'tcx>(&mut self, - save_ctxt: SaveContext<'l, 'tcx>, - krate: &ast::Crate, - cratename: &str); + fn save<'l, 'tcx>( + &mut self, + save_ctxt: SaveContext<'l, 'tcx>, + krate: &ast::Crate, + cratename: &str, + ); } /// Dump the save-analysis results to a file. pub struct DumpHandler<'a> { odir: Option<&'a Path>, - cratename: String + cratename: String, } impl<'a> DumpHandler<'a> { pub fn new(odir: Option<&'a Path>, cratename: &str) -> DumpHandler<'a> { DumpHandler { odir, - cratename: cratename.to_owned() + cratename: cratename.to_owned(), } } @@ -943,8 +1003,10 @@ impl<'a> DumpHandler<'a> { error!("Could not create directory {}: {}", root_path.display(), e); } - let executable = - sess.crate_types.borrow().iter().any(|ct| *ct == CrateTypeExecutable); + let executable = sess.crate_types + .borrow() + .iter() + .any(|ct| *ct == CrateTypeExecutable); let mut out_name = if executable { "".to_owned() } else { @@ -961,19 +1023,21 @@ impl<'a> DumpHandler<'a> { info!("Writing output to {}", file_name.display()); - let output_file = File::create(&file_name).unwrap_or_else(|e| { - sess.fatal(&format!("Could not open {}: {}", file_name.display(), e)) - }); + let output_file = File::create(&file_name).unwrap_or_else( + |e| sess.fatal(&format!("Could not open {}: {}", file_name.display(), e)), + ); output_file } } impl<'a> SaveHandler for DumpHandler<'a> { - fn save<'l, 'tcx>(&mut self, - save_ctxt: SaveContext<'l, 'tcx>, - krate: &ast::Crate, - cratename: &str) { + fn save<'l, 'tcx>( + &mut self, + save_ctxt: SaveContext<'l, 'tcx>, + krate: &ast::Crate, + cratename: &str, + ) { let output = &mut self.output_file(&save_ctxt); let mut dumper = JsonDumper::new(output, save_ctxt.config.clone()); let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper); @@ -989,10 +1053,12 @@ pub struct CallbackHandler<'b> { } impl<'b> SaveHandler for CallbackHandler<'b> { - fn save<'l, 'tcx>(&mut self, - save_ctxt: SaveContext<'l, 'tcx>, - krate: &ast::Crate, - cratename: &str) { + fn save<'l, 'tcx>( + &mut self, + save_ctxt: SaveContext<'l, 'tcx>, + krate: &ast::Crate, + cratename: &str, + ) { // We're using the JsonDumper here because it has the format of the // save-analysis results that we will pass to the callback. IOW, we are // using the JsonDumper to collect the save-analysis results, but not @@ -1006,12 +1072,14 @@ impl<'b> SaveHandler for CallbackHandler<'b> { } } -pub fn process_crate<'l, 'tcx, H: SaveHandler>(tcx: TyCtxt<'l, 'tcx, 'tcx>, - krate: &ast::Crate, - analysis: &'l ty::CrateAnalysis, - cratename: &str, - config: Option, - mut handler: H) { +pub fn process_crate<'l, 'tcx, H: SaveHandler>( + tcx: TyCtxt<'l, 'tcx, 'tcx>, + krate: &ast::Crate, + analysis: &'l ty::CrateAnalysis, + cratename: &str, + config: Option, + mut handler: H, +) { let _ignore = tcx.dep_graph.in_ignore(); assert!(analysis.glob_map.is_some()); @@ -1034,10 +1102,8 @@ fn find_config(supplied: Option) -> Config { return config; } match env::var_os("RUST_SAVE_ANALYSIS_CONFIG") { - Some(config_string) => { - rustc_serialize::json::decode(config_string.to_str().unwrap()) - .expect("Could not deserialize save-analysis config") - }, + Some(config_string) => rustc_serialize::json::decode(config_string.to_str().unwrap()) + .expect("Could not deserialize save-analysis config"), None => Config::default(), } } diff --git a/src/librustc_save_analysis/sig.rs b/src/librustc_save_analysis/sig.rs index 80c1b0ebeb0..b244876226c 100644 --- a/src/librustc_save_analysis/sig.rs +++ b/src/librustc_save_analysis/sig.rs @@ -35,9 +35,9 @@ // // FIXME where clauses need implementing, defs/refs in generics are mostly missing. -use {SaveContext, id_from_def_id, id_from_node_id}; +use {id_from_def_id, id_from_node_id, SaveContext}; -use rls_data::{Signature, SigElement}; +use rls_data::{SigElement, Signature}; use rustc::hir::def::Def; use syntax::ast::{self, NodeId}; @@ -75,36 +75,39 @@ pub fn variant_signature(variant: &ast::Variant, scx: &SaveContext) -> Option Option { +pub fn method_signature( + id: NodeId, + ident: ast::Ident, + generics: &ast::Generics, + m: &ast::MethodSig, + scx: &SaveContext, +) -> Option { if !scx.config.signatures { return None; } make_method_signature(id, ident, generics, m, scx).ok() } -pub fn assoc_const_signature(id: NodeId, - ident: ast::Name, - ty: &ast::Ty, - default: Option<&ast::Expr>, - scx: &SaveContext) - -> Option { +pub fn assoc_const_signature( + id: NodeId, + ident: ast::Name, + ty: &ast::Ty, + default: Option<&ast::Expr>, + scx: &SaveContext, +) -> Option { if !scx.config.signatures { return None; } make_assoc_const_signature(id, ident, ty, default, scx).ok() } -pub fn assoc_type_signature(id: NodeId, - ident: ast::Ident, - bounds: Option<&ast::TyParamBounds>, - default: Option<&ast::Ty>, - scx: &SaveContext) - -> Option { +pub fn assoc_type_signature( + id: NodeId, + ident: ast::Ident, + bounds: Option<&ast::TyParamBounds>, + default: Option<&ast::Ty>, + scx: &SaveContext, +) -> Option { if !scx.config.signatures { return None; } @@ -117,11 +120,12 @@ trait Sig { fn make(&self, offset: usize, id: Option, scx: &SaveContext) -> Result; } -fn extend_sig(mut sig: Signature, - text: String, - defs: Vec, - refs: Vec) - -> Signature { +fn extend_sig( + mut sig: Signature, + text: String, + defs: Vec, + refs: Vec, +) -> Signature { sig.text = text; sig.defs.extend(defs.into_iter()); sig.refs.extend(refs.into_iter()); @@ -142,8 +146,12 @@ fn merge_sigs(text: String, sigs: Vec) -> Signature { let (defs, refs): (Vec<_>, Vec<_>) = sigs.into_iter().map(|s| (s.defs, s.refs)).unzip(); - result.defs.extend(defs.into_iter().flat_map(|ds| ds.into_iter())); - result.refs.extend(refs.into_iter().flat_map(|rs| rs.into_iter())); + result + .defs + .extend(defs.into_iter().flat_map(|ds| ds.into_iter())); + result + .refs + .extend(refs.into_iter().flat_map(|rs| rs.into_iter())); result } @@ -188,9 +196,7 @@ impl Sig for ast::Ty { let text = format!("{}{}", prefix, nested.text); Ok(replace_text(nested, text)) } - ast::TyKind::Never => { - Ok(text_sig("!".to_owned())) - }, + ast::TyKind::Never => Ok(text_sig("!".to_owned())), ast::TyKind::Tup(ref ts) => { let mut text = "(".to_owned(); let mut defs = vec![]; @@ -215,8 +221,11 @@ impl Sig for ast::Ty { if !f.lifetimes.is_empty() { // FIXME defs, bounds on lifetimes text.push_str("for<"); - text.push_str(&f.lifetimes.iter().map(|l| - l.lifetime.ident.to_string()).collect::>().join(", ")); + text.push_str(&f.lifetimes + .iter() + .map(|l| l.lifetime.ident.to_string()) + .collect::>() + .join(", ")); text.push('>'); } @@ -251,9 +260,7 @@ impl Sig for ast::Ty { Ok(Signature { text, defs, refs }) } - ast::TyKind::Path(None, ref path) => { - path.make(offset, id, scx) - } + ast::TyKind::Path(None, ref path) => path.make(offset, id, scx), ast::TyKind::Path(Some(ref qself), ref path) => { let nested_ty = qself.ty.make(offset + 1, id, scx)?; let prefix = if qself.position == 0 { @@ -325,11 +332,13 @@ impl Sig for ast::Item { text.push_str("mut "); } let name = self.ident.to_string(); - let defs = vec![SigElement { - id: id_from_node_id(self.id, scx), - start: offset + text.len(), - end: offset + text.len() + name.len(), - }]; + let defs = vec![ + SigElement { + id: id_from_node_id(self.id, scx), + start: offset + text.len(), + end: offset + text.len() + name.len(), + }, + ]; text.push_str(&name); text.push_str(": "); @@ -346,11 +355,13 @@ impl Sig for ast::Item { ast::ItemKind::Const(ref ty, ref expr) => { let mut text = "const ".to_owned(); let name = self.ident.to_string(); - let defs = vec![SigElement { - id: id_from_node_id(self.id, scx), - start: offset + text.len(), - end: offset + text.len() + name.len(), - }]; + let defs = vec![ + SigElement { + id: id_from_node_id(self.id, scx), + start: offset + text.len(), + end: offset + text.len() + name.len(), + }, + ]; text.push_str(&name); text.push_str(": "); @@ -379,12 +390,7 @@ impl Sig for ast::Item { } text.push_str("fn "); - let mut sig = name_and_generics(text, - offset, - generics, - self.id, - self.ident, - scx)?; + let mut sig = name_and_generics(text, offset, generics, self.id, self.ident, scx)?; sig.text.push('('); for i in &decl.inputs { @@ -413,11 +419,13 @@ impl Sig for ast::Item { ast::ItemKind::Mod(ref _mod) => { let mut text = "mod ".to_owned(); let name = self.ident.to_string(); - let defs = vec![SigElement { - id: id_from_node_id(self.id, scx), - start: offset + text.len(), - end: offset + text.len() + name.len(), - }]; + let defs = vec![ + SigElement { + id: id_from_node_id(self.id, scx), + start: offset + text.len(), + end: offset + text.len() + name.len(), + }, + ]; text.push_str(&name); // Could be either `mod foo;` or `mod foo { ... }`, but we'll just puck one. text.push(';'); @@ -430,12 +438,7 @@ impl Sig for ast::Item { } ast::ItemKind::Ty(ref ty, ref generics) => { let text = "type ".to_owned(); - let mut sig = name_and_generics(text, - offset, - generics, - self.id, - self.ident, - scx)?; + let mut sig = name_and_generics(text, offset, generics, self.id, self.ident, scx)?; sig.text.push_str(" = "); let ty = ty.make(offset + sig.text.len(), id, scx)?; @@ -446,34 +449,19 @@ impl Sig for ast::Item { } ast::ItemKind::Enum(_, ref generics) => { let text = "enum ".to_owned(); - let mut sig = name_and_generics(text, - offset, - generics, - self.id, - self.ident, - scx)?; + let mut sig = name_and_generics(text, offset, generics, self.id, self.ident, scx)?; sig.text.push_str(" {}"); Ok(sig) } ast::ItemKind::Struct(_, ref generics) => { let text = "struct ".to_owned(); - let mut sig = name_and_generics(text, - offset, - generics, - self.id, - self.ident, - scx)?; + let mut sig = name_and_generics(text, offset, generics, self.id, self.ident, scx)?; sig.text.push_str(" {}"); Ok(sig) } ast::ItemKind::Union(_, ref generics) => { let text = "union ".to_owned(); - let mut sig = name_and_generics(text, - offset, - generics, - self.id, - self.ident, - scx)?; + let mut sig = name_and_generics(text, offset, generics, self.id, self.ident, scx)?; sig.text.push_str(" {}"); Ok(sig) } @@ -488,12 +476,7 @@ impl Sig for ast::Item { text.push_str("unsafe "); } text.push_str("trait "); - let mut sig = name_and_generics(text, - offset, - generics, - self.id, - self.ident, - scx)?; + let mut sig = name_and_generics(text, offset, generics, self.id, self.ident, scx)?; if !bounds.is_empty() { sig.text.push_str(": "); @@ -515,13 +498,15 @@ impl Sig for ast::Item { text.push_str(" for .. {}"); Ok(replace_text(trait_sig, text)) } - ast::ItemKind::Impl(unsafety, - polarity, - defaultness, - ref generics, - ref opt_trait, - ref ty, - _) => { + ast::ItemKind::Impl( + unsafety, + polarity, + defaultness, + ref generics, + ref opt_trait, + ref ty, + _, + ) => { let mut text = String::new(); if let ast::Defaultness::Default = defaultness { text.push_str("default "); @@ -562,8 +547,7 @@ impl Sig for ast::Item { ast::ItemKind::ExternCrate(_) => Err("extern crate"), // FIXME should implement this (e.g., pub use). ast::ItemKind::Use(_) => Err("import"), - ast::ItemKind::Mac(..) | - ast::ItemKind::MacroDef(_) => Err("Macro"), + ast::ItemKind::Mac(..) | ast::ItemKind::MacroDef(_) => Err("Macro"), } } } @@ -573,19 +557,14 @@ impl Sig for ast::Path { let def = scx.get_path_def(id.ok_or("Missing id for Path")?); let (name, start, end) = match def { - Def::Label(..) | - Def::PrimTy(..) | - Def::SelfTy(..) | - Def::Err => { + Def::Label(..) | Def::PrimTy(..) | Def::SelfTy(..) | Def::Err => { return Ok(Signature { text: pprust::path_to_string(self), defs: vec![], refs: vec![], }) } - Def::AssociatedConst(..) | - Def::Variant(..) | - Def::VariantCtor(..) => { + Def::AssociatedConst(..) | Def::Variant(..) | Def::VariantCtor(..) => { let len = self.segments.len(); if len < 2 { return Err("Bad path"); @@ -635,9 +614,11 @@ impl Sig for ast::Generics { if !l.bounds.is_empty() { l_text.push_str(": "); - let bounds = l.bounds.iter().map(|l| { - l.ident.to_string() - }).collect::>().join(" + "); + let bounds = l.bounds + .iter() + .map(|l| l.ident.to_string()) + .collect::>() + .join(" + "); l_text.push_str(&bounds); // FIXME add lifetime bounds refs. } @@ -662,7 +643,11 @@ impl Sig for ast::Generics { } text.push('>'); - Ok(Signature {text, defs, refs: vec![] }) + Ok(Signature { + text, + defs, + refs: vec![], + }) } } @@ -710,11 +695,7 @@ impl Sig for ast::Variant_ { refs.extend(field_sig.refs.into_iter()); } text.push('}'); - Ok(Signature { - text, - defs, - refs, - }) + Ok(Signature { text, defs, refs }) } ast::VariantData::Tuple(ref fields, id) => { let name_def = SigElement { @@ -733,11 +714,7 @@ impl Sig for ast::Variant_ { refs.extend(field_sig.refs.into_iter()); } text.push(')'); - Ok(Signature { - text, - defs, - refs, - }) + Ok(Signature { text, defs, refs }) } ast::VariantData::Unit(id) => { let name_def = SigElement { @@ -763,12 +740,7 @@ impl Sig for ast::ForeignItem { let mut text = String::new(); text.push_str("fn "); - let mut sig = name_and_generics(text, - offset, - generics, - self.id, - self.ident, - scx)?; + let mut sig = name_and_generics(text, offset, generics, self.id, self.ident, scx)?; sig.text.push('('); for i in &decl.inputs { @@ -800,11 +772,13 @@ impl Sig for ast::ForeignItem { text.push_str("mut "); } let name = self.ident.to_string(); - let defs = vec![SigElement { - id: id_from_node_id(self.id, scx), - start: offset + text.len(), - end: offset + text.len() + name.len(), - }]; + let defs = vec![ + SigElement { + id: id_from_node_id(self.id, scx), + start: offset + text.len(), + end: offset + text.len() + name.len(), + }, + ]; text.push_str(&name); text.push_str(": "); @@ -816,11 +790,13 @@ impl Sig for ast::ForeignItem { ast::ForeignItemKind::Ty => { let mut text = "type ".to_owned(); let name = self.ident.to_string(); - let defs = vec![SigElement { - id: id_from_node_id(self.id, scx), - start: offset + text.len(), - end: offset + text.len() + name.len(), - }]; + let defs = vec![ + SigElement { + id: id_from_node_id(self.id, scx), + start: offset + text.len(), + end: offset + text.len() + name.len(), + }, + ]; text.push_str(&name); text.push(';'); @@ -834,13 +810,14 @@ impl Sig for ast::ForeignItem { } } -fn name_and_generics(mut text: String, - offset: usize, - generics: &ast::Generics, - id: NodeId, - name: ast::Ident, - scx: &SaveContext) - -> Result { +fn name_and_generics( + mut text: String, + offset: usize, + generics: &ast::Generics, + id: NodeId, + name: ast::Ident, + scx: &SaveContext, +) -> Result { let name = name.to_string(); let def = SigElement { id: id_from_node_id(id, scx), @@ -855,19 +832,22 @@ fn name_and_generics(mut text: String, } -fn make_assoc_type_signature(id: NodeId, - ident: ast::Ident, - bounds: Option<&ast::TyParamBounds>, - default: Option<&ast::Ty>, - scx: &SaveContext) - -> Result { +fn make_assoc_type_signature( + id: NodeId, + ident: ast::Ident, + bounds: Option<&ast::TyParamBounds>, + default: Option<&ast::Ty>, + scx: &SaveContext, +) -> Result { let mut text = "type ".to_owned(); let name = ident.to_string(); - let mut defs = vec![SigElement { - id: id_from_node_id(id, scx), - start: text.len(), - end: text.len() + name.len(), - }]; + let mut defs = vec![ + SigElement { + id: id_from_node_id(id, scx), + start: text.len(), + end: text.len() + name.len(), + }, + ]; let mut refs = vec![]; text.push_str(&name); if let Some(bounds) = bounds { @@ -886,19 +866,22 @@ fn make_assoc_type_signature(id: NodeId, Ok(Signature { text, defs, refs }) } -fn make_assoc_const_signature(id: NodeId, - ident: ast::Name, - ty: &ast::Ty, - default: Option<&ast::Expr>, - scx: &SaveContext) - -> Result { +fn make_assoc_const_signature( + id: NodeId, + ident: ast::Name, + ty: &ast::Ty, + default: Option<&ast::Expr>, + scx: &SaveContext, +) -> Result { let mut text = "const ".to_owned(); let name = ident.to_string(); - let mut defs = vec![SigElement { - id: id_from_node_id(id, scx), - start: text.len(), - end: text.len() + name.len(), - }]; + let mut defs = vec![ + SigElement { + id: id_from_node_id(id, scx), + start: text.len(), + end: text.len() + name.len(), + }, + ]; let mut refs = vec![]; text.push_str(&name); text.push_str(": "); @@ -916,12 +899,13 @@ fn make_assoc_const_signature(id: NodeId, Ok(Signature { text, defs, refs }) } -fn make_method_signature(id: NodeId, - ident: ast::Ident, - generics: &ast::Generics, - m: &ast::MethodSig, - scx: &SaveContext) - -> Result { +fn make_method_signature( + id: NodeId, + ident: ast::Ident, + generics: &ast::Generics, + m: &ast::MethodSig, + scx: &SaveContext, +) -> Result { // FIXME code dup with function signature let mut text = String::new(); if m.constness.node == ast::Constness::Const { @@ -937,12 +921,7 @@ fn make_method_signature(id: NodeId, } text.push_str("fn "); - let mut sig = name_and_generics(text, - 0, - generics, - id, - ident, - scx)?; + let mut sig = name_and_generics(text, 0, generics, id, ident, scx)?; sig.text.push('('); for i in &m.decl.inputs { diff --git a/src/librustc_save_analysis/span_utils.rs b/src/librustc_save_analysis/span_utils.rs index 5bfb4d1b3b2..ff1a8541e06 100644 --- a/src/librustc_save_analysis/span_utils.rs +++ b/src/librustc_save_analysis/span_utils.rs @@ -42,7 +42,11 @@ impl<'a> SpanUtils<'a> { if path.is_absolute() { path.clone().display().to_string() } else { - env::current_dir().unwrap().join(&path).display().to_string() + env::current_dir() + .unwrap() + .join(&path) + .display() + .to_string() } } @@ -66,7 +70,7 @@ impl<'a> SpanUtils<'a> { loop { let ts = toks.real_token(); if ts.tok == token::Eof { - return result + return result; } if bracket_count == 0 && (ts.tok.is_ident() || ts.tok.is_keyword(keywords::SelfValue)) { result = Some(ts.sp); @@ -122,10 +126,9 @@ impl<'a> SpanUtils<'a> { loop { let next = toks.real_token(); - if (next.tok == token::Lt || next.tok == token::Colon) && - angle_count == 0 && - bracket_count == 0 && - prev.tok.is_ident() { + if (next.tok == token::Lt || next.tok == token::Colon) && angle_count == 0 + && bracket_count == 0 && prev.tok.is_ident() + { result = Some(prev.sp); } @@ -152,12 +155,14 @@ impl<'a> SpanUtils<'a> { } if angle_count != 0 || bracket_count != 0 { let loc = self.sess.codemap().lookup_char_pos(span.lo()); - span_bug!(span, - "Mis-counted brackets when breaking path? Parsing '{}' \ - in {}, line {}", - self.snippet(span), - loc.file.name, - loc.line); + span_bug!( + span, + "Mis-counted brackets when breaking path? Parsing '{}' \ + in {}, line {}", + self.snippet(span), + loc.file.name, + loc.line + ); } if result.is_none() && prev.tok.is_ident() && angle_count == 0 { return Some(prev.sp); @@ -211,7 +216,7 @@ impl<'a> SpanUtils<'a> { if f(ts.tok) { let ts = toks.real_token(); if ts.tok == token::Eof { - return None + return None; } else { return Some(ts.sp); } @@ -278,7 +283,12 @@ impl<'a> SpanUtils<'a> { }; //If the span comes from a fake filemap, filter it. - if !self.sess.codemap().lookup_char_pos(parent.lo()).file.is_real_file() { + if !self.sess + .codemap() + .lookup_char_pos(parent.lo()) + .file + .is_real_file() + { return true; }