Skip to content

Commit

Permalink
Do not suggest private traits that have missing method
Browse files Browse the repository at this point in the history
When encountering a method call for an ADT that doesn't have any
implementation of it, we search for traits that could be implemented
that do have that method. Filter out private non-local traits that would
not be able to be implemented.

This doesn't account for public traits that are in a private scope, but
works as a first approximation and is a more correct behavior than the
current one.
  • Loading branch information
estebank committed Jan 22, 2018
1 parent fdc18b3 commit 4121ddb
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
9 changes: 7 additions & 2 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, '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())
&& self.associated_item(info.def_id, item_name, Namespace::Value).is_some()
(type_is_local || info.def_id.is_local()) &&
self.associated_item(info.def_id, item_name, Namespace::Value)
.filter(|item| {
// We only want to suggest public or local traits (#45781).
item.vis == ty::Visibility::Public || info.def_id.is_local()
})
.is_some()
})
.collect::<Vec<_>>();

Expand Down
3 changes: 2 additions & 1 deletion src/librustc_typeck/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,13 @@ This API is completely unstable and subject to change.
#![feature(advanced_slice_patterns)]
#![feature(box_patterns)]
#![feature(box_syntax)]
#![feature(crate_visibility_modifier)]
#![feature(conservative_impl_trait)]
#![feature(copy_closures, clone_closures)]
#![feature(crate_visibility_modifier)]
#![feature(from_ref)]
#![feature(match_default_bindings)]
#![feature(never_type)]
#![feature(option_filter)]
#![feature(quote)]
#![feature(refcell_replace_swap)]
#![feature(rustc_diagnostic_macros)]
Expand Down
12 changes: 4 additions & 8 deletions src/test/ui/impl-trait/no-method-suggested-traits.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ error[E0599]: no method named `method` found for type `Foo` in the current scope
= note: the following traits define an item `method`, perhaps you need to implement one of them:
candidate #1: `foo::Bar`
candidate #2: `no_method_suggested_traits::foo::PubPub`
candidate #3: `no_method_suggested_traits::bar::PubPriv`
candidate #4: `no_method_suggested_traits::qux::PrivPub`
candidate #5: `no_method_suggested_traits::quz::PrivPriv`
candidate #6: `no_method_suggested_traits::Reexported`
candidate #3: `no_method_suggested_traits::qux::PrivPub`
candidate #4: `no_method_suggested_traits::Reexported`

error[E0599]: no method named `method` found for type `std::rc::Rc<&mut std::boxed::Box<&Foo>>` in the current scope
--> $DIR/no-method-suggested-traits.rs:52:43
Expand All @@ -110,10 +108,8 @@ error[E0599]: no method named `method` found for type `std::rc::Rc<&mut std::box
= note: the following traits define an item `method`, perhaps you need to implement one of them:
candidate #1: `foo::Bar`
candidate #2: `no_method_suggested_traits::foo::PubPub`
candidate #3: `no_method_suggested_traits::bar::PubPriv`
candidate #4: `no_method_suggested_traits::qux::PrivPub`
candidate #5: `no_method_suggested_traits::quz::PrivPriv`
candidate #6: `no_method_suggested_traits::Reexported`
candidate #3: `no_method_suggested_traits::qux::PrivPub`
candidate #4: `no_method_suggested_traits::Reexported`

error[E0599]: no method named `method2` found for type `u64` in the current scope
--> $DIR/no-method-suggested-traits.rs:55:10
Expand Down
6 changes: 2 additions & 4 deletions src/test/ui/method-call-err-msg.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ error[E0599]: no method named `take` found for type `Foo` in the current scope
`&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::collections::hash::Recover`
candidate #2: `std::io::Read`
candidate #3: `std::iter::Iterator`
candidate #4: `alloc::btree::Recover`
candidate #1: `std::io::Read`
candidate #2: `std::iter::Iterator`

error: aborting due to 4 previous errors

16 changes: 16 additions & 0 deletions src/test/ui/suggestions/dont-suggest-private-trait-method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

struct T;

fn main() {
T::new();
//~^ ERROR no function or associated item named `new` found for type `T` in the current scope
}
11 changes: 11 additions & 0 deletions src/test/ui/suggestions/dont-suggest-private-trait-method.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E0599]: no function or associated item named `new` found for type `T` in the current scope
--> $DIR/dont-suggest-private-trait-method.rs:14:5
|
11 | struct T;
| --------- function or associated item `new` not found for this
...
14 | T::new();
| ^^^^^^ function or associated item not found in `T`

error: aborting due to previous error

0 comments on commit 4121ddb

Please sign in to comment.