Skip to content

Commit febef47

Browse files
committed
Refactor how the prelude is handled
1 parent 21064d0 commit febef47

File tree

3 files changed

+21
-30
lines changed

3 files changed

+21
-30
lines changed

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,14 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
634634
module_.increment_outstanding_references_for(target, ValueNS, is_public);
635635
module_.increment_outstanding_references_for(target, TypeNS, is_public);
636636
}
637-
GlobImport => {
637+
GlobImport if !is_prelude => {
638638
// Set the glob flag. This tells us that we don't know the
639639
// module's exports ahead of time.
640640
module_.inc_glob_count(is_public)
641641
}
642+
// Prelude imports are not included in the glob counts since they do not get added to
643+
// `resolved_globs` -- they are handled separately in `resolve_imports`.
644+
GlobImport => {}
642645
}
643646

644647
let directive =

src/librustc_resolve/lib.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ pub struct ModuleS<'a> {
820820
// entry block for `f`.
821821
module_children: RefCell<NodeMap<Module<'a>>>,
822822

823-
shadowed_traits: RefCell<Vec<&'a NameBinding<'a>>>,
823+
prelude: RefCell<Option<Module<'a>>>,
824824

825825
glob_importers: RefCell<Vec<(Module<'a>, &'a ImportDirective)>>,
826826
resolved_globs: RefCell<(Vec<Module<'a>> /* public */, Vec<Module<'a>> /* private */)>,
@@ -855,7 +855,7 @@ impl<'a> ModuleS<'a> {
855855
resolutions: RefCell::new(HashMap::new()),
856856
unresolved_imports: RefCell::new(Vec::new()),
857857
module_children: RefCell::new(NodeMap()),
858-
shadowed_traits: RefCell::new(Vec::new()),
858+
prelude: RefCell::new(None),
859859
glob_importers: RefCell::new(Vec::new()),
860860
resolved_globs: RefCell::new((Vec::new(), Vec::new())),
861861
public_glob_count: Cell::new(0),
@@ -3336,33 +3336,25 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
33363336
}
33373337

33383338
// Look for trait children.
3339-
build_reduced_graph::populate_module_if_necessary(self, &search_module);
3340-
3341-
search_module.for_each_child(|_, ns, name_binding| {
3339+
let mut search_in_module = |module: Module<'a>| module.for_each_child(|_, ns, binding| {
33423340
if ns != TypeNS { return }
3343-
let trait_def_id = match name_binding.def() {
3341+
let trait_def_id = match binding.def() {
33443342
Some(Def::Trait(trait_def_id)) => trait_def_id,
33453343
Some(..) | None => return,
33463344
};
33473345
if self.trait_item_map.contains_key(&(name, trait_def_id)) {
33483346
add_trait_info(&mut found_traits, trait_def_id, name);
33493347
let trait_name = self.get_trait_name(trait_def_id);
3350-
self.record_use(trait_name, TypeNS, name_binding);
3351-
}
3352-
});
3353-
3354-
// Look for shadowed traits.
3355-
for binding in search_module.shadowed_traits.borrow().iter() {
3356-
let did = binding.def().unwrap().def_id();
3357-
if self.trait_item_map.contains_key(&(name, did)) {
3358-
add_trait_info(&mut found_traits, did, name);
3359-
let trait_name = self.get_trait_name(did);
33603348
self.record_use(trait_name, TypeNS, binding);
33613349
}
3362-
}
3350+
});
3351+
search_in_module(search_module);
33633352

33643353
match search_module.parent_link {
3365-
NoParentLink | ModuleParentLink(..) => break,
3354+
NoParentLink | ModuleParentLink(..) => {
3355+
search_module.prelude.borrow().map(search_in_module);
3356+
break;
3357+
}
33663358
BlockParentLink(parent_module, _) => {
33673359
search_module = parent_module;
33683360
}

src/librustc_resolve/resolve_imports.rs

Lines changed: 7 additions & 11 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 {
@@ -252,7 +249,8 @@ impl<'a> ::ModuleS<'a> {
252249
}
253250
}
254251

255-
resolution.result(true)
252+
self.prelude.borrow().map(|prelude| prelude.resolve_name(name, ns, false))
253+
.unwrap_or(Failed(None))
256254
}
257255

258256
// Define the name or return the existing binding if there is a collision.
@@ -616,6 +614,11 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
616614
}
617615
build_reduced_graph::populate_module_if_necessary(self.resolver, target_module);
618616

617+
if directive.is_prelude {
618+
*module_.prelude.borrow_mut() = Some(target_module);
619+
return Success(());
620+
}
621+
619622
// Add to target_module's glob_importers and module_'s resolved_globs
620623
target_module.glob_importers.borrow_mut().push((module_, directive));
621624
match *module_.resolved_globs.borrow_mut() {
@@ -678,13 +681,6 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
678681
self.resolver.session.add_lint(lint, id, binding.span.unwrap(), msg);
679682
}
680683
}
681-
682-
// We can always use methods from the prelude traits
683-
for glob_binding in resolution.duplicate_globs.iter() {
684-
if glob_binding.defined_with(DefModifiers::PRELUDE) {
685-
module.shadowed_traits.borrow_mut().push(glob_binding);
686-
}
687-
}
688684
}
689685

690686
if reexports.len() > 0 {

0 commit comments

Comments
 (0)