about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2020-05-03 18:20:53 -0700
committerEsteban Küber <esteban@kuber.com.ar>2020-05-03 18:20:53 -0700
commitb368229d9bbaa840e777d33e36e649967e7ecb04 (patch)
tree2cdc6f590c232bd5d9f8cadaef59950a3cecbb63
parent3c872e2dc70cf20b5ac7c5ced4191824cd64bd2c (diff)
downloadrust-b368229d9bbaa840e777d33e36e649967e7ecb04.tar.gz
rust-b368229d9bbaa840e777d33e36e649967e7ecb04.zip
review comment: use early return
-rw-r--r--src/librustc_middle/ty/error.rs29
1 files changed, 19 insertions, 10 deletions
diff --git a/src/librustc_middle/ty/error.rs b/src/librustc_middle/ty/error.rs
index 22f576db08d..4e1a8b0e92f 100644
--- a/src/librustc_middle/ty/error.rs
+++ b/src/librustc_middle/ty/error.rs
@@ -609,7 +609,6 @@ impl<T> Trait<T> for X {
             "consider constraining the associated type `{}` to `{}`",
             values.expected, values.found
         );
-        let mut suggested = false;
         let body_owner = self.hir().get_if_local(body_owner_def_id);
         let current_method_ident = body_owner.and_then(|n| n.ident()).map(|i| i.name);
 
@@ -634,7 +633,10 @@ impl<T> Trait<T> for X {
             // type error is a comparison of an `impl` with its `trait` or when the
             // scope is outside of a `Body`.
         } else {
-            suggested |= self.point_at_methods_that_satisfy_associated_type(
+            // If we find a suitable associated function that returns the expected type, we don't
+            // want the more general suggestion later in this method about "consider constraining
+            // the associated type or calling a method that returns the associated type".
+            let point_at_assoc_fn = self.point_at_methods_that_satisfy_associated_type(
                 db,
                 assoc.container.id(),
                 current_method_ident,
@@ -643,25 +645,32 @@ impl<T> Trait<T> for X {
             );
             // Possibly suggest constraining the associated type to conform to the
             // found type.
-            suggested |=
-                self.suggest_constraint(db, &msg, body_owner_def_id, proj_ty, values.found);
-        }
-        if !suggested {
-            suggested = self.point_at_associated_type(db, body_owner_def_id, values.found);
+            if self.suggest_constraint(db, &msg, body_owner_def_id, proj_ty, values.found)
+                || point_at_assoc_fn
+            {
+                return;
+            }
         }
+
         if let ty::Opaque(def_id, _) = proj_ty.self_ty().kind {
             // When the expected `impl Trait` is not defined in the current item, it will come from
             // a return type. This can occur when dealing with `TryStream` (#71035).
-            suggested |= self.constrain_associated_type_structured_suggestion(
+            if self.constrain_associated_type_structured_suggestion(
                 db,
                 self.def_span(def_id),
                 &assoc,
                 values.found,
                 &msg,
-            );
+            ) {
+                return;
+            }
+        }
+
+        if self.point_at_associated_type(db, body_owner_def_id, values.found) {
+            return;
         }
 
-        if !suggested && !impl_comparison {
+        if !impl_comparison {
             // Generic suggestion when we can't be more specific.
             if callable_scope {
                 db.help(&format!("{} or calling a method that returns `{}`", msg, values.expected));