Skip to content

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

Closed

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

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Oct 2, 2022
  2. chenyukang commented on Oct 3, 2022

    @chenyukang
    Member

    @rustbot claim

  3. added
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    D-papercutDiagnostics: An error or lint that needs small tweaks.
    on Oct 3, 2022
  4. removed their assignment
    on Oct 4, 2022
  5. togami2864 commented on Oct 10, 2022

    @togami2864
    Contributor

    @rustbot claim

  6. nyurik commented on Mar 15, 2023

    @nyurik
    ContributorAuthor

    Apparently the derive macros also have this shortcoming, added an example above

  7. estebank commented on Sep 3, 2026

    @estebank
    Contributor

    Current output:

    error: cannot find macro `bitflags` in this scope
     --> src/main.rs:5:5
      |
    5 |     bitflags! {
      |     ^^^^^^^^
      |
      = note: `bitflags` is in scope, but it is a crate, not a macro
    help: consider importing this macro
      |
    4 + use bitflags::bitflags;
      |
    
    error: cannot find derive macro `Serialize` in this scope
       --> src/main.rs:1:10
        |
      1 | #[derive(Serialize)]
        |          ^^^^^^^^^
        |
    note: `Serialize` is a trait, not a derive macro
       --> /playground/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/markup5ever-0.12.1/serialize.rs:36:1
        |
     36 | pub trait Serialize {
        | ^^^^^^^^^^^^^^^^^^^
    note: `Serialize` is a trait, not a derive macro
       --> /playground/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/markup5ever-0.11.0/serialize.rs:36:1
        |
     36 | pub trait Serialize {
        | ^^^^^^^^^^^^^^^^^^^
    note: `Serialize` is a trait, not a derive macro
       --> /playground/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/rkyv-0.8.18/src/traits.rs:262:1
        |
    262 | pub trait Serialize<S: Fallible + ?Sized>: Archive {
        | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    note: `Serialize` is a trait, not a derive macro
       --> /playground/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/serde_core-1.0.229/src/ser/mod.rs:234:1
        |
    234 | pub trait Serialize {
        | ^^^^^^^^^^^^^^^^^^^
        = help: consider implementing `Serialize` for your type manually
    help: consider importing one of these derive macros
        |
      1 + use noop_proc_macro::Serialize;
        |
      1 + use rkyv::Serialize;
        |
      1 + use rkyv_derive::Serialize;
        |
      1 + use serde::Serialize;
        |
        = and 1 other candidate
    

    We could probably clean up the case where there are multiple traits and derives with the same name to be less verbose. It would make sense to not even mention existing traits if importing the derives are suggested. But that's besides the point, the original report is now fixed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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