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

Weird compiler error for type annotations #64524

Open
olegnn opened this issue Sep 16, 2019 · 3 comments
Open

Weird compiler error for type annotations #64524

olegnn opened this issue Sep 16, 2019 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@olegnn
Copy link
Contributor

olegnn commented Sep 16, 2019

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`
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 16, 2019
@estebank
Copy link
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.

@Spoonbender
Copy link

Triage: no change

@estebank
Copy link
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
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants