From 67a0d27c6529de335304b10c6ae6b0eed77bfc79 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Thu, 11 May 2017 17:45:27 +1200 Subject: [PATCH 1/2] Ensure we walk the root module of the crate --- src/librustc_save_analysis/data.rs | 2 +- src/librustc_save_analysis/dump_visitor.rs | 27 +++++++++++++++++++ src/librustc_save_analysis/external_data.rs | 4 +-- src/librustc_save_analysis/json_api_dumper.rs | 2 +- src/librustc_save_analysis/json_dumper.rs | 2 +- src/librustc_save_analysis/lib.rs | 2 +- 6 files changed, 33 insertions(+), 6 deletions(-) diff --git a/src/librustc_save_analysis/data.rs b/src/librustc_save_analysis/data.rs index d4ded71a33390..cac1a2e3c5af1 100644 --- a/src/librustc_save_analysis/data.rs +++ b/src/librustc_save_analysis/data.rs @@ -267,7 +267,7 @@ pub struct ModData { pub items: Vec, pub visibility: Visibility, pub docs: String, - pub sig: Signature, + pub sig: Option, pub attributes: Vec, } diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index 763414c1a555f..dd022dee78cba 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -1211,6 +1211,33 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> { } impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll, D> { + fn visit_mod(&mut self, m: &'l ast::Mod, span: Span, id: NodeId) { + // Since we handle explicit modules ourselves in visit_item, this should + // only get called for the root module of a crate. + assert_eq!(id, ast::CRATE_NODE_ID); + + let qualname = format!("::{}", self.tcx.node_path_str(id)); + + let cm = self.tcx.sess.codemap(); + let filename = cm.span_to_filename(span); + self.dumper.mod_data(ModData { + id: id, + name: String::new(), + qualname: qualname, + span: span, + scope: id, + filename: filename, + items: m.items.iter().map(|i| i.id).collect(), + visibility: Visibility::Public, + // TODO Visitor doesn't pass us the attibutes. + docs: String::new(), + sig: None, + // TODO Visitor doesn't pass us the attibutes. + attributes: vec![], + }.lower(self.tcx)); + self.nest_scope(id, |v| visit::walk_mod(v, m)); + } + fn visit_item(&mut self, item: &'l ast::Item) { use syntax::ast::ItemKind::*; self.process_macro_use(item.span, item.id); diff --git a/src/librustc_save_analysis/external_data.rs b/src/librustc_save_analysis/external_data.rs index 6fd2de97767e1..02441a0587eb6 100644 --- a/src/librustc_save_analysis/external_data.rs +++ b/src/librustc_save_analysis/external_data.rs @@ -392,7 +392,7 @@ pub struct ModData { pub items: Vec, pub visibility: Visibility, pub docs: String, - pub sig: Signature, + pub sig: Option, pub attributes: Vec, } @@ -410,7 +410,7 @@ impl Lower for data::ModData { items: self.items.into_iter().map(|id| make_def_id(id, &tcx.hir)).collect(), visibility: self.visibility, docs: self.docs, - sig: self.sig.lower(tcx), + sig: self.sig.map(|s| s.lower(tcx)), attributes: self.attributes.lower(tcx), } } diff --git a/src/librustc_save_analysis/json_api_dumper.rs b/src/librustc_save_analysis/json_api_dumper.rs index 41221ad986379..49b14f5eca072 100644 --- a/src/librustc_save_analysis/json_api_dumper.rs +++ b/src/librustc_save_analysis/json_api_dumper.rs @@ -293,7 +293,7 @@ impl Into> for ModData { parent: None, decl_id: None, docs: self.docs, - sig: Some(self.sig.into()), + sig: self.sig.map(|s| s.into()), attributes: vec![], }), _ => None, diff --git a/src/librustc_save_analysis/json_dumper.rs b/src/librustc_save_analysis/json_dumper.rs index 2d1e12bf0a10d..eaa0c0825f0e9 100644 --- a/src/librustc_save_analysis/json_dumper.rs +++ b/src/librustc_save_analysis/json_dumper.rs @@ -121,7 +121,7 @@ impl<'b, O: DumpOutput + 'b> Dump for JsonDumper { children: data.items.into_iter().map(|id| id_from_def_id(id)).collect(), decl_id: None, docs: data.docs, - sig: Some(data.sig.into()), + sig: data.sig.map(|s| s.into()), attributes: data.attributes.into_iter().map(|a| a.into()).collect(), }; if def.span.file_name.to_str().unwrap() != def.value { diff --git a/src/librustc_save_analysis/lib.rs b/src/librustc_save_analysis/lib.rs index fd6898d19d84f..9283c6d7b5380 100644 --- a/src/librustc_save_analysis/lib.rs +++ b/src/librustc_save_analysis/lib.rs @@ -257,7 +257,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> { items: m.items.iter().map(|i| i.id).collect(), visibility: From::from(&item.vis), docs: docs_for_attrs(&item.attrs), - sig: self.sig_base(item), + sig: Some(self.sig_base(item)), attributes: item.attrs.clone(), })) } From fb7ba4772cdad24083fa73552425d4f91fd8be33 Mon Sep 17 00:00:00 2001 From: Nick Cameron Date: Fri, 12 May 2017 07:15:29 +1200 Subject: [PATCH 2/2] Pass crate attributes in visit.rs --- src/librustc/lint/context.rs | 2 +- src/librustc_passes/hir_stats.rs | 2 +- src/librustc_save_analysis/dump_visitor.rs | 8 +++----- src/libsyntax/util/node_count.rs | 2 +- src/libsyntax/visit.rs | 8 +++++--- src/libsyntax_ext/proc_macro_registrar.rs | 2 +- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index 6f3e84247f797..60e671f1772e3 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -1055,7 +1055,7 @@ impl<'a> ast_visit::Visitor<'a> for EarlyContext<'a> { run_lints!(self, check_ident, early_passes, sp, id); } - fn visit_mod(&mut self, m: &'a ast::Mod, s: Span, n: ast::NodeId) { + fn visit_mod(&mut self, m: &'a ast::Mod, s: Span, _a: &[ast::Attribute], n: ast::NodeId) { run_lints!(self, check_mod, early_passes, m, s, n); ast_visit::walk_mod(self, m); run_lints!(self, check_mod_post, early_passes, m, s, n); diff --git a/src/librustc_passes/hir_stats.rs b/src/librustc_passes/hir_stats.rs index 749146fe49672..29fac5463e557 100644 --- a/src/librustc_passes/hir_stats.rs +++ b/src/librustc_passes/hir_stats.rs @@ -252,7 +252,7 @@ impl<'v> hir_visit::Visitor<'v> for StatCollector<'v> { impl<'v> ast_visit::Visitor<'v> for StatCollector<'v> { - fn visit_mod(&mut self, m: &'v ast::Mod, _s: Span, _n: NodeId) { + fn visit_mod(&mut self, m: &'v ast::Mod, _s: Span, _a: &[ast::Attribute], _n: NodeId) { self.record("Mod", Id::None, m); ast_visit::walk_mod(self, m) } diff --git a/src/librustc_save_analysis/dump_visitor.rs b/src/librustc_save_analysis/dump_visitor.rs index dd022dee78cba..a95236e2a5072 100644 --- a/src/librustc_save_analysis/dump_visitor.rs +++ b/src/librustc_save_analysis/dump_visitor.rs @@ -1211,7 +1211,7 @@ impl<'l, 'tcx: 'l, 'll, D: Dump + 'll> DumpVisitor<'l, 'tcx, 'll, D> { } impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll, D> { - fn visit_mod(&mut self, m: &'l ast::Mod, span: Span, id: NodeId) { + fn visit_mod(&mut self, m: &'l ast::Mod, span: Span, attrs: &[ast::Attribute], id: NodeId) { // Since we handle explicit modules ourselves in visit_item, this should // only get called for the root module of a crate. assert_eq!(id, ast::CRATE_NODE_ID); @@ -1229,11 +1229,9 @@ impl<'l, 'tcx: 'l, 'll, D: Dump +'ll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll, filename: filename, items: m.items.iter().map(|i| i.id).collect(), visibility: Visibility::Public, - // TODO Visitor doesn't pass us the attibutes. - docs: String::new(), + docs: docs_for_attrs(attrs), sig: None, - // TODO Visitor doesn't pass us the attibutes. - attributes: vec![], + attributes: attrs.to_owned(), }.lower(self.tcx)); self.nest_scope(id, |v| visit::walk_mod(v, m)); } diff --git a/src/libsyntax/util/node_count.rs b/src/libsyntax/util/node_count.rs index 9d9957a0f4534..0a5d0c2e7fe01 100644 --- a/src/libsyntax/util/node_count.rs +++ b/src/libsyntax/util/node_count.rs @@ -31,7 +31,7 @@ impl<'ast> Visitor<'ast> for NodeCounter { self.count += 1; walk_ident(self, span, ident); } - fn visit_mod(&mut self, m: &Mod, _s: Span, _n: NodeId) { + fn visit_mod(&mut self, m: &Mod, _s: Span, _a: &[Attribute], _n: NodeId) { self.count += 1; walk_mod(self, m) } diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index 2e42c6986e64e..9288d95009c1d 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -56,7 +56,9 @@ pub trait Visitor<'ast>: Sized { fn visit_ident(&mut self, span: Span, ident: Ident) { walk_ident(self, span, ident); } - fn visit_mod(&mut self, m: &'ast Mod, _s: Span, _n: NodeId) { walk_mod(self, m) } + fn visit_mod(&mut self, m: &'ast Mod, _s: Span, _attrs: &[Attribute], _n: NodeId) { + walk_mod(self, m); + } fn visit_foreign_item(&mut self, i: &'ast ForeignItem) { walk_foreign_item(self, i) } fn visit_global_asm(&mut self, ga: &'ast GlobalAsm) { walk_global_asm(self, ga) } fn visit_item(&mut self, i: &'ast Item) { walk_item(self, i) } @@ -172,7 +174,7 @@ pub fn walk_ident<'a, V: Visitor<'a>>(visitor: &mut V, span: Span, ident: Ident) } pub fn walk_crate<'a, V: Visitor<'a>>(visitor: &mut V, krate: &'a Crate) { - visitor.visit_mod(&krate.module, krate.span, CRATE_NODE_ID); + visitor.visit_mod(&krate.module, krate.span, &krate.attrs, CRATE_NODE_ID); walk_list!(visitor, visit_attribute, &krate.attrs); } @@ -249,7 +251,7 @@ pub fn walk_item<'a, V: Visitor<'a>>(visitor: &mut V, item: &'a Item) { item.id) } ItemKind::Mod(ref module) => { - visitor.visit_mod(module, item.span, item.id) + visitor.visit_mod(module, item.span, &item.attrs, item.id) } ItemKind::ForeignMod(ref foreign_module) => { walk_list!(visitor, visit_foreign_item, &foreign_module.items); diff --git a/src/libsyntax_ext/proc_macro_registrar.rs b/src/libsyntax_ext/proc_macro_registrar.rs index bb89caab709b0..6318abec69f06 100644 --- a/src/libsyntax_ext/proc_macro_registrar.rs +++ b/src/libsyntax_ext/proc_macro_registrar.rs @@ -329,7 +329,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> { visit::walk_item(self, item); } - fn visit_mod(&mut self, m: &'a ast::Mod, _s: Span, id: NodeId) { + fn visit_mod(&mut self, m: &'a ast::Mod, _s: Span, _a: &[ast::Attribute], id: NodeId) { let mut prev_in_root = self.in_root; if id != ast::CRATE_NODE_ID { prev_in_root = mem::replace(&mut self.in_root, false);