Skip to content

Attribute macro within macro_rules! macro causes error messages to lose spans #75801

@wuggen

Description

@wuggen

I tried the following on the current nightly build:

macro_rules! foo {
    ($arg:expr) => {
        #[foo]
        fn bar() {
            let _bar: u32 = $arg;
        }
    };
}
foo!("baz");

// In a different, proc-macro crate:
#[proc_macro_attribute]
pub fn foo(_args: TokenStream, item: TokenStream) -> TokenStream {
    item
}

I expected to see something like the following:

error[E0308]: mismatched types
 --> src/lib.rs:3:9
  |
  | foo!("baz");
  |       ^^^^^
  |      |
  |      expected `u32`, found `&str`

error: aborting due to previous error

Instead, the error message lacks any span or additional context at all:

error[E0308]: mismatched types

error: aborting due to previous error

Removing the #[foo] from the macro_rules! body, or writing #[foo] fn bar() { ... } manually without going through the foo! macro, produces a correct error message; it's only when they are combined that the error loses contextual information.

Meta

The current beta and nightly builds are affected:

rustc --version --verbose:

rustc 1.47.0-nightly (de521cbb3 2020-08-21)
binary: rustc
commit-hash: de521cbb303c08febd9fa3755caccd4f3e491ea3
commit-date: 2020-08-21
host: x86_64-unknown-linux-gnu
release: 1.47.0-nightly
LLVM version: 10.0
rustc 1.46.0-beta.4 (32c481e15 2020-08-09)
binary: rustc
commit-hash: 32c481e156c6431d2947d865bd2abc2315a931fc
commit-date: 2020-08-09
host: x86_64-unknown-linux-gnu
release: 1.46.0-beta.4
LLVM version: 10.0

The current stable version displays the expected behavior.

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-proc-macrosArea: Procedural macrosC-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions