Skip to content

Commit e26aac5

Browse files
authoredJan 25, 2018
Rollup merge of rust-lang#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 rust-lang#45781.
2 parents 4cf26f8 + 4121ddb commit e26aac5

File tree

6 files changed

+42
-15
lines changed

6 files changed

+42
-15
lines changed
 

‎src/librustc_typeck/check/method/suggest.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
513513
// this isn't perfect (that is, there are cases when
514514
// implementing a trait would be legal but is rejected
515515
// here).
516-
(type_is_local || info.def_id.is_local())
517-
&& self.associated_item(info.def_id, item_name, Namespace::Value).is_some()
516+
(type_is_local || info.def_id.is_local()) &&
517+
self.associated_item(info.def_id, item_name, Namespace::Value)
518+
.filter(|item| {
519+
// We only want to suggest public or local traits (#45781).
520+
item.vis == ty::Visibility::Public || info.def_id.is_local()
521+
})
522+
.is_some()
518523
})
519524
.collect::<Vec<_>>();
520525

‎src/librustc_typeck/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,13 @@ This API is completely unstable and subject to change.
7575
#![feature(advanced_slice_patterns)]
7676
#![feature(box_patterns)]
7777
#![feature(box_syntax)]
78-
#![feature(crate_visibility_modifier)]
7978
#![feature(conservative_impl_trait)]
8079
#![feature(copy_closures, clone_closures)]
80+
#![feature(crate_visibility_modifier)]
8181
#![feature(from_ref)]
8282
#![feature(match_default_bindings)]
8383
#![feature(never_type)]
84+
#![feature(option_filter)]
8485
#![feature(quote)]
8586
#![feature(refcell_replace_swap)]
8687
#![feature(rustc_diagnostic_macros)]

‎src/test/ui/impl-trait/no-method-suggested-traits.stderr

+4-8
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,8 @@ error[E0599]: no method named `method` found for type `Foo` in the current scope
9595
= note: the following traits define an item `method`, perhaps you need to implement one of them:
9696
candidate #1: `foo::Bar`
9797
candidate #2: `no_method_suggested_traits::foo::PubPub`
98-
candidate #3: `no_method_suggested_traits::bar::PubPriv`
99-
candidate #4: `no_method_suggested_traits::qux::PrivPub`
100-
candidate #5: `no_method_suggested_traits::quz::PrivPriv`
101-
candidate #6: `no_method_suggested_traits::Reexported`
98+
candidate #3: `no_method_suggested_traits::qux::PrivPub`
99+
candidate #4: `no_method_suggested_traits::Reexported`
102100

103101
error[E0599]: no method named `method` found for type `std::rc::Rc<&mut std::boxed::Box<&Foo>>` in the current scope
104102
--> $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
110108
= note: the following traits define an item `method`, perhaps you need to implement one of them:
111109
candidate #1: `foo::Bar`
112110
candidate #2: `no_method_suggested_traits::foo::PubPub`
113-
candidate #3: `no_method_suggested_traits::bar::PubPriv`
114-
candidate #4: `no_method_suggested_traits::qux::PrivPub`
115-
candidate #5: `no_method_suggested_traits::quz::PrivPriv`
116-
candidate #6: `no_method_suggested_traits::Reexported`
111+
candidate #3: `no_method_suggested_traits::qux::PrivPub`
112+
candidate #4: `no_method_suggested_traits::Reexported`
117113

118114
error[E0599]: no method named `method2` found for type `u64` in the current scope
119115
--> $DIR/no-method-suggested-traits.rs:55:10

‎src/test/ui/method-call-err-msg.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ error[E0599]: no method named `take` found for type `Foo` in the current scope
3838
`&mut Foo : std::iter::Iterator`
3939
= help: items from traits can only be used if the trait is implemented and in scope
4040
= note: the following traits define an item `take`, perhaps you need to implement one of them:
41-
candidate #1: `std::collections::hash::Recover`
42-
candidate #2: `std::io::Read`
43-
candidate #3: `std::iter::Iterator`
44-
candidate #4: `alloc::btree::Recover`
41+
candidate #1: `std::io::Read`
42+
candidate #2: `std::iter::Iterator`
4543

4644
error: aborting due to 4 previous errors
4745

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
struct T;
12+
13+
fn main() {
14+
T::new();
15+
//~^ ERROR no function or associated item named `new` found for type `T` in the current scope
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error[E0599]: no function or associated item named `new` found for type `T` in the current scope
2+
--> $DIR/dont-suggest-private-trait-method.rs:14:5
3+
|
4+
11 | struct T;
5+
| --------- function or associated item `new` not found for this
6+
...
7+
14 | T::new();
8+
| ^^^^^^ function or associated item not found in `T`
9+
10+
error: aborting due to previous error
11+

0 commit comments

Comments
 (0)
Please sign in to comment.