Skip to content

Misleading error messages when using the wrong macro fragment specifiers #132655

@szostid

Description

@szostid

Issue:

#[rustfmt::skip]
mod issue {
    struct SomeType<T>(std::marker::PhantomData<T>);

    macro_rules! test {
        ($ty:ty) => {
            impl<$ty,> SomeType<$ty> {}
        };
    }

    // Works fine
    impl<T,> SomeType<T> {}
    //   error: expected one of `>` or `as`, found `,`
    //   --> src\lib.rs:7:21
    //    |
    // 7  |             impl<$ty,> SomeType<$ty> {}
    //    |                     ^ expected one of `>` or `as`
    // ...
    // 12 |     test!(T);
    //    |     -------- in this macro invocation
    test!(T);

    macro_rules! test2 {
        ($ty:ty) => {
            impl<$ty> SomeType<$ty> {}
        };
    }

    // Works fine
    impl<T> SomeType<T> {}
    // error: expected `::`, found `SomeType`
    //   --> src\lib.rs:17:23
    //    |
    // 17 |             impl<$ty> SomeType<$ty> {}
    //    |                       ^^^^^^^^ expected `::`
    // ...
    // 22 |     test2!(T);
    //    |     --------- in this macro invocation
    test2!(T);
}

The macro should behave exactly like its manual expansion would; I've inlined the reported problems into the code.

Meta

The error happens in both stable and nightly toolchains, on linux and windows

rustc --version --verbose:

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-pc-windows-msvc
release: 1.82.0
LLVM version: 19.1.1

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, ..)D-confusingDiagnostics: Confusing error or lint that should be reworked.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions