Skip to content

Commit 754c176

Browse files
committed
Refactor how the prelude is handled
1 parent 7fc0e40 commit 754c176

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,11 +664,12 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
664664
module_.increment_outstanding_references_for(target, ValueNS, is_public);
665665
module_.increment_outstanding_references_for(target, TypeNS, is_public);
666666
}
667-
GlobImport => {
667+
GlobImport if !is_prelude => {
668668
// Set the glob flag. This tells us that we don't know the
669669
// module's exports ahead of time.
670670
module_.inc_glob_count(is_public)
671671
}
672+
_ => {}
672673
}
673674

674675
let directive =

src/librustc_resolve/lib.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,7 @@ pub struct ModuleS<'a> {
830830
// entry block for `f`.
831831
module_children: RefCell<NodeMap<Module<'a>>>,
832832

833+
prelude: RefCell<Option<Module<'a>>>,
833834
shadowed_traits: RefCell<Vec<&'a NameBinding<'a>>>,
834835

835836
glob_importers: RefCell<Vec<(Module<'a>, &'a ImportDirective)>>,
@@ -865,6 +866,7 @@ impl<'a> ModuleS<'a> {
865866
resolutions: RefCell::new(HashMap::new()),
866867
unresolved_imports: RefCell::new(Vec::new()),
867868
module_children: RefCell::new(NodeMap()),
869+
prelude: RefCell::new(None),
868870
shadowed_traits: RefCell::new(Vec::new()),
869871
glob_importers: RefCell::new(Vec::new()),
870872
resolved_globs: RefCell::new((Vec::new(), Vec::new())),
@@ -3403,34 +3405,36 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
34033405
None => {} // Nothing to do.
34043406
}
34053407

3406-
// Look for trait children.
3407-
build_reduced_graph::populate_module_if_necessary(self, &search_module);
3408+
// Look for shadowed traits.
3409+
for binding in search_module.shadowed_traits.borrow().iter() {
3410+
let did = binding.def().unwrap().def_id();
3411+
if self.trait_item_map.contains_key(&(name, did)) {
3412+
add_trait_info(&mut found_traits, did, name);
3413+
let trait_name = self.get_trait_name(did);
3414+
self.record_use(trait_name, TypeNS, binding);
3415+
}
3416+
}
34083417

3409-
search_module.for_each_child(|_, ns, name_binding| {
3418+
// Look for trait children.
3419+
let mut search_in_module = |module: Module<'a>| module.for_each_child(|_, ns, binding| {
34103420
if ns != TypeNS { return }
3411-
let trait_def_id = match name_binding.def() {
3421+
let trait_def_id = match binding.def() {
34123422
Some(Def::Trait(trait_def_id)) => trait_def_id,
34133423
Some(..) | None => return,
34143424
};
34153425
if self.trait_item_map.contains_key(&(name, trait_def_id)) {
34163426
add_trait_info(&mut found_traits, trait_def_id, name);
34173427
let trait_name = self.get_trait_name(trait_def_id);
3418-
self.record_use(trait_name, TypeNS, name_binding);
3419-
}
3420-
});
3421-
3422-
// Look for shadowed traits.
3423-
for binding in search_module.shadowed_traits.borrow().iter() {
3424-
let did = binding.def().unwrap().def_id();
3425-
if self.trait_item_map.contains_key(&(name, did)) {
3426-
add_trait_info(&mut found_traits, did, name);
3427-
let trait_name = self.get_trait_name(did);
34283428
self.record_use(trait_name, TypeNS, binding);
34293429
}
3430-
}
3430+
});
3431+
search_in_module(search_module);
34313432

34323433
match search_module.parent_link {
3433-
NoParentLink | ModuleParentLink(..) => break,
3434+
NoParentLink | ModuleParentLink(..) => {
3435+
search_module.prelude.borrow().map(search_in_module);
3436+
break;
3437+
}
34343438
BlockParentLink(parent_module, _) => {
34353439
search_module = parent_module;
34363440
}

src/librustc_resolve/resolve_imports.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ impl ImportDirective {
9898
if let GlobImport = self.subclass {
9999
modifiers = modifiers | DefModifiers::GLOB_IMPORTED;
100100
}
101-
if self.is_prelude {
102-
modifiers = modifiers | DefModifiers::PRELUDE;
103-
}
104101

105102
NameBinding {
106103
kind: NameBindingKind::Import {
@@ -233,7 +230,8 @@ impl<'a> ::ModuleS<'a> {
233230
}
234231
}
235232

236-
resolution.result(true)
233+
self.prelude.borrow().map(|prelude| prelude.resolve_name(name, ns, false))
234+
.unwrap_or(Failed(None))
237235
}
238236

239237
// Define the name or return the existing binding if there is a collision.
@@ -607,6 +605,11 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
607605
}
608606
build_reduced_graph::populate_module_if_necessary(self.resolver, target_module);
609607

608+
if directive.is_prelude {
609+
*module_.prelude.borrow_mut() = Some(target_module);
610+
return Success(());
611+
}
612+
610613
// Add to target_module's glob_importers and module_'s resolved_globs
611614
target_module.glob_importers.borrow_mut().push((module_, directive));
612615
match *module_.resolved_globs.borrow_mut() {

0 commit comments

Comments
 (0)