Skip to content

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

Closed
@vext01

Description

@vext01

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| {
    ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-inferenceArea: Type inferenceC-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions