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

Unhelpful highlighting in type inference error #68063

Open
Torrencem opened this issue Jan 9, 2020 · 1 comment
Open

Unhelpful highlighting in type inference error #68063

Torrencem opened this issue Jan 9, 2020 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Torrencem
Copy link

MWE of a type inference error that came up in a real project:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=cd3198d1a30439a01ca2cfa3a78b1124

The user should fix this error by adding an explicit type for the {integer} literal 1 (i.e. 1u32 or 1u64), since these could do completely different things for the generic type T. The compiler catches this, but the highlighting seems to imply that it can't infer the type of the entire expression !(number >> 1), when in reality the problem is with the 1: no matter what type 1 takes, the full expression is always type T. This is pretty confusing

@Torrencem
Copy link
Author

Full code for reference:

use std::ops::{Shr, Not};

trait MyTrait: 
  Shr<u32, Output=Self> + Shr<u64, Output=Self>
+ Not<Output=Self>
+ Sized {}
impl MyTrait for usize {}

fn neg_shift_right_by_one<T: MyTrait>(number: T) -> T {
    !(number >> 1)
}

gives:

error[E0282]: type annotations needed
  --> src/lib.rs:11:5
   |
11 |     !(number >> 1)
   |     ^^^^^^^^^^^^^^ cannot infer type
   |
   = note: type must be known at this point

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-inference Area: Type inference C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jan 9, 2020
@estebank estebank added the D-confusing Diagnostics: Confusing error or lint that should be reworked. label Jan 10, 2020
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-enhancement Category: An issue proposing an enhancement or a PR with one. D-confusing Diagnostics: Confusing error or lint that should be reworked. 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