提交 e26aac5a 编写于 作者: G Guillaume Gomez 提交者: GitHub

Rollup merge of #47534 - estebank:suggest-public-traits, r=petrochenkov

On missing method do not suggest private traits

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.

Fix #45781.
......@@ -513,8 +513,13 @@ fn suggest_traits_to_import(&self,
// 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 @@
#![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
// 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
}
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
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册