Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate in this macro invocation labels #116836

Closed
gurry opened this issue Oct 17, 2023 · 2 comments · Fixed by #116838
Closed

Duplicate in this macro invocation labels #116836

gurry opened this issue Oct 17, 2023 · 2 comments · Fixed by #116838
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@gurry
Copy link
Contributor

gurry commented Oct 17, 2023

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

@gurry gurry added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 17, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 17, 2023
@gurry
Copy link
Contributor Author

gurry commented Oct 17, 2023

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

@Noratrieb Noratrieb removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 17, 2023
@gurry
Copy link
Contributor Author

gurry commented Oct 17, 2023

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

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 17, 2023
… r=petrochenkov

Fix duplicate labels emitted in `render_multispan_macro_backtrace()`

This PR replaces the `Vec` used to store labels with an `FxIndexSet` in order to eliminate duplicates

Fixes rust-lang#116836
@bors bors closed this as completed in ae466d2 Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants