]> git.lizzy.rs Git - rust.git/commitdiff
Do not suggest implementing traits if present in predicates
authorEsteban Küber <esteban@kuber.com.ar>
Tue, 25 Feb 2020 20:10:48 +0000 (12:10 -0800)
committerEsteban Küber <esteban@kuber.com.ar>
Fri, 28 Feb 2020 19:37:59 +0000 (11:37 -0800)
src/librustc_typeck/check/method/suggest.rs
src/test/ui/methods/method-call-err-msg.stderr
src/test/ui/union/union-derive-clone.stderr
src/test/ui/unique-object-noncopyable.stderr

index 5528895ad038941b7c0407cb3c5a40abe42cd76c..44f87319ebf4fae5ff4d6d2877d33fde97e93204 100644 (file)
@@ -696,6 +696,7 @@ trait bound{s}",
                         item_name,
                         source,
                         out_of_scope_traits,
+                        &unsatisfied_predicates,
                     );
                 }
 
@@ -895,6 +896,7 @@ fn suggest_traits_to_import<'b>(
         item_name: ast::Ident,
         source: SelfSource<'b>,
         valid_out_of_scope_traits: Vec<DefId>,
+        unsatisfied_predicates: &[(ty::Predicate<'tcx>, Option<ty::Predicate<'tcx>>)],
     ) {
         if self.suggest_valid_traits(err, valid_out_of_scope_traits) {
             return;
@@ -915,7 +917,13 @@ fn suggest_traits_to_import<'b>(
                 // this isn't perfect (that is, there are cases when
                 // implementing a trait would be legal but is rejected
                 // here).
-                (type_is_local || info.def_id.is_local())
+                !unsatisfied_predicates.iter().any(|(p, _)| match p {
+                    // Hide traits if they are present in predicates as they can be fixed without
+                    // having to implement them.
+                    ty::Predicate::Trait(t, _) => t.def_id() != info.def_id,
+                    ty::Predicate::Projection(p) => p.item_def_id() != info.def_id,
+                    _ => true,
+                }) && (type_is_local || info.def_id.is_local())
                     && self
                         .associated_item(info.def_id, item_name, Namespace::ValueNS)
                         .filter(|item| {
index ecb6950615748f36b0558f2bcb3fc322c9c646e0..4678642dd6d0cb9bfff87afa1879d6af72ae7b3d 100644 (file)
@@ -47,9 +47,8 @@ LL |      .take()
            `Foo: std::iter::Iterator`
            which is required by `&mut Foo: std::iter::Iterator`
    = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following traits define an item `take`, perhaps you need to implement one of them:
-           candidate #1: `std::io::Read`
-           candidate #2: `std::iter::Iterator`
+   = note: the following trait defines an item `take`, perhaps you need to implement it:
+           candidate #1: `std::iter::Iterator`
 
 error[E0061]: this function takes 3 arguments but 0 arguments were supplied
   --> $DIR/method-call-err-msg.rs:21:7
index fdf2393656e12d94c92cebfc322299b2a1f74f35..01c8e8471aac2b2242f7904646bb3838720c65ea 100644 (file)
@@ -25,9 +25,6 @@ LL |     let w = u.clone();
    = note: the method `clone` exists but the following trait bounds were not satisfied:
            `CloneNoCopy: std::marker::Copy`
            which is required by `U5<CloneNoCopy>: std::clone::Clone`
-   = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `clone`, perhaps you need to implement it:
-           candidate #1: `std::clone::Clone`
 
 error: aborting due to 2 previous errors
 
index 1c5fed2c33b4d56ce6f03bb4e5b33bd5944253c7..286008188fcebd7538ef20190ae371dbb30c1637 100644 (file)
@@ -20,9 +20,6 @@ LL | pub struct Box<T: ?Sized>(Unique<T>);
            which is required by `std::boxed::Box<dyn Foo>: std::clone::Clone`
            `dyn Foo: std::clone::Clone`
            which is required by `std::boxed::Box<dyn Foo>: std::clone::Clone`
-   = help: items from traits can only be used if the trait is implemented and in scope
-   = note: the following trait defines an item `clone`, perhaps you need to implement it:
-           candidate #1: `std::clone::Clone`
 
 error: aborting due to previous error