Skip to content

Weird compiler error for type annotations #64524

Open

Description

@olegnn
use std::thread;

fn main() {
    let result = {
        let spawned_result = thread::spawn(|| 3).join();
        let result = spawned_result.map_err(|e| e.into());
        let handler = |a| Ok(a + 1);
        result.and_then(handler)
    };

    println!("{:?}", result);
}

(Playground)

Error will be

error[E0282]: type annotations needed for `std::result::Result<i32, E>`
 --> src/main.rs:6:37
  |
4 |     let result = {
  |         ------ consider giving `result` the explicit type `std::result::Result<i32, E>`, where the type parameter `F` is specified
5 |         let spawned_result = thread::spawn(|| 3).join();
6 |         let result = spawned_result.map_err(|e| e.into());
  |                                     ^^^^^^^ cannot infer type for `F`

Which is a little bit confusing because there's no type parameter F in std::result::Result<i32, E>.

On the other hand, if we place let handler = |a| Ok(a + 1); before let result = spawned_result.map_err(|e| e.into());, we will receive correct message:

error[E0282]: type annotations needed for `std::result::Result<i32, E>`
 --> src/main.rs:6:27
  |
4 |     let result = {
  |         ------ consider giving `result` the explicit type `std::result::Result<i32, E>`, where the type parameter `E` is specified
5 |         let spawned_result = thread::spawn(|| 3).join();
6 |         let handler = |a| Ok(a + 1);
  |                           ^^ cannot infer type for `E`

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Sep 16, 2019
  2. estebank commented on Sep 16, 2019

    @estebank
    Contributor

    I believe the F it's referring to here is the one in thread::spawn<F, T>(f: F), while the E it should be talking about is the one in Result<T, E>. I believe we're mixing the type argument name from the return and the input.

  3. Spoonbender commented on Mar 22, 2022

    @Spoonbender

    Triage: no change

  4. estebank commented on Aug 14, 2023

    @estebank
    Contributor

    Current output:

    error[E0282]: type annotations needed for `Result<i32, _>`
     --> f71.rs:6:13
      |
    6 |         let result = spawned_result.map_err(|e| e.into());
      |             ^^^^^^
      |
    help: consider giving `result` an explicit type, where the type for type parameter `F` is specified
      |
    6 |         let result: Result<i32, _> = spawned_result.map_err(|e| e.into());
      |                   ++++++++++++++++
    

    After #114811:

    error[E0283]: type annotations needed for `Result<i32, _>`
     --> f71.rs:6:13
      |
    6 |         let result = spawned_result.map_err(|e| e.into());
      |             ^^^^^^                                ---- type must be known at this point
      |
      = note: cannot satisfy `_: From<Box<dyn Any + Send>>`
      = note: required for `Box<dyn Any + Send>` to implement `Into<_>`
    help: consider giving `result` an explicit type, where the type for type parameter `U` is specified
      |
    6 |         let result: Result<i32, _> = spawned_result.map_err(|e| e.into());
      |                   ++++++++++++++++
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions