Skip to content

Commit 39e6ae2

Browse files
committed
Clean up get_traits_containing_item.
1 parent 8d9ba29 commit 39e6ae2

File tree

1 file changed

+42
-50
lines changed

1 file changed

+42
-50
lines changed

src/librustc_resolve/lib.rs

+42-50
Original file line numberDiff line numberDiff line change
@@ -2863,72 +2863,64 @@ impl<'a> Resolver<'a> {
28632863
fn get_traits_containing_item(&mut self, name: Name) -> Vec<TraitCandidate> {
28642864
debug!("(getting traits containing item) looking for '{}'", name);
28652865

2866-
fn add_trait_info(found_traits: &mut Vec<TraitCandidate>,
2867-
trait_def_id: DefId,
2868-
import_id: Option<NodeId>,
2869-
name: Name) {
2870-
debug!("(adding trait info) found trait {:?} for method '{}'",
2871-
trait_def_id,
2872-
name);
2873-
found_traits.push(TraitCandidate {
2874-
def_id: trait_def_id,
2875-
import_id: import_id,
2876-
});
2877-
}
2878-
28792866
let mut found_traits = Vec::new();
28802867
// Look for the current trait.
28812868
if let Some((trait_def_id, _)) = self.current_trait_ref {
28822869
if self.trait_item_map.contains_key(&(name, trait_def_id)) {
2883-
add_trait_info(&mut found_traits, trait_def_id, None, name);
2870+
found_traits.push(TraitCandidate { def_id: trait_def_id, import_id: None });
28842871
}
28852872
}
28862873

28872874
let mut search_module = self.current_module;
28882875
loop {
2889-
// Look for trait children.
2890-
let mut search_in_module = |this: &mut Self, module: Module<'a>| {
2891-
let mut traits = module.traits.borrow_mut();
2892-
if traits.is_none() {
2893-
let mut collected_traits = Vec::new();
2894-
module.for_each_child(|name, ns, binding| {
2895-
if ns != TypeNS { return }
2896-
if let Def::Trait(_) = binding.def() {
2897-
collected_traits.push((name, binding));
2898-
}
2899-
});
2900-
*traits = Some(collected_traits.into_boxed_slice());
2901-
}
2902-
2903-
for &(trait_name, binding) in traits.as_ref().unwrap().iter() {
2904-
let trait_def_id = binding.def().def_id();
2905-
if this.trait_item_map.contains_key(&(name, trait_def_id)) {
2906-
let mut import_id = None;
2907-
if let NameBindingKind::Import { directive, .. } = binding.kind {
2908-
let id = directive.id;
2909-
this.maybe_unused_trait_imports.insert(id);
2910-
this.add_to_glob_map(id, trait_name);
2911-
import_id = Some(id);
2912-
}
2913-
add_trait_info(&mut found_traits, trait_def_id, import_id, name);
2914-
}
2915-
}
2916-
};
2917-
search_in_module(self, search_module);
2876+
self.get_traits_in_module_containing_item(name, search_module, &mut found_traits);
2877+
match search_module.kind {
2878+
ModuleKind::Block(..) => search_module = search_module.parent.unwrap(),
2879+
_ => break,
2880+
}
2881+
}
29182882

2919-
if let ModuleKind::Block(..) = search_module.kind {
2920-
search_module = search_module.parent.unwrap();
2921-
} else {
2922-
if !search_module.no_implicit_prelude {
2923-
self.prelude.map(|prelude| search_in_module(self, prelude));
2924-
}
2925-
break;
2883+
if let Some(prelude) = self.prelude {
2884+
if !search_module.no_implicit_prelude {
2885+
self.get_traits_in_module_containing_item(name, prelude, &mut found_traits);
29262886
}
29272887
}
29282888

29292889
found_traits
29302890
}
29312891

2892+
fn get_traits_in_module_containing_item(&mut self,
2893+
name: Name,
2894+
module: Module,
2895+
found_traits: &mut Vec<TraitCandidate>) {
2896+
let mut traits = module.traits.borrow_mut();
2897+
if traits.is_none() {
2898+
let mut collected_traits = Vec::new();
2899+
module.for_each_child(|name, ns, binding| {
2900+
if ns != TypeNS { return }
2901+
if let Def::Trait(_) = binding.def() {
2902+
collected_traits.push((name, binding));
2903+
}
2904+
});
2905+
*traits = Some(collected_traits.into_boxed_slice());
2906+
}
2907+
2908+
for &(trait_name, binding) in traits.as_ref().unwrap().iter() {
2909+
let trait_def_id = binding.def().def_id();
2910+
if self.trait_item_map.contains_key(&(name, trait_def_id)) {
2911+
let import_id = match binding.kind {
2912+
NameBindingKind::Import { directive, .. } => {
2913+
self.maybe_unused_trait_imports.insert(directive.id);
2914+
self.add_to_glob_map(directive.id, trait_name);
2915+
Some(directive.id)
2916+
}
2917+
_ => None,
2918+
};
2919+
found_traits.push(TraitCandidate { def_id: trait_def_id, import_id: import_id });
2920+
}
2921+
}
2922+
}
2923+
29322924
/// When name resolution fails, this method can be used to look up candidate
29332925
/// entities with the expected name. It allows filtering them using the
29342926
/// supplied predicate (which should be used to only accept the types of

0 commit comments

Comments
 (0)