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

Diagnostics doesn't provide proper explaination as to where type annotations should be added #48089

Open
shingtaklam1324 opened this Issue Feb 9, 2018 · 2 comments

Comments

Projects
None yet
2 participants
@shingtaklam1324
Copy link

shingtaklam1324 commented Feb 9, 2018

Code

fn main() {
    let lst: [([i32; 10], bool); 10] = [([0; 10], false); 10];
    lst.sort_by_key(|&(v, _)| v.iter().sum());
    println!("{:?}", lst);
}

Current Error message

This fails compilation (on purpose), and the error message is as below

error[E0282]: type annotations needed
 --> src/main.rs:3:9
  |
3 |     lst.sort_by_key(|&(v, _)| v.iter().sum());
  |         ^^^^^^^^^^^ cannot infer type for `B`

error: aborting due to previous error

error: Could not compile `playground`.

Problem

The current error message isn't helpful by producing a message with the type B, which can be confusing to users, (it comes from te type signature of fn sort_by_key<B, F>(&mut self, _: F) where B: Ord, F: FnMut(&T) -> B as estebank points out).As well as that, the positions of the arrows implies that the error comes from .sort_by_key(), when in fact it comes from the need for type annotations for .sum().

Proposed solution

error[E0282]: type annotations needed
 --> src/main.rs:3:9
  |
3 |     lst.sort_by_key(|&(v, _)| v.iter().sum());
  |                               ^^^^^^^^^^^^^^ cannot infer type for `B`

error: aborting due to previous error

error: Could not compile `playground`.

The proposed change would be to point to the inside of the function, which would (in my opinion) be clearer to the user that the type annotations are needed inside the function, not with .sort_by_key()

Playground

@estebank

This comment has been minimized.

Copy link
Contributor

estebank commented Feb 9, 2018

I would go further and say that we should always point to the definition span of the method that we failed to infer a type argument for, and when it is from another crate, present a reasonable signature, as we do for other diagnostics:

error[E0282]: type annotations needed
 --> src/main.rs:3:9
  |
3 |     lst.sort_by_key(|&(v, _)| v.iter().sum());
  |                               ^^^^^^^^^^^^^^ cannot infer type for `B`
  = note: could not infer type `B` for method `fn sort_by_key<B, F>(&mut self, _: F) where B: Ord, F: FnMut(&T) -> B`

In cases where the type argument comes from a trait, I'd point at the trait definition and the method.

@shingtaklam1324

This comment has been minimized.

Copy link
Author

shingtaklam1324 commented Feb 10, 2018

After further reviewing, this is similar to #46333 and also dependent on #47319.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.