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

dedup for duplicate suggestions #118057

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions compiler/rustc_errors/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -622,17 +622,18 @@ impl Diagnostic {
pub fn multipart_suggestion_with_style(
&mut self,
msg: impl Into<SubdiagnosticMessage>,
suggestion: Vec<(Span, String)>,
mut suggestion: Vec<(Span, String)>,
applicability: Applicability,
style: SuggestionStyle,
) -> &mut Self {
let mut parts = suggestion
suggestion.sort_unstable();
suggestion.dedup();

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we dedup after sorting? In case we have [(first span, first stuff), (second span, second stuff), (first span, first stuff)].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great tips! Before this, I always assumed that Vec::dedup was equivalent to Vec::from(HashSet::from(vector)).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not sort suggestions twice. They are already sorted by span a few lines below.

let parts = suggestion
.into_iter()
.map(|(span, snippet)| SubstitutionPart { snippet, span })
.collect::<Vec<_>>();

parts.sort_unstable_by_key(|part| part.span);

assert!(!parts.is_empty());
debug_assert_eq!(
parts.iter().find(|part| part.span.is_empty() && part.snippet.is_empty()),
Expand Down
10 changes: 10 additions & 0 deletions tests/ui/macros/issue-118048.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
macro_rules! foo {
($ty:ty) => {
fn foo(_: $ty, _: $ty) {}
}
}

foo!(_);
//~^ ERROR the placeholder `_` is not allowed within types on item signatures for functions

fn main() {}
21 changes: 21 additions & 0 deletions tests/ui/macros/issue-118048.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
error[E0121]: the placeholder `_` is not allowed within types on item signatures for functions
--> $DIR/issue-118048.rs:7:6
|
LL | foo!(_);
| ^
| |
| not allowed in type signatures
| not allowed in type signatures
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth noting that for this case, there's a duplicate label generated by placeholder_types. As we did previously, the method to eliminate the duplication is to add placeholder_types.dedup().

|
help: use type parameters instead
|
LL ~ fn foo<T>(_: $ty, _: $ty) {}
LL | }
LL | }
LL |
LL ~ foo!(T);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously it displayed here as foo!(TT).

|

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0121`.
Loading