Skip to content

Help messages could include required use line. #77270

Open
@follower

Description

@follower

{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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions