Skip to content

Commit d49a9db

Browse files
committed
auto merge of #6646 : dotdash/rust/method_lookup, r=brson
2 parents 7abcc14 + 19dc728 commit d49a9db

File tree

2 files changed

+100
-91
lines changed

2 files changed

+100
-91
lines changed

src/libcore/hashmap.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,9 @@ impl<K:Hash + Eq,V> Map<K, V> for HashMap<K, V> {
303303

304304
/// Visit all key-value pairs
305305
fn each<'a>(&'a self, blk: &fn(&K, &'a V) -> bool) -> bool {
306-
for uint::range(0, self.buckets.len()) |i| {
307-
for self.buckets[i].each |bucket| {
308-
if !blk(&bucket.key, &bucket.value) {
306+
for self.buckets.each |bucket| {
307+
for bucket.each |pair| {
308+
if !blk(&pair.key, &pair.value) {
309309
return false;
310310
}
311311
}

src/librustc/middle/resolve.rs

Lines changed: 97 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,7 @@ pub fn Resolver(session: Session,
734734

735735
graph_root: graph_root,
736736

737-
trait_info: HashMap::new(),
737+
method_map: @mut HashMap::new(),
738738
structs: HashSet::new(),
739739

740740
unresolved_imports: 0,
@@ -776,7 +776,7 @@ pub struct Resolver {
776776

777777
graph_root: @mut NameBindings,
778778

779-
trait_info: HashMap<def_id, HashSet<ident>>,
779+
method_map: @mut HashMap<ident, HashSet<def_id>>,
780780
structs: HashSet<def_id>,
781781

782782
// The number of imports that are currently unresolved.
@@ -1292,7 +1292,15 @@ pub impl Resolver {
12921292
}
12931293

12941294
let def_id = local_def(item.id);
1295-
self.trait_info.insert(def_id, method_names);
1295+
for method_names.each |name| {
1296+
if !self.method_map.contains_key(name) {
1297+
self.method_map.insert(*name, HashSet::new());
1298+
}
1299+
match self.method_map.find_mut(name) {
1300+
Some(s) => { s.insert(def_id); },
1301+
_ => fail!("Can't happen"),
1302+
}
1303+
}
12961304

12971305
name_bindings.define_type(privacy, def_trait(def_id), sp);
12981306
visit_item(item, new_parent, visitor);
@@ -1589,7 +1597,15 @@ pub impl Resolver {
15891597
interned_method_names.insert(method_name);
15901598
}
15911599
}
1592-
self.trait_info.insert(def_id, interned_method_names);
1600+
for interned_method_names.each |name| {
1601+
if !self.method_map.contains_key(name) {
1602+
self.method_map.insert(*name, HashSet::new());
1603+
}
1604+
match self.method_map.find_mut(name) {
1605+
Some(s) => { s.insert(def_id); },
1606+
_ => fail!("Can't happen"),
1607+
}
1608+
}
15931609

15941610
child_name_bindings.define_type(Public, def, dummy_sp());
15951611
}
@@ -4935,118 +4951,111 @@ pub impl Resolver {
49354951
debug!("(searching for traits containing method) looking for '%s'",
49364952
*self.session.str_of(name));
49374953

4954+
49384955
let mut found_traits = ~[];
49394956
let mut search_module = self.current_module;
4940-
loop {
4941-
// Look for the current trait.
4942-
match /*bad*/copy self.current_trait_refs {
4943-
Some(trait_def_ids) => {
4944-
for trait_def_ids.each |trait_def_id| {
4945-
self.add_trait_info_if_containing_method(
4946-
&mut found_traits, *trait_def_id, name);
4947-
}
4948-
}
4949-
None => {
4950-
// Nothing to do.
4951-
}
4952-
}
4953-
4954-
// Look for trait children.
4955-
for search_module.children.each_value |&child_name_bindings| {
4956-
match child_name_bindings.def_for_namespace(TypeNS) {
4957-
Some(def) => {
4958-
match def {
4959-
def_trait(trait_def_id) => {
4960-
self.add_trait_info_if_containing_method(
4961-
&mut found_traits, trait_def_id, name);
4962-
}
4963-
_ => {
4964-
// Continue.
4957+
match self.method_map.find(&name) {
4958+
Some(candidate_traits) => loop {
4959+
// Look for the current trait.
4960+
match /*bad*/copy self.current_trait_refs {
4961+
Some(trait_def_ids) => {
4962+
for trait_def_ids.each |trait_def_id| {
4963+
if candidate_traits.contains(trait_def_id) {
4964+
self.add_trait_info(
4965+
&mut found_traits,
4966+
*trait_def_id, name);
49654967
}
49664968
}
49674969
}
49684970
None => {
4969-
// Continue.
4971+
// Nothing to do.
49704972
}
49714973
}
4972-
}
49734974

4974-
// Look for imports.
4975-
for search_module.import_resolutions.each_value
4976-
|&import_resolution| {
4977-
4978-
match import_resolution.target_for_namespace(TypeNS) {
4979-
None => {
4980-
// Continue.
4981-
}
4982-
Some(target) => {
4983-
match target.bindings.def_for_namespace(TypeNS) {
4984-
Some(def) => {
4985-
match def {
4986-
def_trait(trait_def_id) => {
4987-
let added = self.
4988-
add_trait_info_if_containing_method(
4975+
// Look for trait children.
4976+
for search_module.children.each_value |&child_name_bindings| {
4977+
match child_name_bindings.def_for_namespace(TypeNS) {
4978+
Some(def) => {
4979+
match def {
4980+
def_trait(trait_def_id) => {
4981+
if candidate_traits.contains(&trait_def_id) {
4982+
self.add_trait_info(
49894983
&mut found_traits,
49904984
trait_def_id, name);
4991-
if added {
4992-
self.used_imports.insert(
4993-
import_resolution.id);
4994-
}
4995-
}
4996-
_ => {
4997-
// Continue.
49984985
}
49994986
}
5000-
}
5001-
None => {
5002-
// Continue.
4987+
_ => {
4988+
// Continue.
4989+
}
50034990
}
50044991
}
4992+
None => {
4993+
// Continue.
4994+
}
50054995
}
50064996
}
5007-
}
50084997

5009-
// Move to the next parent.
5010-
match search_module.parent_link {
5011-
NoParentLink => {
5012-
// Done.
5013-
break;
4998+
// Look for imports.
4999+
for search_module.import_resolutions.each_value
5000+
|&import_resolution| {
5001+
5002+
match import_resolution.target_for_namespace(TypeNS) {
5003+
None => {
5004+
// Continue.
5005+
}
5006+
Some(target) => {
5007+
match target.bindings.def_for_namespace(TypeNS) {
5008+
Some(def) => {
5009+
match def {
5010+
def_trait(trait_def_id) => {
5011+
if candidate_traits.contains(&trait_def_id) {
5012+
self.add_trait_info(
5013+
&mut found_traits,
5014+
trait_def_id, name);
5015+
self.used_imports.insert(
5016+
import_resolution.id);
5017+
}
5018+
}
5019+
_ => {
5020+
// Continue.
5021+
}
5022+
}
5023+
}
5024+
None => {
5025+
// Continue.
5026+
}
5027+
}
5028+
}
5029+
}
50145030
}
5015-
ModuleParentLink(parent_module, _) |
5016-
BlockParentLink(parent_module, _) => {
5017-
search_module = parent_module;
5031+
5032+
// Move to the next parent.
5033+
match search_module.parent_link {
5034+
NoParentLink => {
5035+
// Done.
5036+
break;
5037+
}
5038+
ModuleParentLink(parent_module, _) |
5039+
BlockParentLink(parent_module, _) => {
5040+
search_module = parent_module;
5041+
}
50185042
}
5019-
}
5043+
},
5044+
_ => ()
50205045
}
50215046

50225047
return found_traits;
50235048
}
50245049

5025-
fn add_trait_info_if_containing_method(&self,
5026-
found_traits: &mut ~[def_id],
5027-
trait_def_id: def_id,
5028-
name: ident)
5029-
-> bool {
5030-
debug!("(adding trait info if containing method) trying trait %d:%d \
5031-
for method '%s'",
5050+
fn add_trait_info(&self,
5051+
found_traits: &mut ~[def_id],
5052+
trait_def_id: def_id,
5053+
name: ident) {
5054+
debug!("(adding trait info) found trait %d:%d for method '%s'",
50325055
trait_def_id.crate,
50335056
trait_def_id.node,
50345057
*self.session.str_of(name));
5035-
5036-
match self.trait_info.find(&trait_def_id) {
5037-
Some(trait_info) if trait_info.contains(&name) => {
5038-
debug!("(adding trait info if containing method) found trait \
5039-
%d:%d for method '%s'",
5040-
trait_def_id.crate,
5041-
trait_def_id.node,
5042-
*self.session.str_of(name));
5043-
found_traits.push(trait_def_id);
5044-
true
5045-
}
5046-
Some(_) | None => {
5047-
false
5048-
}
5049-
}
5058+
found_traits.push(trait_def_id);
50505059
}
50515060

50525061
fn add_fixed_trait_for_expr(@mut self,

0 commit comments

Comments
 (0)