Skip to content

macro_attr feature gate error is spanned to the declaration of the macro when it should be the call site #147135

@kpreid

Description

@kpreid

I tried this code:

// foo/src/lib.rs
#![feature(macro_attr)]

#[macro_export]
macro_rules! identity {
    attr() (
        $( $item:tt )*
    ) => {
        $( $item )*
    }
}
// bar/src/lib.rs
#[foo::identity]
pub fn problem() {}

I expected to see this happen: Error pointing to bar/src/lib.rs:2

Instead, this happened:

error[E0658]: `macro_rules!` attributes are unstable
 --> /Users/kpreid/Desktop/attr/foo/src/lib.rs:4:1
  |
4 | macro_rules! identity {
  | ^^^^^^^^^^^^^^^^^^^^^
  |
  = note: see issue #83527 <https://github.com/rust-lang/rust/issues/83527> for more information
  = help: add `#![feature(macro_attr)]` to the crate attributes to enable
  = note: this compiler was built on 2025-09-27; consider upgrading it if it is out of date

For more information about this error, try `rustc --explain E0658`.
error: could not compile `bar` (lib) due to 1 previous error

Therefore, it is impossible to tell which crate needs the attribute added except by examining Cargo's “could not compile” line.

Meta

rustc --version --verbose:

rustc 1.92.0-nightly (4082d6a3f 2025-09-27)
binary: rustc
commit-hash: 4082d6a3f0347c2fc4b8c8d5a6a38ed7248fa161
commit-date: 2025-09-27
host: aarch64-apple-darwin
release: 1.92.0-nightly
LLVM version: 21.1.2

@rustbot label A-macros A-diagnostics A-stability T-compiler

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-stabilityArea: `#[stable]`, `#[unstable]` etc.C-bugCategory: This is a bug.D-imprecise-spansDiagnostics: spans don't point to exactly the erroneous codeT-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