Skip to content

Misplaced suggestion for missing generic parameter in macro invocation #90557

Open
@AuroransSolis

Description

@AuroransSolis

Given the following code:

struct Example<T, U> {
    foo: T,
    bar: U,
}

macro_rules! impl_example {
    ($($t:ty)+) => {$(
        impl Example<$t> {
            fn baz() {
                println!(":)");
            }
        }
    )}
}

impl_example! { u8 }

The current output is:

error[E0107]: this struct takes 2 generic arguments but 1 generic argument was supplied
  --> src/lib.rs:8:14
   |
8  |         impl Example<$t> {
   |              ^^^^^^^ expected 2 generic arguments
...
16 | impl_example! { u8 }
   | --------------------
   | |               |
   | |               supplied 1 generic argument
   | in this macro invocation
   |
note: struct defined here, with 2 generic parameters: `T`, `U`
  --> src/lib.rs:1:8
   |
1  | struct Example<T, U> {
   |        ^^^^^^^ -  -
   = note: this error originates in the macro `impl_example` (in Nightly builds, run with -Z macro-backtrace for more info)
help: add missing generic argument
   |
16 | impl_example! { u8, U }
   |                   +++

Most of the error message is good up until the help section, whereupon following it (by passing, say, u8, u8 to the macro call) leads to another error:

error: no rules expected the token `,`
  --> src/lib.rs:16:19
   |
6  | macro_rules! impl_example {
   | ------------------------- when calling this macro
...
16 | impl_example! { u8, u8 }
   |                   ^ no rules expected this token in macro call

Asking for another generic parameter is good, but it's being asked for in the wrong place.

As for the versions affected, I've tried it on the stable (1.56.1), nightly (2021-11-02 18bc4bee9710b181b440), and beta (2021-11-01 708d57e288d051a6238e) versions available on the Playground as of the time of writing, and all of them are affected.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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