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

Include trait signature in "type annotations needed" error message. #46333

Closed
vext01 opened this issue Nov 28, 2017 · 1 comment · Fixed by #65951
Closed

Include trait signature in "type annotations needed" error message. #46333

vext01 opened this issue Nov 28, 2017 · 1 comment · Fixed by #65951
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics

Comments

@vext01
Copy link
Contributor

vext01 commented Nov 28, 2017

With the following code:

use std::process::{Command, Stdio};                                             
                                                                                
fn main() {                                                                     
    let process = Command::new("wc")                                            
                          .stdout(Stdio::piped())                               
                          .spawn()                                              
                          .or_else(|err| {                                      
                              panic!("oh no: {:?}", err);                        
                          }).unwrap();                                          
}   

You will get the error message:

error[E0282]: type annotations needed
 --> src/main.rs:7:28
  |
7 |                           .or_else(|err| {
  |                            ^^^^^^^ cannot infer type for `F`

I can't help but feel that this is not useful, because it's not clear what F is.

A novice user would be baffled. If we are lucky they might look at the signature of or_else to see if F is defined there. It is:

fn or_else<F, O>(self, op: O) -> Result<T, F>
where
    O: FnOnce(E) -> Result<T, F>, 

I think it would be useful to include this signature in the error message, so that the user can see what F is without having to look it up in the docs.

Indeed, this error is fixed by adding types to the or_else call, like so:

.or_else::<io::Error, _>(|err| {
    ...
@kennytm kennytm added A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Nov 28, 2017
@estebank estebank added E-needs-mentor WG-diagnostics Working group: Diagnostics labels Dec 7, 2017
@estebank
Copy link
Contributor

Being able to display this is dependent on the work mentioned in #47319 (comment)

@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Dec 11, 2019
bors added a commit that referenced this issue Dec 11, 2019
Point at method call when type annotations are needed

- Point at method call instead of whole expression when type annotations are needed.
- Suggest use of turbofish on function and methods.

Fix #49391, fix #46333, fix #48089. CC #58517, #63502, #63082.

r? @nikomatsakis
@bors bors closed this as completed in 8843b28 Dec 14, 2019
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 A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants