-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Implement suggestions for traits to import. #21008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
r? @pcwalton (rust_highfive has picked a reviewer for you, use r? to override) |
This... isn't quite ready yet. The test's full output is:
In particular: it lists private traits too. |
(Also, the first help's wording is awkward.) Would appreciate any guidance for either. |
Repeating the span for each help message sounds redundant. |
.any(|item| { | ||
debug!("report_error: item: {:?}", *item); | ||
match *item { | ||
ty::MethodTraitItem(ref meth) => meth.name == method_name, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you may want to filter out private methods here ( meth.vis
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a trait, so there's no distinction.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah, you're right.
Improved it slightly, but it has the reexported path backwards (i.e. filtering by visibility removed the no-method-suggested-traits.rs:23:10: 23:18 error: type `u32` does not implement any method in scope named `method`
no-method-suggested-traits.rs:23 1u32.method();
^~~~~~~~
no-method-suggested-traits.rs:23:10: 23:18 help: methods from traits can only be called if the trait is implemented and in scope; the following traits define a method `method`:
no-method-suggested-traits.rs:23 1u32.method();
^~~~~~~~
no-method-suggested-traits.rs:17:5: 19:6 help: candidate #1: `foo::Bar`
no-method-suggested-traits.rs:17 trait Bar { //~ HELP `foo::Bar`
no-method-suggested-traits.rs:18 fn method(&self);
no-method-suggested-traits.rs:19 }
no-method-suggested-traits.rs:23:10: 23:18 help: candidate #2: `no_method_suggested_traits::foo::PubPub`
no-method-suggested-traits.rs:23 1u32.method();
^~~~~~~~
no-method-suggested-traits.rs:23:10: 23:18 help: candidate #3: `no_method_suggested_traits::reexport::Reexported`
no-method-suggested-traits.rs:23 1u32.method();
^~~~~~~~
error: aborting due to previous error |
@@ -340,6 +345,42 @@ pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, | |||
|
|||
report_candidates(fcx, span, method_name, static_sources); | |||
} | |||
|
|||
let candidates = all_traits(fcx.ccx) | |||
.filter(|did| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
goodness but do we need some helpers for these kinds of things...
Also, let's pull this code into its own file |
6713a40
to
012ca02
Compare
For a call like `foo.bar()` where the method `bar` can't be resolved, the compiler will search for traits that have methods with name `bar` to give a more informative error, providing a list of possibilities. Closes rust-lang#7643.
012ca02
to
06ad8bb
Compare
Updated with tweaks and better organisation. The test output now looks like:
|
I've made a few attempts to e.g. extract whether a non-local trait can be accessed from the current crate but I have not been able to achieve it in a correct-enough way as yet. |
@huonw do you find this is a big issue in practice? I would have expected we could kind of just suggest everything and the user can sort it out (and/or leave the screening of unreachable traits as a fixme). |
anyway, I think we could land the code as is, but with some relatively simple tweaks we could at least indicate whether the trait is implemented -- so we could do something like this, only print out the traits that are implemented but not imported, and if none are implemented, suggest traits to implement. That's probably good enough. |
Updated again, now with is-trait-implemented filtering.
|
If `a.method();` can't be resolved, we first look for implemented traits globally and suggest those. If there are no such traits found, we only then fall back to suggesting from the unfiltered list of traits.
62e47ad
to
0a55aac
Compare
Nice, I'll read over the code shortly. I think we should modify the error message to make it clearer what it means to be "in scope" -- like we should say "try adding |
So I had a few nits on huonw@0a55aac but @huonw said he wouldn't be around much over the next two days. So I'm giving
Nonetheless, I think this is a big step up in clarify, so I'd like to land. |
For a call like `foo.bar()` where the method `bar` can't be resolved, the compiler will search for traits that have methods with name `bar` to give a more informative error, providing a list of possibilities. Closes #7643.
For a call like
foo.bar()
where the methodbar
can't be resolved,the compiler will search for traits that have methods with name
bar
togive a more informative error, providing a list of possibilities.
Closes #7643.