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

Unclear error message for E0283 #45453

Closed
Riateche opened this issue Oct 22, 2017 · 4 comments
Closed

Unclear error message for E0283 #45453

Riateche opened this issue Oct 22, 2017 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints 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.

Comments

@Riateche
Copy link

Riateche commented Oct 22, 2017

Code:

fn func1<T: Into<String>>(x: i32) {}
fn main() {
    func1(42);
}

Error:

error[E0283]: type annotations required: cannot resolve `_: std::convert::Into<std::string::String>`
 --> src/main.rs:6:5
  |
6 |     func1(42);
  |     ^^^^^
  |
  = note: required by `func1`

error: aborting due to previous error

The error is actually caused by the function declaration (it was not supposed to have generic parameters), but without looking at the function it's impossible to understand the message. The error message should show signature of func1 and say that type annotation is required for T argument of func1.

By the way, is there a reason func1 doesn't trigger a warning by itself? I imagine there may be cases where you want functions with unbound type parameters, but I'm not sure about that.

@TimNN
Copy link
Contributor

TimNN commented Oct 24, 2017

The function signature itself is perfectly valid, you can even call it, e.g. func1::<String>(42);. However a warning on the function declaration maybe useful, potentially as a lint in clippy. And the error on use could probably be improved.

@TimNN TimNN added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Oct 24, 2017
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this issue Jan 17, 2018
…nkfelix

On E0283, point at method with the requirements

On required type annotation diagnostic error, point at method with the
requirements if the span is available.

CC rust-lang#45453.
@estebank
Copy link
Contributor

Current output:

error[E0283]: type annotations required: cannot resolve `_: std::convert::Into<std::string::String>`
 --> src/main.rs:3:5
  |
3 |     func1(42);
  |     ^^^^^
  |
note: required by `func1`
 --> src/main.rs:1:1
  |
1 | fn func1<T: Into<String>>(x: i32) {}
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

It should refer to T instead of _ at the very least, it'd be even better if the span pointed at T: Into<String>.

@mykmelez
Copy link

I'm unsure that it's the same issue, but (as a relative newcomer to Rust) I was also confused by:

error[E0283]: type annotations required: cannot resolve `_: std::iter::FromIterator<()>`
  --> src/main.rs:83:65
   |
83 | ["a", "b", "c"].iter().map(|elt| -> () { println!("{}", elt) }).collect();
   |                                                                 ^^^^^^^

Perhaps it would be helpful for the error message to suggest at least the syntax required, something like:

["a", "b", "c"].iter().map(|elt| -> () { println!("{}", elt) }).collect::<annotation>();

Even if it can't determine the specific type to annotate; which, in this case, seems to be the unit type:

["a", "b", "c"].iter().map(|elt| -> () { println!("{}", elt) }).collect::<()>();

@estebank
Copy link
Contributor

Current output:

error[E0283]: type annotations needed
 --> src/main.rs:3:5
  |
1 | fn func1<T: Into<String>>(x: i32) {}
  |    -----    ------------ required by this bound in `func1`
2 | fn main() {
3 |     func1(42);
  |     ^^^^^
  |     |
  |     cannot infer type for type parameter `T` declared on the function `func1`
  |     help: consider specifying the type argument in the function call: `func1::<T>`
  |
  = note: cannot resolve `_: std::convert::Into<std::string::String>`

For the last comment, the output is:

error[E0283]: type annotations needed
 --> src/main.rs:2:69
  |
2 |     ["a", "b", "c"].iter().map(|elt| -> () { println!("{}", elt) }).collect();
  |                                                                     ^^^^^^^
  |                                                                     |
  |                                                                     cannot infer type for type parameter `B` declared on the method `collect`
  |                                                                     help: consider specifying the type argument in the method call: `collect::<B>`
  |
  = note: cannot resolve `_: std::iter::FromIterator<()>`

I believe we give enough context now. We do not take a guess at what could be a reasonable type for the turbofish ::<B>, but we provide the syntax and if you follow it blindly you get cannot find type B in this scope which should be enough to figure out you have to put a type there.

@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 17, 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 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.
Projects
None yet
Development

No branches or pull requests

4 participants