Skip to content

Duplicate in this macro invocation labels #116836

Closed
@gurry

Description

@gurry

Code

enum Enum<T1, T2> {  VariantA { _v1: T1, _v2: T2 }, VariantB }

type EnumUnit = Enum<(), ()>;

macro_rules! test {
    () => (if let EnumUnit::VariantB::<i32, u32> {} = 5 { true } else { false });
}

fn main() {
    test!();
}

Current output

error[E0109]: type arguments are not allowed on this type
  --> ../dup-macro-invoc-diag.rs:6:40
   |
6  |     () => (if let EnumUnit::VariantB::<i32, u32> {} = 5 { true } else { false });
   |                             --------   ^^^  ^^^ type argument not allowed
   |                             |
   |                             not allowed on this type
...
10 |     test!();
   |     -------
   |     |
   |     in this macro invocation
   |     in this macro invocation
   |
   = note: enum variants can't have type parameters
   = note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might have meant to specify type parameters on enum `Enum`
   |
6  -     () => (if let EnumUnit::VariantB::<i32, u32> {} = 5 { true } else { false });
6  +     () => (if let EnumUnit::<i32, u32>::VariantB {} = 5 { true } else { false });
   |

Desired output

error[E0109]: type arguments are not allowed on this type
  --> ../dup-macro-invoc-diag.rs:6:40
   |
6  |     () => (if let EnumUnit::VariantB::<i32, u32> {} = 5 { true } else { false });
   |                             --------   ^^^  ^^^ type argument not allowed
   |                             |
   |                             not allowed on this type
...
10 |     test!();
   |     -------
   |     |
   |     in this macro invocation
   |
   = note: enum variants can't have type parameters
   = note: this error originates in the macro `test` (in Nightly builds, run with -Z macro-backtrace for more info)
help: you might have meant to specify type parameters on enum `Enum`
   |
6  -     () => (if let EnumUnit::VariantB::<i32, u32> {} = 5 { true } else { false });
6  +     () => (if let EnumUnit::<i32, u32>::VariantB {} = 5 { true } else { false });
   |

Rationale and extra context

The label in this macro invocation should appear only once.

Other cases

No response

Anything else?

The number of label repetitions equal the number of primary macro spans in the diagnostic. In the above code we have two primary spans:

6  |     () => (if let EnumUnit::VariantB::<i32, u32> {} = 5 { true } else { false });
   |                             --------   ^^^  ^^^ type argument not allowed

Therefore we see two repetitions of the in this macro invocation label:

10 |     test!();
   |     -------
   |     |
   |     in this macro invocation
   |     in this macro invocation

If we add another type param to enum Enum<T1, T2> to make it enum Enum<T1, T2, T3> and correspondingly update the macro it will result in three primary spans and therefore we will now see three occurrences of the label.

Metadata

rustc 1.72.1 (d5c2e9c 2023-09-13)
binary: rustc
commit-hash: d5c2e9c
commit-date: 2023-09-13
host: x86_64-pc-windows-msvc
release: 1.72.1
LLVM version: 16.0.5

Metadata

Metadata

Assignees

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