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

Wrong error message for missed type inference #71584

Closed
leonardo-m opened this issue Apr 26, 2020 · 3 comments · Fixed by #71960
Closed

Wrong error message for missed type inference #71584

leonardo-m opened this issue Apr 26, 2020 · 3 comments · Fixed by #71960
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

@leonardo-m
Copy link

Code that asks for some type inference:

fn main() {
    let n: u32 = 1;
    let mut d: u64 = 2;
    d = d % n.into();
}

It gives:

error[E0284]: type annotations needed for `u64`
 --> ...\test.rs:4:11
  |
3 |     let mut d: u64 = 2;
  |         ----- consider giving `d` a type
4 |     d = d % n.into();
  |           ^ cannot infer type for type `u64`
  |
  = note: cannot satisfy `<u64 as std::ops::Rem<_>>::Output == u64`

The error message "consider giving d a type" refers to a "d" that is actually typed as u64.
Using rustc 1.44.0-nightly (3360cc3 2020-04-24).

@leonardo-m leonardo-m added the C-bug Category: This is a bug. label Apr 26, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 26, 2020
@cuviper
Copy link
Member

cuviper commented Apr 26, 2020

This is similar to my #71538, although your example is much simpler.

@estebank
Copy link
Contributor

CC #70082 #69683 #69455 #69214

@jonaskoelker
Copy link

I've seen the same(ish) bug on stable rustc 1.43.0 (4fb7144ed 2020-04-20), so the fault is apparently not a regression from my stable 1.43 to OP's nightly 1.44. I'm using this code:

fn main() {
    if true {
        let f = |number: usize| number;
    } else {
        let confusion: usize = 0usize + 0usize.into();
    }
}

fn main2() {
    let f = |number: usize| number;
    let confusion: usize = 0usize + 0usize.into();
}

fn main3() {
    let x: usize = 0usize + 0usize.into();
}

The errors are

-*- mode: compilation; default-directory: "~/src/rustc-misleading-error-message/src/" -*-
Compilation started at Wed Apr 29 00:02:23

cargo build --bin bug
   Compiling misleading v0.1.0 (/home/jonas/src/rustc-misleading-error-message)
error[E0284]: type annotations needed for `usize`
 --> src/main.rs:3:18
  |
3 |         let f = |number: usize| number;
  |                  ^^^^^^ consider giving this closure parameter a type
  |
  = note: cannot resolve `<usize as std::ops::Add<_>>::Output == usize`

error[E0284]: type annotations needed for `usize`
  --> src/main.rs:10:14
   |
10 |     let f = |number: usize| number;
   |              ^^^^^^ consider giving this closure parameter a type
   |
   = note: cannot resolve `<usize as std::ops::Add<_>>::Output == usize`

error[E0284]: type annotations needed for `usize`
  --> src/main.rs:15:27
   |
15 |     let x: usize = 0usize + 0usize.into();
   |         -                 ^ cannot infer type for type `usize`
   |         |
   |         consider giving `x` a type
   |
   = note: cannot resolve `<usize as std::ops::Add<_>>::Output == usize`

error: aborting due to 3 previous errors

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

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

Compilation exited abnormally with code 101 at Wed Apr 29 00:02:23

(copy-pasted from an emacs compilation buffer)

It amuses me that |x: t| x "needs" a t type annotation thanks to code inside a never-taken branch. One would think that deleting dead code is a behavior-preserving transformation, at least when no error messages are given about the dead code :)

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

Successfully merging a pull request may close this issue.

5 participants