Skip to content

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

Closed
@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.

Activity

Aaron1011

Aaron1011 commented on Aug 22, 2020

@Aaron1011
Member

Bisected to #73345

This is caused by the fact that we no longer pass a single NtItem to #[foo] - instead, we pass the individual tokens of the bar function. Since the tokenstream contains an NtExpr (for the $arg metavariable), the pretty-print/reparse check fails, causing us to lose spans.

This will be fixed by #73084

added
A-diagnosticsArea: Messages for errors, warnings, and lints
A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)
on Aug 22, 2020
Aaron1011

Aaron1011 commented on Sep 5, 2020

@Aaron1011
Member

Error in the latest nightly:

error[E0308]: mismatched types
 --> src/main.rs:9:6
  |
5 |             let _bar: u32 = $arg;
  |                       --- expected due to this
...
9 | foo!("baz");
  |      ^^^^^ expected `u32`, found `&str`

error: aborting due to previous error
added
E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.
on Sep 5, 2020
added 2 commits that reference this issue on Mar 30, 2021

Rollup merge of rust-lang#83671 - JohnTitor:issue-75801-test, r=Dylan…

f821e25

Rollup merge of rust-lang#83671 - JohnTitor:issue-75801-test, r=Dylan…

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

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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @Aaron1011@jonas-schievink@wuggen

      Issue actions

        Attribute macro within macro_rules! macro causes error messages to lose spans · Issue #75801 · rust-lang/rust