Skip to content

Commit

Permalink
Do not suggest implementing traits if present in predicates
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Feb 28, 2020
1 parent 61bc7a3 commit a9a99df
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
10 changes: 9 additions & 1 deletion src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
item_name,
source,
out_of_scope_traits,
&unsatisfied_predicates,
);
}

Expand Down Expand Up @@ -895,6 +896,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
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;
Expand All @@ -915,7 +917,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// 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| {
Expand Down
5 changes: 2 additions & 3 deletions src/test/ui/methods/method-call-err-msg.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions src/test/ui/union/union-derive-clone.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 0 additions & 3 deletions src/test/ui/unique-object-noncopyable.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit a9a99df

Please sign in to comment.