Do not suggest private traits that have missing method
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.
This commit is contained in:
parent
fdc18b3067
commit
4121ddb041
@ -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<_>>();
|
||||
|
||||
|
@ -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)]
|
||||
|
@ -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
|
||||
@ -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
|
||||
|
@ -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
src/test/ui/suggestions/dont-suggest-private-trait-method.rs
Normal file
16
src/test/ui/suggestions/dont-suggest-private-trait-method.rs
Normal file
@ -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
|
||||
}
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user