rust-lang / rust-lang/rust

Misleading error messages when using the wrong macro fragment specifiers

Open
#132,655 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-diagnostics A-macros D-confusing
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the inline macro reproducer with the affected rustc toolchain and compare its diagnostics with the manual expansions shown in the issue. The issue names no source files or tests, so locate the compiler handling for macro fragment expansion and diagnostics. Done means the macro errors communicate the corresponding manual-expansion problem rather than pointing to misleading tokens.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.