Skip to content

Show an actionable help text to add a "use ..." on error "cannot find macro" #102601

Open
@nyurik

Description

@nyurik

Given the following code:

fn main() {
    bitflags! {
        struct Flags: u32 {
            const A = 0b00000001;
        }
    }
}

The current output is:

error: cannot find macro `bitflags` in this scope
 --> src/main.rs:2:5
  |
2 |     bitflags! {
  |     ^^^^^^^^
  |
  = note: consider importing this macro:
          bitflags::bitflags
  = note: `bitflags` is in scope, but it is a crate, not a macro

The output should include an actionable help that tells novice users what to do in a way that can be copy/pasted, instead of just specifying which macro to use. Modeling after the error message of a missing type, the missing macro message should look like this (in green):

help: consider importing this macro
    |
1   | use bitflags::bitflags;
    |

Same for Derive macros

The same should also be done for the derive macros, e.g. instead of this:

error: cannot find derive macro `Bytenum` in this scope
 --> src/main.rs:4:10
  |
4 | #[derive(Bytenum, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
  |          ^^^^^^^
  |
  = help: consider importing this derive macro:
          bytenum::Bytenum

the help message should be

  = help: consider importing this derive macro:
    |
1   | use bytenum::Bytenum;
    |

Relevant code

See also the code that generates the message above: compiler/rustc_resolve/src/diagnostics.rs

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-papercutDiagnostics: An error or lint that needs small tweaks.T-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