Skip to content

Help messages could include required use line. #77270

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

Open
follower opened this issue Sep 27, 2020 · 3 comments
Open

Help messages could include required use line. #77270

follower opened this issue Sep 27, 2020 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@follower
Copy link
Contributor

follower commented Sep 27, 2020

{This specific case originates from the following issue & comment: #47168 (comment) }

Playground link
I tried this code:

test(array.len());

I expected to see this happen:

error[E0308]: mismatched types
 --> src/main.rs:3:10
  |
3 |     test(array.len());
  |          ^^^^^^^^^^^ expected u32, found usize
help: you can convert an `usize` to `u32` or panic if it the converted value wouldn't fit
  |
3 |     use std::convert::TryInto;
4 |     test(array.len().try_into().unwrap());
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

(Or similar.)

i.e. An error message that includes the required use line necessary for the suggestion to compile.

Instead, this happened:

error[E0308]: mismatched types
 --> src/main.rs:3:10
  |
3 |     test(array.len());
  |          ^^^^^^^^^^^ expected u32, found usize
help: you can convert an `usize` to `u32` or panic if it the converted value wouldn't fit
  |
3 |     test(array.len().try_into().unwrap());
  |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

(Which would be fine if there is a pre-existing use std::convert::TryInto;.)

This meant that--after applying the suggested fix--when compiling I got another error relating to the suggested code:

error[E0599]: no method named `try_into` found for type `usize` in the current scope
 --> src/main.rs:3:22
  |
3 |     test(array.len().try_into().unwrap());
  |                      ^^^^^^^^
  |
  = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope, perhaps add a `use` for it:
  |
1 | use std::convert::TryInto;

This was surprising to me because I thought the compiler diagnostics were magic & infallible . :D

Note: Example code & messages were copy & pasted from original issue.

@follower follower added the C-bug Category: This is a bug. label Sep 27, 2020
@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. and removed C-bug Category: This is a bug. labels Sep 27, 2020
@jyn514 jyn514 added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Sep 27, 2020
@camelid camelid added the A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` label Sep 27, 2020
@JohnTitor JohnTitor added the P-low Low priority label Oct 10, 2020
@bishtpawan
Copy link
Contributor

Hi @follower, I've raised the PR by updating the error message but the build is failing. Can you please help me to understand the scenario?
Thanks

@bishtpawan
Copy link
Contributor

Hi @follower, did you get a chance to review the PR, and can you please help to understand the flow? Actually, I'm unable to update the message from where it is generating.
Thanks

@erik55
Copy link

erik55 commented Jan 14, 2021

I would vote for leaving it like it was. I know from other Linters (for example HLint), that they only show one hint at a time, not recursive hints. This hints and suggestions are for learning. You learn step by step, and are then able to understand every single step. If you change a lot of code at a time, in the end you don’t know why you did what.

Leave it like it was and close this unnecessary feature request.

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-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. P-low Low priority 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

7 participants