Refactor how the prelude is handled
This commit is contained in:
parent
21064d097e
commit
febef471e3
|
@ -634,11 +634,14 @@ impl<'a, 'b:'a, 'tcx:'b> GraphBuilder<'a, 'b, 'tcx> {
|
|||
module_.increment_outstanding_references_for(target, ValueNS, is_public);
|
||||
module_.increment_outstanding_references_for(target, TypeNS, is_public);
|
||||
}
|
||||
GlobImport => {
|
||||
GlobImport if !is_prelude => {
|
||||
// Set the glob flag. This tells us that we don't know the
|
||||
// module's exports ahead of time.
|
||||
module_.inc_glob_count(is_public)
|
||||
}
|
||||
// Prelude imports are not included in the glob counts since they do not get added to
|
||||
// `resolved_globs` -- they are handled separately in `resolve_imports`.
|
||||
GlobImport => {}
|
||||
}
|
||||
|
||||
let directive =
|
||||
|
|
|
@ -820,7 +820,7 @@ pub struct ModuleS<'a> {
|
|||
// entry block for `f`.
|
||||
module_children: RefCell<NodeMap<Module<'a>>>,
|
||||
|
||||
shadowed_traits: RefCell<Vec<&'a NameBinding<'a>>>,
|
||||
prelude: RefCell<Option<Module<'a>>>,
|
||||
|
||||
glob_importers: RefCell<Vec<(Module<'a>, &'a ImportDirective)>>,
|
||||
resolved_globs: RefCell<(Vec<Module<'a>> /* public */, Vec<Module<'a>> /* private */)>,
|
||||
|
@ -855,7 +855,7 @@ impl<'a> ModuleS<'a> {
|
|||
resolutions: RefCell::new(HashMap::new()),
|
||||
unresolved_imports: RefCell::new(Vec::new()),
|
||||
module_children: RefCell::new(NodeMap()),
|
||||
shadowed_traits: RefCell::new(Vec::new()),
|
||||
prelude: RefCell::new(None),
|
||||
glob_importers: RefCell::new(Vec::new()),
|
||||
resolved_globs: RefCell::new((Vec::new(), Vec::new())),
|
||||
public_glob_count: Cell::new(0),
|
||||
|
@ -3336,33 +3336,25 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
|
|||
}
|
||||
|
||||
// Look for trait children.
|
||||
build_reduced_graph::populate_module_if_necessary(self, &search_module);
|
||||
|
||||
search_module.for_each_child(|_, ns, name_binding| {
|
||||
let mut search_in_module = |module: Module<'a>| module.for_each_child(|_, ns, binding| {
|
||||
if ns != TypeNS { return }
|
||||
let trait_def_id = match name_binding.def() {
|
||||
let trait_def_id = match binding.def() {
|
||||
Some(Def::Trait(trait_def_id)) => trait_def_id,
|
||||
Some(..) | None => return,
|
||||
};
|
||||
if self.trait_item_map.contains_key(&(name, trait_def_id)) {
|
||||
add_trait_info(&mut found_traits, trait_def_id, name);
|
||||
let trait_name = self.get_trait_name(trait_def_id);
|
||||
self.record_use(trait_name, TypeNS, name_binding);
|
||||
}
|
||||
});
|
||||
|
||||
// Look for shadowed traits.
|
||||
for binding in search_module.shadowed_traits.borrow().iter() {
|
||||
let did = binding.def().unwrap().def_id();
|
||||
if self.trait_item_map.contains_key(&(name, did)) {
|
||||
add_trait_info(&mut found_traits, did, name);
|
||||
let trait_name = self.get_trait_name(did);
|
||||
self.record_use(trait_name, TypeNS, binding);
|
||||
}
|
||||
}
|
||||
});
|
||||
search_in_module(search_module);
|
||||
|
||||
match search_module.parent_link {
|
||||
NoParentLink | ModuleParentLink(..) => break,
|
||||
NoParentLink | ModuleParentLink(..) => {
|
||||
search_module.prelude.borrow().map(search_in_module);
|
||||
break;
|
||||
}
|
||||
BlockParentLink(parent_module, _) => {
|
||||
search_module = parent_module;
|
||||
}
|
||||
|
|
|
@ -98,9 +98,6 @@ impl ImportDirective {
|
|||
if let GlobImport = self.subclass {
|
||||
modifiers = modifiers | DefModifiers::GLOB_IMPORTED;
|
||||
}
|
||||
if self.is_prelude {
|
||||
modifiers = modifiers | DefModifiers::PRELUDE;
|
||||
}
|
||||
|
||||
NameBinding {
|
||||
kind: NameBindingKind::Import {
|
||||
|
@ -252,7 +249,8 @@ impl<'a> ::ModuleS<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
resolution.result(true)
|
||||
self.prelude.borrow().map(|prelude| prelude.resolve_name(name, ns, false))
|
||||
.unwrap_or(Failed(None))
|
||||
}
|
||||
|
||||
// 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> {
|
|||
}
|
||||
build_reduced_graph::populate_module_if_necessary(self.resolver, target_module);
|
||||
|
||||
if directive.is_prelude {
|
||||
*module_.prelude.borrow_mut() = Some(target_module);
|
||||
return Success(());
|
||||
}
|
||||
|
||||
// Add to target_module's glob_importers and module_'s resolved_globs
|
||||
target_module.glob_importers.borrow_mut().push((module_, directive));
|
||||
match *module_.resolved_globs.borrow_mut() {
|
||||
|
@ -678,13 +681,6 @@ impl<'a, 'b:'a, 'tcx:'b> ImportResolver<'a, 'b, 'tcx> {
|
|||
self.resolver.session.add_lint(lint, id, binding.span.unwrap(), msg);
|
||||
}
|
||||
}
|
||||
|
||||
// We can always use methods from the prelude traits
|
||||
for glob_binding in resolution.duplicate_globs.iter() {
|
||||
if glob_binding.defined_with(DefModifiers::PRELUDE) {
|
||||
module.shadowed_traits.borrow_mut().push(glob_binding);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if reexports.len() > 0 {
|
||||
|
|
Loading…
Reference in New Issue