From 99711c1863fc712dc14ca61809055b283415acbe Mon Sep 17 00:00:00 2001
From: Florian Diebold <flodiebold@gmail.com>
Date: Mon, 25 Mar 2019 21:24:20 +0100
Subject: [PATCH] Clean up comments / use nicer Iterator methods

---
 crates/ra_hir/src/resolve.rs              |  2 +-
 crates/ra_hir/src/ty/method_resolution.rs | 23 ++++++++++++++---------
 2 files changed, 15 insertions(+), 10 deletions(-)

diff --git a/crates/ra_hir/src/resolve.rs b/crates/ra_hir/src/resolve.rs
index 0f5031e7624..2609585b1f1 100644
--- a/crates/ra_hir/src/resolve.rs
+++ b/crates/ra_hir/src/resolve.rs
@@ -187,7 +187,7 @@ impl Resolver {
                 }
                 .into_iter()
             })
-            .flat_map(|i| i)
+            .flatten()
     }
 
     fn module(&self) -> Option<(&CrateDefMap, CrateModuleId)> {
diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs
index 146e8a02e7d..3ac8dc46b0d 100644
--- a/crates/ra_hir/src/ty/method_resolution.rs
+++ b/crates/ra_hir/src/ty/method_resolution.rs
@@ -118,12 +118,8 @@ pub(crate) fn implements(db: &impl HirDatabase, trait_ref: TraitRef) -> bool {
         None => return false,
     };
     let crate_impl_blocks = db.impls_in_crate(krate);
-    for impl_block in crate_impl_blocks.lookup_impl_blocks_for_trait(&trait_ref.trait_) {
-        if &impl_block.target_ty(db) == trait_ref.self_ty() {
-            return true;
-        }
-    }
-    false
+    let mut impl_blocks = crate_impl_blocks.lookup_impl_blocks_for_trait(&trait_ref.trait_);
+    impl_blocks.any(|impl_block| &impl_block.target_ty(db) == trait_ref.self_ty())
 }
 
 fn def_crate(db: &impl HirDatabase, ty: &Ty) -> Option<Crate> {
@@ -145,7 +141,7 @@ impl Ty {
         name: &Name,
         resolver: &Resolver,
     ) -> Option<(Ty, Function)> {
-        // FIXME: what has priority, an inherent method that needs autoderefs or a trait method?
+        // FIXME: trait methods should be used before autoderefs
         let inherent_method = self.clone().iterate_methods(db, |ty, f| {
             let sig = f.signature(db);
             if sig.name() == name && sig.has_self_param() {
@@ -178,12 +174,21 @@ impl Ty {
                 }
             }
         }
-        // FIXME the implements check may result in other obligations or unifying variables?
+        // FIXME:
+        //  - we might not actually be able to determine fully that the type
+        //    implements the trait here; it's enough if we (well, Chalk) determine
+        //    that it's possible.
+        //  - when the trait method is picked, we need to register an
+        //    'obligation' somewhere so that we later check that it's really
+        //    implemented
+        //  - both points go for additional requirements from where clauses as
+        //    well (in fact, the 'implements' condition could just be considered a
+        //    'where Self: Trait' clause)
         candidates.retain(|(t, _m)| {
             let trait_ref = TraitRef { trait_: *t, substs: Substs::single(self.clone()) };
             db.implements(trait_ref)
         });
-        // FIXME what happens if there are still multiple potential candidates?
+        // FIXME if there's multiple candidates here, that's an ambiguity error
         let (_chosen_trait, chosen_method) = candidates.first()?;
         Some((self.clone(), *chosen_method))
     }