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

Closed
shingtaklam1324 opened this issue Feb 9, 2018 · 4 comments · Fixed by #65951
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@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 estebank added A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. A-diagnostics Area: Messages for errors, warnings, and lints labels Feb 9, 2018
@estebank
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
Copy link
Author

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

@estebank
Copy link
Contributor

Triage: no change

@estebank
Copy link
Contributor

Output with #65951, which provides the right suggestion:

error[E0282]: type annotations needed
 --> file13.rs:3:9
  |
3 |     lst.sort_by_key(|&(v, _)| v.iter().sum());
  |         ^^^^^^^^^^^                    --- help: consider specifying the type argument in the method call: `sum::<S>`
  |         |
  |         cannot infer type for `K`

bors added a commit that referenced this issue Dec 11, 2019
Point at method call when type annotations are needed

- Point at method call instead of whole expression when type annotations are needed.
- Suggest use of turbofish on function and methods.

Fix #49391, fix #46333, fix #48089. CC #58517, #63502, #63082.

r? @nikomatsakis
@bors bors closed this as completed in 8843b28 Dec 14, 2019
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-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants