Skip to content

Do not mention traits on derive macro used out of scope #162260

Closed

Description

@estebank

Code

// https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=59c21350087b0a8594c3d41efc25b609

#[derive(Serialize)]
struct S;

Current output

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

Desired output

error: cannot find derive macro `Serialize` in this scope
   --> src/main.rs:1:10
    |
  1 | #[derive(Serialize)]
    |          ^^^^^^^^^
    |
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

Rationale and extra context

When the derive macro with an appropriate name is suggested, we should likely not point at the traits with the same name, as the likelihood of the derives being what was desired is quite high, and the verbosity of the error currently is not ideal. We could also instead mention only the full paths for the existing traits in a single note, to reduce the verbosity.

Noticed while looking at #102601.

Other cases

Rust Version

1.98.1

Anything else?

No response

Activity

  1. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-resolveArea: Name/path resolution done by `rustc_resolve` specifically
    P-lowLow priority
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.
    on Sep 3, 2026
  2. self-assigned this
    on Sep 4, 2026
  3. estebank commented on Sep 8, 2026

    @estebank
    ContributorAuthor

    Another marginal improvement we could do is for the case where there is no derive merge the notes into a single one (it saves a few lines):

    error: cannot find derive macro `Serialize` in this scope
       --> src/main.rs:1:10
        |
      1 | #[derive(Serialize)]
        |          ^^^^^^^^^
        |
    note: there are multiple `Serialize` traits, but no derive macro
       --> /playground/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/markup5ever-0.12.1/serialize.rs:36:1
        |
     36 | pub trait Serialize {
        | ^^^^^^^^^^^^^^^^^^^
       ::: /playground/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/markup5ever-0.11.0/serialize.rs:36:1
        |
     36 | pub trait Serialize {
        | ^^^^^^^^^^^^^^^^^^^
       ::: /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 {
        | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
       ::: /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
    
  4. added a commit that references this issue on Sep 14, 2026
    dccd073
  5. added a commit that references this issue on Sep 14, 2026
    ee892f2
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-resolveArea: Name/path resolution done by `rustc_resolve` specificallyD-verboseDiagnostics: Too much output caused by a single piece of incorrect code.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