Skip to content

Commit b3a81ee

Browse files
committed
Build the reduced graph during expansion.
1 parent ebaaafc commit b3a81ee

File tree

4 files changed

+61
-26
lines changed

4 files changed

+61
-26
lines changed

src/librustc_driver/driver.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,12 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
639639
}
640640
sess.track_errors(|| sess.lint_store.borrow_mut().process_command_line(sess))?;
641641

642+
// Currently, we ignore the name resolution data structures for the purposes of dependency
643+
// tracking. Instead we will run name resolution and include its output in the hash of each
644+
// item, much like we do for macro expansion. In other words, the hash reflects not just
645+
// its contents but the results of name resolution on those contents. Hopefully we'll push
646+
// this back at some point.
647+
let _ignore = sess.dep_graph.in_ignore();
642648
let mut crate_loader = CrateLoader::new(sess, &cstore, &krate, crate_name);
643649
let resolver_arenas = Resolver::arenas();
644650
let mut resolver =
@@ -742,13 +748,6 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
742748
|| ast_validation::check_crate(sess, &krate));
743749

744750
time(sess.time_passes(), "name resolution", || -> CompileResult {
745-
// Currently, we ignore the name resolution data structures for the purposes of dependency
746-
// tracking. Instead we will run name resolution and include its output in the hash of each
747-
// item, much like we do for macro expansion. In other words, the hash reflects not just
748-
// its contents but the results of name resolution on those contents. Hopefully we'll push
749-
// this back at some point.
750-
let _ignore = sess.dep_graph.in_ignore();
751-
resolver.build_reduced_graph(&krate);
752751
resolver.resolve_imports();
753752

754753
// Since import resolution will eventually happen in expansion,

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ use syntax::ast::Name;
3131
use syntax::attr;
3232
use syntax::parse::token;
3333

34-
use syntax::ast::{Block, Crate};
35-
use syntax::ast::{ForeignItem, ForeignItemKind, Item, ItemKind};
34+
use syntax::ast::{self, Block, ForeignItem, ForeignItemKind, Item, ItemKind};
3635
use syntax::ast::{Mutability, StmtKind, TraitItem, TraitItemKind};
3736
use syntax::ast::{Variant, ViewPathGlob, ViewPathList, ViewPathSimple};
3837
use syntax::parse::token::keywords;
@@ -53,11 +52,6 @@ impl<'a> ToNameBinding<'a> for (Def, Span, ty::Visibility) {
5352
}
5453

5554
impl<'b> Resolver<'b> {
56-
/// Constructs the reduced graph for the entire crate.
57-
pub fn build_reduced_graph(&mut self, krate: &Crate) {
58-
visit::walk_crate(&mut BuildReducedGraphVisitor { resolver: self }, krate);
59-
}
60-
6155
/// Defines `name` in namespace `ns` of module `parent` to be `def` if it is not yet defined;
6256
/// otherwise, reports an error.
6357
fn define<T>(&mut self, parent: Module<'b>, name: Name, ns: Namespace, def: T)
@@ -72,7 +66,7 @@ impl<'b> Resolver<'b> {
7266
fn block_needs_anonymous_module(&mut self, block: &Block) -> bool {
7367
// If any statements are items, we need to create an anonymous module
7468
block.stmts.iter().any(|statement| match statement.node {
75-
StmtKind::Item(_) => true,
69+
StmtKind::Item(_) | StmtKind::Mac(_) => true,
7670
_ => false,
7771
})
7872
}
@@ -206,6 +200,8 @@ impl<'b> Resolver<'b> {
206200
}
207201
}
208202

203+
ItemKind::Mod(..) if item.ident == keywords::Invalid.ident() => {} // Crate root
204+
209205
ItemKind::Mod(..) => {
210206
let def = Def::Mod(self.definitions.local_def_id(item.id));
211207
let module = self.arenas.alloc_module(ModuleS {
@@ -478,12 +474,42 @@ impl<'b> Resolver<'b> {
478474
}
479475
}
480476

481-
struct BuildReducedGraphVisitor<'a, 'b: 'a> {
482-
resolver: &'a mut Resolver<'b>,
477+
pub struct BuildReducedGraphVisitor<'a, 'b: 'a> {
478+
pub resolver: &'a mut Resolver<'b>,
479+
}
480+
481+
impl<'a, 'b> BuildReducedGraphVisitor<'a, 'b> {
482+
fn visit_invoc(&mut self, id: ast::NodeId) {
483+
self.resolver.expansion_data.get_mut(&id.as_u32()).unwrap().module2 =
484+
self.resolver.current_module;
485+
}
486+
}
487+
488+
macro_rules! method {
489+
($visit:ident: $ty:ty, $invoc:path, $walk:ident) => {
490+
fn $visit(&mut self, node: &$ty) {
491+
match node.node {
492+
$invoc(..) => self.visit_invoc(node.id),
493+
_ => visit::$walk(self, node),
494+
}
495+
}
496+
}
483497
}
484498

485499
impl<'a, 'b> Visitor for BuildReducedGraphVisitor<'a, 'b> {
500+
method!(visit_impl_item: ast::ImplItem, ast::ImplItemKind::Macro, walk_impl_item);
501+
method!(visit_stmt: ast::Stmt, ast::StmtKind::Mac, walk_stmt);
502+
method!(visit_expr: ast::Expr, ast::ExprKind::Mac, walk_expr);
503+
method!(visit_pat: ast::Pat, ast::PatKind::Mac, walk_pat);
504+
method!(visit_ty: ast::Ty, ast::TyKind::Mac, walk_ty);
505+
486506
fn visit_item(&mut self, item: &Item) {
507+
match item.node {
508+
ItemKind::Mac(..) if item.id == ast::DUMMY_NODE_ID => return, // Scope placeholder
509+
ItemKind::Mac(..) => return self.visit_invoc(item.id),
510+
_ => {}
511+
}
512+
487513
let parent = self.resolver.current_module;
488514
self.resolver.build_reduced_graph_for_item(item);
489515
visit::walk_item(self, item);
@@ -492,6 +518,7 @@ impl<'a, 'b> Visitor for BuildReducedGraphVisitor<'a, 'b> {
492518

493519
fn visit_foreign_item(&mut self, foreign_item: &ForeignItem) {
494520
self.resolver.build_reduced_graph_for_foreign_item(foreign_item);
521+
visit::walk_foreign_item(self, foreign_item);
495522
}
496523

497524
fn visit_block(&mut self, block: &Block) {
@@ -515,7 +542,7 @@ impl<'a, 'b> Visitor for BuildReducedGraphVisitor<'a, 'b> {
515542
(Def::Method(item_def_id), ValueNS)
516543
}
517544
TraitItemKind::Type(..) => (Def::AssociatedTy(item_def_id), TypeNS),
518-
TraitItemKind::Macro(_) => panic!("unexpanded macro in resolve!"),
545+
TraitItemKind::Macro(_) => return self.visit_invoc(item.id),
519546
};
520547

521548
self.resolver.trait_item_map.insert((item.ident.name, def_id), is_static_method);

src/librustc_resolve/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ pub struct Resolver<'a> {
10741074
macro_names: FnvHashSet<Name>,
10751075

10761076
// Maps the `Mark` of an expansion to its containing module or block.
1077-
expansion_data: FnvHashMap<u32, macros::ExpansionData>,
1077+
expansion_data: FnvHashMap<u32, macros::ExpansionData<'a>>,
10781078
}
10791079

10801080
pub struct ResolverArenas<'a> {
@@ -1192,7 +1192,7 @@ impl<'a> Resolver<'a> {
11921192
DefCollector::new(&mut definitions).collect_root();
11931193

11941194
let mut expansion_data = FnvHashMap();
1195-
expansion_data.insert(0, macros::ExpansionData::root()); // Crate root expansion
1195+
expansion_data.insert(0, macros::ExpansionData::root(graph_root)); // Crate root expansion
11961196

11971197
Resolver {
11981198
session: session,

src/librustc_resolve/macros.rs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use Resolver;
11+
use {Module, Resolver};
12+
use build_reduced_graph::BuildReducedGraphVisitor;
1213
use rustc::hir::def_id::{CRATE_DEF_INDEX, DefIndex};
1314
use rustc::hir::map::DefCollector;
1415
use rustc::middle::cstore::LoadedMacro;
@@ -30,16 +31,18 @@ use syntax::visit::{self, Visitor};
3031
use syntax_pos::Span;
3132

3233
#[derive(Clone)]
33-
pub struct ExpansionData {
34+
pub struct ExpansionData<'a> {
3435
module: Rc<ModuleData>,
3536
def_index: DefIndex,
37+
pub module2: Module<'a>,
3638
}
3739

38-
impl ExpansionData {
39-
pub fn root() -> Self {
40+
impl<'a> ExpansionData<'a> {
41+
pub fn root(graph_root: Module<'a>) -> Self {
4042
ExpansionData {
4143
module: Default::default(),
4244
def_index: CRATE_DEF_INDEX,
45+
module2: graph_root,
4346
}
4447
}
4548
}
@@ -58,10 +61,14 @@ impl<'a> base::Resolver for Resolver<'a> {
5861
}
5962

6063
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion) {
61-
let module = self.expansion_data[&mark.as_u32()].module.clone();
62-
let mut visitor = ExpansionVisitor { current_module: module, resolver: self };
64+
let expansion_data = self.expansion_data[&mark.as_u32()].clone();
65+
self.current_module = expansion_data.module2;
66+
let mut visitor =
67+
ExpansionVisitor { current_module: expansion_data.module, resolver: self };
68+
6369
visitor.collect_def_ids(mark, expansion);
6470
expansion.visit_with(&mut visitor);
71+
expansion.visit_with(&mut BuildReducedGraphVisitor { resolver: visitor.resolver });
6572
}
6673

6774
fn add_macro(&mut self, scope: Mark, mut def: ast::MacroDef) {
@@ -210,11 +217,13 @@ impl<'a, 'b> ExpansionVisitor<'a, 'b> {
210217
fn collect_def_ids(&mut self, mark: Mark, expansion: &Expansion) {
211218
let expansion_data = &mut self.resolver.expansion_data;
212219
let module = &self.current_module;
220+
let module2 = self.resolver.current_module;
213221
let def_index = expansion_data[&mark.as_u32()].def_index;
214222
let visit_macro_invoc = &mut |id: ast::NodeId, def_index| {
215223
expansion_data.insert(id.as_u32(), ExpansionData {
216224
def_index: def_index,
217225
module: module.clone(),
226+
module2: module2,
218227
});
219228
};
220229

0 commit comments

Comments
 (0)