Skip to content

Commit dd3738a

Browse files
committed
resolve: Record full parent scope data for imports
1 parent cae6efc commit dd3738a

File tree

3 files changed

+52
-46
lines changed

3 files changed

+52
-46
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
126126
mut uniform_paths_canary_emitted: bool,
127127
nested: bool,
128128
item: &Item,
129-
expansion: Mark,
129+
parent_scope: ParentScope<'a>,
130130
) {
131131
debug!("build_reduced_graph_for_use_tree(parent_prefix={:?}, \
132132
uniform_paths_canary_emitted={}, \
@@ -224,7 +224,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
224224
root_use_tree.span,
225225
root_id,
226226
ty::Visibility::Invisible,
227-
expansion,
227+
parent_scope.clone(),
228228
true, // is_uniform_paths_canary
229229
);
230230
};
@@ -354,7 +354,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
354354
root_use_tree.span,
355355
root_id,
356356
vis,
357-
expansion,
357+
parent_scope,
358358
false, // is_uniform_paths_canary
359359
);
360360
}
@@ -371,7 +371,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
371371
root_use_tree.span,
372372
root_id,
373373
vis,
374-
expansion,
374+
parent_scope,
375375
false, // is_uniform_paths_canary
376376
);
377377
}
@@ -409,16 +409,17 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
409409
uniform_paths_canary_emitted,
410410
true,
411411
item,
412-
expansion,
412+
parent_scope.clone(),
413413
);
414414
}
415415
}
416416
}
417417
}
418418

419419
/// Constructs the reduced graph for one item.
420-
fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
421-
let parent = self.current_module;
420+
fn build_reduced_graph_for_item(&mut self, item: &Item, parent_scope: ParentScope<'a>) {
421+
let parent = parent_scope.module;
422+
let expansion = parent_scope.expansion;
422423
let ident = item.ident;
423424
let sp = item.span;
424425
let vis = self.resolve_visibility(&item.vis);
@@ -435,7 +436,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
435436
false, // uniform_paths_canary_emitted
436437
false,
437438
item,
438-
expansion,
439+
parent_scope,
439440
);
440441
}
441442

@@ -448,7 +449,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
448449
self.injected_crate = Some(module);
449450
}
450451

