Skip to content

Commit 641274f

Browse files
committed
Resolve imports during expansion.
1 parent 9071206 commit 641274f

File tree

6 files changed

+26
-24
lines changed

6 files changed

+26
-24
lines changed

src/librustc_driver/driver.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,6 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
755755
|| ast_validation::check_crate(sess, &krate));
756756

757757
time(sess.time_passes(), "name resolution", || -> CompileResult {
758-
resolver.resolve_imports();
759-
760758
// Since import resolution will eventually happen in expansion,
761759
// don't perform `after_expand` until after import resolution.
762760
after_expand(&krate)?;

src/librustc_resolve/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ use std::fmt;
7676
use std::mem::replace;
7777
use std::rc::Rc;
7878

79-
use resolve_imports::{ImportDirective, ImportDirectiveSubclass, NameResolution};
79+
use resolve_imports::{ImportDirective, ImportDirectiveSubclass, NameResolution, ImportResolver};
8080
use macros::{InvocationData, LegacyBinding, LegacyScope};
8181

8282
// NB: This module needs to be declared first so diagnostics are
@@ -1335,6 +1335,7 @@ impl<'a> Resolver<'a> {
13351335

13361336
/// Entry point to crate resolution.
13371337
pub fn resolve_crate(&mut self, krate: &Crate) {
1338+
ImportResolver { resolver: self }.finalize_imports();
13381339
self.current_module = self.graph_root;
13391340
visit::walk_crate(self, krate);
13401341

src/librustc_resolve/macros.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
use {Module, ModuleKind, Resolver};
1212
use build_reduced_graph::BuildReducedGraphVisitor;
13+
use resolve_imports::ImportResolver;
1314
use rustc::hir::def_id::{DefId, BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, DefIndex};
1415
use rustc::hir::def::{Def, Export};
1516
use rustc::hir::map::{self, DefCollector};
@@ -185,6 +186,10 @@ impl<'a> base::Resolver for Resolver<'a> {
185186
self.macros_at_scope.insert(id, macros);
186187
}
187188

189+
fn resolve_imports(&mut self) {
190+
ImportResolver { resolver: self }.resolve_imports()
191+
}
192+
188193
fn find_attr_invoc(&mut self, attrs: &mut Vec<ast::Attribute>) -> Option<ast::Attribute> {
189194
for i in 0..attrs.len() {
190195
let name = intern(&attrs[i].name());

src/librustc_resolve/resolve_imports.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ use syntax_pos::Span;
3232
use std::cell::{Cell, RefCell};
3333
use std::mem;
3434

35-
impl<'a> Resolver<'a> {
36-
pub fn resolve_imports(&mut self) {
37-
ImportResolver { resolver: self }.resolve_imports();
38-
}
39-
}
40-
4135
/// Contains data for specific types of import directives.
4236
#[derive(Clone, Debug)]
4337
pub enum ImportDirectiveSubclass<'a> {
@@ -399,8 +393,8 @@ impl<'a> Resolver<'a> {
399393
}
400394
}
401395

402-
struct ImportResolver<'a, 'b: 'a> {
403-
resolver: &'a mut Resolver<'b>,
396+
pub struct ImportResolver<'a, 'b: 'a> {
397+
pub resolver: &'a mut Resolver<'b>,
404398
}
405399

406400
impl<'a, 'b: 'a> ::std::ops::Deref for ImportResolver<'a, 'b> {
@@ -433,28 +427,21 @@ impl<'a, 'b:'a> ImportResolver<'a, 'b> {
433427

434428
/// Resolves all imports for the crate. This method performs the fixed-
435429
/// point iteration.
436-
fn resolve_imports(&mut self) {
437-
let mut i = 0;
430+
pub fn resolve_imports(&mut self) {
438431
let mut prev_num_indeterminates = self.indeterminate_imports.len() + 1;
439-
440432
while self.indeterminate_imports.len() < prev_num_indeterminates {
441433
prev_num_indeterminates = self.indeterminate_imports.len();
442-
debug!("(resolving imports) iteration {}, {} imports left", i, prev_num_indeterminates);
443-
444-
let mut imports = Vec::new();
445-
::std::mem::swap(&mut imports, &mut self.indeterminate_imports);
446-
447-
for import in imports {
434+
for import in mem::replace(&mut self.indeterminate_imports, Vec::new()) {
448435
match self.resolve_import(&import) {
449436
Failed(_) => self.determined_imports.push(import),
450437
Indeterminate => self.indeterminate_imports.push(import),
451438
Success(()) => self.determined_imports.push(import),
452439
}
453440
}
454-
455-
i += 1;
456441
}
442+
}
457443

444+
pub fn finalize_imports(&mut self) {
458445
for module in self.arenas.local_modules().iter() {
459446
self.finalize_resolutions_in(module);
460447
}

src/libsyntax/ext/base.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ pub trait Resolver {
524524
fn add_ext(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>);
525525
fn add_expansions_at_stmt(&mut self, id: ast::NodeId, macros: Vec<Mark>);
526526

527+
fn resolve_imports(&mut self);
527528
fn find_attr_invoc(&mut self, attrs: &mut Vec<Attribute>) -> Option<Attribute>;
528529
fn resolve_macro(&mut self, scope: Mark, path: &ast::Path, force: bool)
529530
-> Result<Rc<SyntaxExtension>, Determinacy>;
@@ -547,6 +548,7 @@ impl Resolver for DummyResolver {
547548
fn add_ext(&mut self, _ident: ast::Ident, _ext: Rc<SyntaxExtension>) {}
548549
fn add_expansions_at_stmt(&mut self, _id: ast::NodeId, _macros: Vec<Mark>) {}
549550

551+
fn resolve_imports(&mut self) {}
550552
fn find_attr_invoc(&mut self, _attrs: &mut Vec<Attribute>) -> Option<Attribute> { None }
551553
fn resolve_macro(&mut self, _scope: Mark, _path: &ast::Path, _force: bool)
552554
-> Result<Rc<SyntaxExtension>, Determinacy> {

src/libsyntax/ext/expand.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
222222
self.cx.current_expansion.depth = 0;
223223

224224
let (expansion, mut invocations) = self.collect_invocations(expansion);
225+
self.resolve_imports();
225226
invocations.reverse();
226227

227228
let mut expansions = Vec::new();
@@ -230,9 +231,9 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
230231
loop {
231232
let invoc = if let Some(invoc) = invocations.pop() {
232233
invoc
233-
} else if undetermined_invocations.is_empty() {
234-
break
235234
} else {
235+
self.resolve_imports();
236+
if undetermined_invocations.is_empty() { break }
236237
invocations = mem::replace(&mut undetermined_invocations, Vec::new());
237238
force = !mem::replace(&mut progress, false);
238239
continue
@@ -292,6 +293,14 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
292293
expansion.fold_with(&mut placeholder_expander)
293294
}
294295

296+
fn resolve_imports(&mut self) {
297+
if self.monotonic {
298+
let err_count = self.cx.parse_sess.span_diagnostic.err_count();
299+
self.cx.resolver.resolve_imports();
300+
self.cx.resolve_err_count += self.cx.parse_sess.span_diagnostic.err_count() - err_count;
301+
}
302+
}
303+
295304
fn collect_invocations(&mut self, expansion: Expansion) -> (Expansion, Vec<Invocation>) {
296305
let result = {
297306
let mut collector = InvocationCollector {

0 commit comments

Comments
 (0)