Skip to content
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

All callable types could have better error messages in the "no method found" case to suggest that you may have forgotten to actually call them #29124

Closed
bstrie opened this issue Oct 17, 2015 · 12 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.

Comments

@bstrie
Copy link
Contributor

bstrie commented Oct 17, 2015

Taking a real-life example from #rust today:

fn main() {
    let mut guess = String::new();
    std::io::stdin.read_line(&mut guess);
}

Yields the following error message:

<anon>:3:20: 3:41 error: no method named `read_line` found for type `fn() -> std::io::stdio::Stdin {std::io::stdio::stdin}` in the current scope
<anon>:3     std::io::stdin.read_line(&mut guess);
                            ^~~~~~~~~~~~~~~~~~~~~

The problem is that the code should read stdin().read_line rather than stdin.read_line, but the error message only hints at that. How hard would it be to add a note to all "no method found" errors when the type is callable?

@bstrie bstrie changed the title All callable types could have better error messages in the "no method found" case to suggest that you may have forgotten to actuall call them All callable types could have better error messages in the "no method found" case to suggest that you may have forgotten to actually call them Oct 17, 2015
@bstrie bstrie added A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. labels Oct 17, 2015
@deej-io
Copy link
Contributor

deej-io commented Oct 19, 2015

I feel like this would be a great way for me to simultaneously start getting to know the compiler and to contribute to rust, so I'd like to take a look at this this evening.

Would anyone be willing to mentor should I need it?

@bstrie
Copy link
Contributor Author

bstrie commented Oct 22, 2015

@djrollins note that I only think that this should be an easy task, I don't have enough experience with the compiler to know that it's easy. :) Theoretically you should only need to find where the compiler is printing that specific error message, then add a "note" when the type mentioned in the error message implements any of Fn, FnMut, or FnOnce. You can look at many other error messages to see examples of "notes" being generated. Figuring out whether or not the type implements any of the Fn traits is probably going to be the trickiest part, because I'm not sure whether we have a convenient API to determine that for an arbitrary type. As a start you could merely implement the note only for bare functions (the fn() types) since they're always callable, though I'm not sure if it's actually any easier to distinguish fn from normal types compared to just looking up whether a type implements any of the callable traits.

For any compiler hacking, I'd recommend joining the #rust-internals IRC channel and casting around in there as you think of questions. That's also probably the best place to find a mentor. Good luck! :)

@deej-io
Copy link
Contributor

deej-io commented Oct 28, 2015

I've given it a go trying to match the type to a TyBareFn as I wasn't sure how I could check for trait implementations with the information available on MethodError. Should I try and get someone to look at it on my fork or should I make a pull request?

@Manishearth
Copy link
Member

There's a callable check here which you can try to use. Might be worth moving that out as a utility.

@Nashenas88
Copy link
Contributor

@djrollins I actually wrote the portion of code referenced by @Manishearth. I wrote a blog post detailing the changes here, which should help out if you're unfamiliar with the compiler. There's no guarantee that those portions of the compiler will be the same, but from what I saw in @Manishearth's link, the important parts are still the same. I haven't worked with the compiler in a little while, but I should still be able to help explain some portions if you need help.

@deej-io
Copy link
Contributor

deej-io commented Dec 2, 2015

Thank you all for all of the advice so far. @Nashenas88 I took the time to read your blog, it was a great read and very informative; I'm going to sit down and go through it again at the weekend before taking another punt at this issue.

@Manishearth Manishearth added the E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion. label Feb 29, 2016
@Manishearth
Copy link
Member

Are you still working on this?

@deej-io
Copy link
Contributor

deej-io commented Mar 1, 2016

Hey @Manishearth. Sorry, I've not actively looked at it for a while and let it slip away from me. I do want to carry on tackling this issue though if that's OK.

@vegai
Copy link
Contributor

vegai commented Mar 1, 2016

I'd like to give this a shot @Manishearth -- can I reach you in irc?

@Manishearth
Copy link
Member

Yes, but @djrollins is already working on this.

Try #31686 instead?

Manishearth pushed a commit to Manishearth/rust that referenced this issue Mar 19, 2016
Fixes issue rust-lang#29124.

If method is called on a function type a note is generated to suggest
that the developer may have forgotten to call it.

e.g.

fn main() {
    let mut guess = String::new();
    std::io::stdin.read_line(&mut guess);
}

will generate the note:

note: called method on function type. did you mean `std::io::stdin().read_line(..)`?
@sophiajt
Copy link
Contributor

@vegai - are you still working on this? I wouldn't mind picking it up if you've moved on, though if you're still working on it no worries :)

@Manishearth
Copy link
Member

@jonathandturner this landed as #32053 / #32358 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-easy Call for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue. E-mentor Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.
Projects
None yet
Development

No branches or pull requests

6 participants