Skip to content

Duplicate in this macro invocation labels #116836

Closed
@gurry

Description

@gurry
Contributor

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

Activity

added
A-diagnosticsArea: Messages for errors, warnings, and lints
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Oct 17, 2023
added
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Oct 17, 2023
gurry

gurry commented on Oct 17, 2023

@gurry
ContributorAuthor

The issue occurs in this function:

fn render_multispan_macro_backtrace(&self, span: &mut MultiSpan, always_backtrace: bool) {

where we iterate through primary spans:

for &sp in span.primary_spans() {

and potentially insert the same label multiple times:

new_labels.push((

without checking if it has already been inserted in the previous iteration of the loop. As a result we may end up with as many duplicates as there are primary spans.

@rustbot claim

removed
needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.
on Oct 17, 2023
gurry

gurry commented on Oct 17, 2023

@gurry
ContributorAuthor

Bisected to nightly-22-06-04

commit[0] 2022-06-02: Auto merge of #97575 - nnethercote:lazify-SourceFile-lines, r=Mark-Simulacrum
commit[1] 2022-06-02: Auto merge of #97598 - spastorino:simplify-universal-impl-trait-lowering, r=cjgillot
commit[2] 2022-06-03: Auto merge of #97667 - matthiaskrgr:rollup-5cfxc85, r=matthiaskrgr
commit[3] 2022-06-03: Auto merge of #97497 - c410-f3r:z-errors, r=petrochenkov
commit[4] 2022-06-03: Auto merge of #96296 - cjgillot:remove-label-lt-shadow, r=petrochenkov
commit[5] 2022-06-03: Auto merge of #97679 - Dylan-DPC:rollup-nswmgmx, r=Dylan-DPC
commit[6] 2022-06-03: Auto merge of #89862 - lcnr:path-generics-diagnostics, r=estebank
commit[7] 2022-06-03: Auto merge of #97694 - Dylan-DPC:rollup-2yxo7ce, r=Dylan-DPC
commit[8] 2022-06-03: Auto merge of #85993 - bjorn3:serde_json, r=wesleywiser
commit[9] 2022-06-03: Auto merge of #95833 - notriddle:notriddle/human-readable-signals, r=yaahc

added a commit that references this issue on Oct 17, 2023

Rollup merge of rust-lang#116838 - gurry:116836-dup-macro-invoc-diag,…

19d100b
added a commit that references this issue on Oct 20, 2023

Auto merge of rust-lang#116838 - gurry:116836-dup-macro-invoc-diag, r…

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

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

    Participants

    @gurry@rustbot@Noratrieb

    Issue actions

      Duplicate `in this macro invocation` labels · Issue #116836 · rust-lang/rust