451-
let used = self.process_legacy_macro_imports(item, module, expansion);
452+
let used = self.process_legacy_macro_imports(item, module, &parent_scope);
452453
let binding =
453454
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.arenas);
454455
if ptr::eq(self.current_module, self.graph_root) {
@@ -473,7 +474,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
473474
let directive = self.arenas.alloc_import_directive(ImportDirective {
474475
root_id: item.id,
475476
id: item.id,
476-
parent,
477+
parent_scope,
477478
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
478479
subclass: ImportDirectiveSubclass::ExternCrate {
479480
source: orig_name,
@@ -483,7 +484,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
483484
span: item.span,
484485
module_path: Vec::new(),
485486
vis: Cell::new(vis),
486-
expansion,
487487
used: Cell::new(used),
488488
is_uniform_paths_canary: false,
489489
});
@@ -856,9 +856,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
856856
}
857857

858858
// This returns true if we should consider the underlying `extern crate` to be used.
859-
fn process_legacy_macro_imports(&mut self, item: &Item, module: Module<'a>, expansion: Mark)
860-
-> bool {
861-
let allow_shadowing = expansion == Mark::root();
859+
fn process_legacy_macro_imports(&mut self, item: &Item, module: Module<'a>,
860+
parent_scope: &ParentScope<'a>) -> bool {
861+
let allow_shadowing = parent_scope.expansion == Mark::root();
862862
let legacy_imports = self.legacy_macro_imports(&item.attrs);
863863
let used = legacy_imports != LegacyMacroImports::default();
864864

@@ -868,18 +868,17 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
868868
"an `extern crate` loading macros must be at the crate root");
869869
}
870870

871-
let (graph_root, arenas) = (self.graph_root, self.arenas);
871+
let arenas = self.arenas;
872872
let macro_use_directive = |span| arenas.alloc_import_directive(ImportDirective {
873873
root_id: item.id,
874874
id: item.id,
875-
parent: graph_root,
875+
parent_scope: parent_scope.clone(),
876876
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
877877
subclass: ImportDirectiveSubclass::MacroUse,
878878
root_span: span,
879879
span,
880880
module_path: Vec::new(),
881881
vis: Cell::new(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))),
882-
expansion,
883882
used: Cell::new(false),
884883
is_uniform_paths_canary: false,
885884
});
@@ -1010,7 +1009,13 @@ impl<'a, 'b, 'cl> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b, 'cl> {
10101009

10111010
let orig_current_module = self.resolver.current_module;
10121011
let orig_current_legacy_scope = self.current_legacy_scope;
1013-
self.resolver.build_reduced_graph_for_item(item, self.expansion);
1012+
let parent_scope = ParentScope {
1013+
module: self.resolver.current_module,
1014+
expansion: self.expansion,
1015+
legacy: self.current_legacy_scope,
1016+
derives: Vec::new(),
1017+
};
1018+
self.resolver.build_reduced_graph_for_item(item, parent_scope);
10141019
visit::walk_item(self, item);
10151020
self.resolver.current_module = orig_current_module;
10161021
if !macro_use {

src/librustc_resolve/macros.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use std::cell::Cell;
4343
use std::mem;
4444
use rustc_data_structures::sync::Lrc;
4545

46-
#[derive(Clone)]
46+
#[derive(Clone, Debug)]
4747
pub struct InvocationData<'a> {
4848
def_index: DefIndex,
4949
/// Module in which the macro was invoked.
@@ -70,6 +70,7 @@ impl<'a> InvocationData<'a> {
7070

7171
/// Binding produced by a `macro_rules` item.
7272
/// Not modularized, can shadow previous legacy bindings, etc.
73+
#[derive(Debug)]
7374
pub struct LegacyBinding<'a> {
7475
binding: &'a NameBinding<'a>,
7576
/// Legacy scope into which the `macro_rules` item was planted.
@@ -82,7 +83,7 @@ pub struct LegacyBinding<'a> {
8283
/// (named or unnamed), or even further if it escapes with `#[macro_use]`.
8384
/// Some macro invocations need to introduce legacy scopes too because they
8485
/// potentially can expand into macro definitions.
85-
#[derive(Copy, Clone)]
86+
#[derive(Copy, Clone, Debug)]
8687
pub enum LegacyScope<'a> {
8788
/// Created when invocation data is allocated in the arena,
8889
/// must be replaced with a proper scope later.
@@ -96,8 +97,8 @@ pub enum LegacyScope<'a> {
9697
Invocation(&'a InvocationData<'a>),
9798
}
9899

99-
/// Everything you need to resolve a macro path.
100-
#[derive(Clone)]
100+
/// Everything you need to resolve a macro or import path.
101+
#[derive(Clone, Debug)]
101102
pub struct ParentScope<'a> {
102103
crate module: Module<'a>,
103104
crate expansion: Mark,

src/librustc_resolve/resolve_imports.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use {NameBinding, NameBindingKind, ToNameBinding, PathResult, PrivacyError};
1616
use {Resolver, Segment};
1717
use {names_to_string, module_to_string};
1818
use {resolve_error, ResolutionError};
19+
use macros::ParentScope;
1920

2021
use rustc_data_structures::ptr_key::PtrKey;
2122
use rustc::ty;
@@ -88,13 +89,12 @@ pub struct ImportDirective<'a> {
8889
/// Span of the *root* use tree (see `root_id`).
8990
pub root_span: Span,
9091

91-
pub parent: Module<'a>,
92+
pub parent_scope: ParentScope<'a>,
9293
pub module_path: Vec<Segment>,
9394
/// The resolution of `module_path`.
9495
pub imported_module: Cell<Option<ModuleOrUniformRoot<'a>>>,
9596
pub subclass: ImportDirectiveSubclass<'a>,
9697
pub vis: Cell<ty::Visibility>,
97-
pub expansion: Mark,
9898
pub used: Cell<bool>,
9999

100100
/// Whether this import is a "canary" for the `uniform_paths` feature,
@@ -307,8 +307,9 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
307307
};
308308
match self.resolve_ident_in_module(module, ident, ns, false, path_span) {
309309
Err(Determined) => continue,
310-
Ok(binding)
311-
if !self.is_accessible_from(binding.vis, single_import.parent) => continue,
310+
Ok(binding) if !self.is_accessible_from(
311+
binding.vis, single_import.parent_scope.module
312+
) => continue,
312313
Ok(_) | Err(Undetermined) => return Err(Undetermined),
313314
}
314315
}
@@ -381,8 +382,9 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
381382

382383
match result {
383384
Err(Determined) => continue,
384-
Ok(binding)
385-
if !self.is_accessible_from(binding.vis, glob_import.parent) => continue,
385+
Ok(binding) if !self.is_accessible_from(
386+
binding.vis, glob_import.parent_scope.module
387+
) => continue,
386388
Ok(_) | Err(Undetermined) => return Err(Undetermined),
387389
}
388390
}
@@ -400,11 +402,11 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
400402
root_span: Span,
401403
root_id: NodeId,
402404
vis: ty::Visibility,
403-
expansion: Mark,
405+
parent_scope: ParentScope<'a>,
404406
is_uniform_paths_canary: bool) {
405-
let current_module = self.current_module;
407+
let current_module = parent_scope.module;
406408
let directive = self.arenas.alloc_import_directive(ImportDirective {
407-
parent: current_module,
409+
parent_scope,
408410
module_path,
409411
imported_module: Cell::new(None),
410412
subclass,
@@ -413,7 +415,6 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
413415
root_span,
414416
root_id,
415417
vis: Cell::new(vis),
416-
expansion,
417418
used: Cell::new(false),
418419
is_uniform_paths_canary,
419420
});
@@ -431,7 +432,7 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
431432
// We don't add prelude imports to the globs since they only affect lexical scopes,
432433
// which are not relevant to import resolution.
433434
GlobImport { is_prelude: true, .. } => {}
434-
GlobImport { .. } => self.current_module.globs.borrow_mut().push(directive),
435+
GlobImport { .. } => current_module.globs.borrow_mut().push(directive),
435436
_ => unreachable!(),
436437
}
437438
}
@@ -462,7 +463,7 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
462463
},
463464
span: directive.span,
464465
vis,
465-
expansion: directive.expansion,
466+
expansion: directive.parent_scope.expansion,
466467
})
467468
}
468469

@@ -568,12 +569,12 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
568569
let scope = match ident.span.reverse_glob_adjust(module.expansion,
569570
directive.span.ctxt().modern()) {
570571
Some(Some(def)) => self.macro_def_scope(def),
571-
Some(None) => directive.parent,
572+
Some(None) => directive.parent_scope.module,
572573
None => continue,
573574
};
574575
if self.is_accessible_from(binding.vis, scope) {
575576
let imported_binding = self.import(binding, directive);
576-
let _ = self.try_define(directive.parent, ident, ns, imported_binding);
577+
let _ = self.try_define(directive.parent_scope.module, ident, ns, imported_binding);
577578
}
578579
}
579580

@@ -587,7 +588,7 @@ impl<'a, 'crateloader> Resolver<'a, 'crateloader> {
587588
let dummy_binding = self.dummy_binding;
588589
let dummy_binding = self.import(dummy_binding, directive);
589590
self.per_ns(|this, ns| {
590-
let _ = this.try_define(directive.parent, target, ns, dummy_binding);
591+
let _ = this.try_define(directive.parent_scope.module, target, ns, dummy_binding);
591592
});
592593
}
593594
}
@@ -856,8 +857,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
856857
Segment::names_to_string(&directive.module_path[..]),
857858
module_to_string(self.current_module).unwrap_or_else(|| "???".to_string()));
858859

859-
860-
self.current_module = directive.parent;
860+
self.current_module = directive.parent_scope.module;
861861

862862
let module = if let Some(module) = directive.imported_module.get() {
863863
module
@@ -868,7 +868,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
868868
directive.vis.set(ty::Visibility::Invisible);
869869
let result = self.resolve_path(
870870
Some(if directive.is_uniform_paths_canary {
871-
ModuleOrUniformRoot::Module(directive.parent)
871+
ModuleOrUniformRoot::Module(directive.parent_scope.module)
872872
} else {
873873
ModuleOrUniformRoot::UniformRoot(keywords::Invalid.name())
874874
}),
@@ -910,7 +910,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
910910
return
911911
};
912912

913-
let parent = directive.parent;
913+
let parent = directive.parent_scope.module;
914914
match result[ns].get() {
915915
Err(Undetermined) => indeterminate = true,
916916
Err(Determined) => {
@@ -942,12 +942,12 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
942942

943943
// If appropriate, returns an error to report.
944944
fn finalize_import(&mut self, directive: &'b ImportDirective<'b>) -> Option<(Span, String)> {
945-
self.current_module = directive.parent;
945+
self.current_module = directive.parent_scope.module;
946946
let ImportDirective { ref module_path, span, .. } = *directive;
947947

948948
let module_result = self.resolve_path(
949949
Some(if directive.is_uniform_paths_canary {
950-
ModuleOrUniformRoot::Module(directive.parent)
950+
ModuleOrUniformRoot::Module(directive.parent_scope.module)
951951
} else {
952952
ModuleOrUniformRoot::UniformRoot(keywords::Invalid.name())
953953
}),
@@ -995,7 +995,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
995995
}
996996

997997
if let ModuleOrUniformRoot::Module(module) = module {
998-
if module.def_id() == directive.parent.def_id() {
998+
if module.def_id() == directive.parent_scope.module.def_id() {
999999
// Importing a module into itself is not allowed.
10001000
return Some((directive.span,
10011001
"Cannot glob-import a module into itself.".to_string()));
@@ -1189,7 +1189,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
11891189
if let Some(Def::Trait(_)) = module.def() {
11901190
self.session.span_err(directive.span, "items in traits are not importable.");
11911191
return;
1192-
} else if module.def_id() == directive.parent.def_id() {
1192+
} else if module.def_id() == directive.parent_scope.module.def_id() {
11931193
return;
11941194
} else if let GlobImport { is_prelude: true, .. } = directive.subclass {
11951195
self.prelude = Some(module);
@@ -1213,7 +1213,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
12131213
};
12141214
if self.is_accessible_from(binding.pseudo_vis(), scope) {
12151215
let imported_binding = self.import(binding, directive);
1216-
let _ = self.try_define(directive.parent, ident, ns, imported_binding);
1216+
let _ = self.try_define(directive.parent_scope.module, ident, ns, imported_binding);
12171217
}
12181218
}
12191219

0 commit comments

Comments
 (0)