Rollup merge of #127717 - gurry:127441-stray-impl-sugg, r=compiler-errors
Fix malformed suggestion for repeated maybe unsized bounds Fixes #127441 Now when we encounter something like `foo(a : impl ?Sized + ?Sized)`, instead of suggesting removal of both bounds and leaving `foo(a: impl )` behind, we suggest changing the first bound to `Sized` and removing the second bound, resulting in `foo(a: impl Sized)`. Although the issue was reported for impl trait types, it also occurred with regular param bounds. So if we encounter `foo<T: ?Sized + ?Sized>(a: T)` we now detect that all the bounds are `?Sized` and therefore emit the suggestion to remove the entire predicate `: ?Sized + ?Sized` resulting in `foo<T>(a: T)`. Lastly, if we encounter a situation where some of the bounds are something other than `?Sized`, then we emit separate removal suggestions for each `?Sized` bound. E.g. if we see `foo(a: impl ?Sized + Bar + ?Sized)` or `foo<T: ?Sized + Bar + ?Sized>(a: T)` we emit suggestions such that the user will be left with `foo(a : impl Bar)` or `foo<T: Bar>(a: T)` respectively.
This commit is contained in:
commit
2ff33bb1df
4 changed files with 295 additions and 29 deletions
|
@ -763,7 +763,7 @@ impl<'hir> Generics<'hir> {
|
|||
)
|
||||
}
|
||||
|
||||
fn span_for_predicate_removal(&self, pos: usize) -> Span {
|
||||
pub fn span_for_predicate_removal(&self, pos: usize) -> Span {
|
||||
let predicate = &self.predicates[pos];
|
||||
let span = predicate.span();
|
||||
|
||||
|
@ -806,15 +806,21 @@ impl<'hir> Generics<'hir> {
|
|||
return self.span_for_predicate_removal(predicate_pos);
|
||||
}
|
||||
|
||||
let span = bounds[bound_pos].span();
|
||||
if bound_pos == 0 {
|
||||
// where T: ?Sized + Bar, Foo: Bar,
|
||||
// ^^^^^^^^^
|
||||
span.to(bounds[1].span().shrink_to_lo())
|
||||
let bound_span = bounds[bound_pos].span();
|
||||
if bound_pos < bounds.len() - 1 {
|
||||
// If there's another bound after the current bound
|
||||
// include the following '+' e.g.:
|
||||
//
|
||||
// `T: Foo + CurrentBound + Bar`
|
||||
// ^^^^^^^^^^^^^^^
|
||||
bound_span.to(bounds[bound_pos + 1].span().shrink_to_lo())
|
||||
} else {
|
||||
// where T: Bar + ?Sized, Foo: Bar,
|
||||
// ^^^^^^^^^
|
||||
bounds[bound_pos - 1].span().shrink_to_hi().to(span)
|
||||
// If the current bound is the last bound
|
||||
// include the preceding '+' E.g.:
|
||||
//
|
||||
// `T: Foo + Bar + CurrentBound`
|
||||
// ^^^^^^^^^^^^^^^
|
||||
bound_span.with_lo(bounds[bound_pos - 1].span().hi())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue