Skip to content

Commit 66931a0

Browse files
petrochenkovpietroalbini
authored andcommitted
resolve: Record full parent scope data for imports
1 parent 7a1093b commit 66931a0

File tree

3 files changed

+52
-45
lines changed

3 files changed

+52
-45
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
};
@@ -348,7 +348,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
348348
root_use_tree.span,
349349
root_id,
350350
vis,
351-
expansion,
351+
parent_scope,
352352
false, // is_uniform_paths_canary
353353
);
354354
}
@@ -365,7 +365,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
365365
root_use_tree.span,
366366
root_id,
367367
vis,
368-
expansion,
368+
parent_scope,
369369
false, // is_uniform_paths_canary
370370
);
371371
}
@@ -403,16 +403,17 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
403403
uniform_paths_canary_emitted,
404404
true,
405405
item,
406-
expansion,
406+
parent_scope.clone(),
407407
);
408408
}
409409
}
410410
}
411411
}
412412

413413
/// Constructs the reduced graph for one item.
414-
fn build_reduced_graph_for_item(&mut self, item: &Item, expansion: Mark) {
415-
let parent = self.current_module;
414+
fn build_reduced_graph_for_item(&mut self, item: &Item, parent_scope: ParentScope<'a>) {
415+
let parent = parent_scope.module;
416+
let expansion = parent_scope.expansion;
416417
let ident = item.ident;
417418
let sp = item.span;
418419
let vis = self.resolve_visibility(&item.vis);
@@ -429,7 +430,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
429430
false, // uniform_paths_canary_emitted
430431
false,
431432
item,
432-
expansion,
433+
parent_scope,
433434
);
434435
}
435436

@@ -442,7 +443,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
442443
self.injected_crate = Some(module);
443444
}
444445

445-
let used = self.process_legacy_macro_imports(item, module, expansion);
446+
let used = self.process_legacy_macro_imports(item, module, &parent_scope);
446447
let binding =
447448
(module, ty::Visibility::Public, sp, expansion).to_name_binding(self.arenas);
448449
if ptr::eq(self.current_module, self.graph_root) {
@@ -467,7 +468,7 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
467468
let directive = self.arenas.alloc_import_directive(ImportDirective {
468469
root_id: item.id,
469470
id: item.id,
470-
parent,
471+
parent_scope,
471472
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
472473
subclass: ImportDirectiveSubclass::ExternCrate {
473474
source: orig_name,
@@ -477,7 +478,6 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
477478
span: item.span,
478479
module_path: Vec::new(),
479480
vis: Cell::new(vis),
480-
expansion,
481481
used: Cell::new(used),
482482
is_uniform_paths_canary: false,
483483
});
@@ -850,9 +850,9 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
850850
}
851851

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

@@ -862,18 +862,17 @@ impl<'a, 'cl> Resolver<'a, 'cl> {
862862
"an `extern crate` loading macros must be at the crate root");
863863
}
864864

865-
let (graph_root, arenas) = (self.graph_root, self.arenas);
865+
let arenas = self.arenas;
866866
let macro_use_directive = |span| arenas.alloc_import_directive(ImportDirective {
867867
root_id: item.id,
868868
id: item.id,
869-
parent: graph_root,
869+
parent_scope: parent_scope.clone(),
870870
imported_module: Cell::new(Some(ModuleOrUniformRoot::Module(module))),
871871
subclass: ImportDirectiveSubclass::MacroUse,
872872
root_span: span,
873873
span,
874874
module_path: Vec::new(),
875875
vis: Cell::new(ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX))),
876-
expansion,
877876
used: Cell::new(false),
878877
is_uniform_paths_canary: false,
879878
});
@@ -1004,7 +1003,13 @@ impl<'a, 'b, 'cl> Visitor<'a> for BuildReducedGraphVisitor<'a, 'b, 'cl> {
10041003

10051004
let orig_current_module = self.resolver.current_module;
10061005
let orig_current_legacy_scope = self.current_legacy_scope;
1007-
self.resolver.build_reduced_graph_for_item(item, self.expansion);
1006+
let parent_scope = ParentScope {
1007+
module: self.resolver.current_module,
1008+
expansion: self.expansion,
1009+
legacy: self.current_legacy_scope,
1010+
derives: Vec::new(),
1011+
};
1012+
self.resolver.build_reduced_graph_for_item(item, parent_scope);
10081013
visit::walk_item(self, item);
10091014
self.resolver.current_module = orig_current_module;
10101015
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 & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use {NameBinding, NameBindingKind, ToNameBinding, PathResult, PrivacyError};
1616
use Resolver;
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<Ident>,
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
}
@@ -854,7 +855,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
854855
names_to_string(&directive.module_path[..]),
855856
module_to_string(self.current_module).unwrap_or_else(|| "???".to_string()));
856857

857-
self.current_module = directive.parent;
858+
self.current_module = directive.parent_scope.module;
858859

859860
let module = if let Some(module) = directive.imported_module.get() {
860861
module
@@ -865,7 +866,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
865866
directive.vis.set(ty::Visibility::Invisible);
866867
let result = self.resolve_path(
867868
Some(if directive.is_uniform_paths_canary {
868-
ModuleOrUniformRoot::Module(directive.parent)
869+
ModuleOrUniformRoot::Module(directive.parent_scope.module)
869870
} else {
870871
ModuleOrUniformRoot::UniformRoot(keywords::Invalid.name())
871872
}),
@@ -907,7 +908,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
907908
return
908909
};
909910

910-
let parent = directive.parent;
911+
let parent = directive.parent_scope.module;
911912
match result[ns].get() {
912913
Err(Undetermined) => indeterminate = true,
913914
Err(Determined) => {
@@ -939,12 +940,12 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
939940

940941
// If appropriate, returns an error to report.
941942
fn finalize_import(&mut self, directive: &'b ImportDirective<'b>) -> Option<(Span, String)> {
942-
self.current_module = directive.parent;
943+
self.current_module = directive.parent_scope.module;
943944
let ImportDirective { ref module_path, span, .. } = *directive;
944945

945946
let module_result = self.resolve_path(
946947
Some(if directive.is_uniform_paths_canary {
947-
ModuleOrUniformRoot::Module(directive.parent)
948+
ModuleOrUniformRoot::Module(directive.parent_scope.module)
948949
} else {
949950
ModuleOrUniformRoot::UniformRoot(keywords::Invalid.name())
950951
}),
@@ -992,7 +993,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
992993
}
993994

994995
if let ModuleOrUniformRoot::Module(module) = module {
995-
if module.def_id() == directive.parent.def_id() {
996+
if module.def_id() == directive.parent_scope.module.def_id() {
996997
// Importing a module into itself is not allowed.
997998
return Some((directive.span,
998999
"Cannot glob-import a module into itself.".to_string()));
@@ -1186,7 +1187,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
11861187
if let Some(Def::Trait(_)) = module.def() {
11871188
self.session.span_err(directive.span, "items in traits are not importable.");
11881189
return;
1189-
} else if module.def_id() == directive.parent.def_id() {
1190+
} else if module.def_id() == directive.parent_scope.module.def_id() {
11901191
return;
11911192
} else if let GlobImport { is_prelude: true, .. } = directive.subclass {
11921193
self.prelude = Some(module);
@@ -1210,7 +1211,7 @@ impl<'a, 'b:'a, 'c: 'b> ImportResolver<'a, 'b, 'c> {
12101211
};
12111212
if self.is_accessible_from(binding.pseudo_vis(), scope) {
12121213
let imported_binding = self.import(binding, directive);
1213-
let _ = self.try_define(directive.parent, ident, ns, imported_binding);
1214+
let _ = self.try_define(directive.parent_scope.module, ident, ns, imported_binding);
12141215
}
12151216
}
12161217

0 commit comments

Comments
 (0)