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

Type inference asks for annotations that are already present #69214

Closed
alecmocatta opened this issue Feb 16, 2020 · 2 comments
Closed

Type inference asks for annotations that are already present #69214

alecmocatta opened this issue Feb 16, 2020 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@alecmocatta
Copy link
Contributor

use std::convert::TryInto;

fn main() {
    let _: usize = Some(0usize).map(|host: usize| 0usize).unwrap() + 0usize.try_into().unwrap();
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
error[E0284]: type annotations needed for `usize`
 --> src/main.rs:4:38
  |
4 |     let _: usize = Some(0usize).map(|host: usize| 0usize).unwrap() + 0usize.try_into().unwrap();
  |                                      ^^^^ consider giving this closure parameter a type
  |
  = note: cannot resolve `<usize as std::ops::Add<_>>::Output == usize`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0284`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 16, 2020
@Coder-256
Copy link
Contributor

That diagnostic is a bit misleading. This works:

Playground

use std::convert::TryInto;

fn main() {
    let _: usize = Some(0usize).map(|host: usize| 0usize).unwrap()
        + (0usize.try_into() as Result<usize, _>).unwrap();
}

I guess the compiler can't figure out the type of the 2nd addend, and by extension, what type you expect from try_into(). I think in this case, I think it's true that usize is the only type that satisfies T for both usize: Add<T, Output = usize> and usize: TryInto<T>. However, I am not convinced that the compiler should be expected to deduce this, especially because it is possible to implement both of these traits on usize for some other T (due to rust-lang/rfcs#2451). For example:

Playground

use std::{convert::TryInto, ops::Add};

struct Foo;

impl Add<Foo> for usize {
    type Output = usize;
    
    fn add(self, rhs: Foo) -> Self::Output {
        unimplemented!()
    }
}

impl TryInto<Foo> for usize {
    type Error = ();
    
    fn try_into(self) -> Result<Foo, Self::Error> {
        unimplemented!()
    }
}

In other words, I think it is expected that this would not compile, and the problem is the misleading diagnostic.

@alecmocatta
Copy link
Contributor Author

Closing as this is fixed in latest nightly, I suspect by #71960. Thanks @estebank!

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

3 participants