Account for trailing comma in removal suggestion

This commit is contained in:
Esteban Küber 2023-12-28 22:26:37 +00:00
parent 2c2f3ed2c4
commit c85bb274f6
9 changed files with 212 additions and 5 deletions

View file

@ -5100,11 +5100,14 @@ fn point_at_assoc_type_restriction(
return;
};
let name = tcx.item_name(proj.projection_ty.def_id);
for pred in generics.predicates {
let mut predicates = generics.predicates.iter().peekable();
let mut prev: Option<&hir::WhereBoundPredicate<'_>> = None;
while let Some(pred) = predicates.next() {
let hir::WherePredicate::BoundPredicate(pred) = pred else {
continue;
};
for bound in pred.bounds {
let mut bounds = pred.bounds.iter().peekable();
while let Some(bound) = bounds.next() {
let Some(trait_ref) = bound.trait_ref() else {
continue;
};
@ -5118,8 +5121,27 @@ fn point_at_assoc_type_restriction(
&& let hir::QPath::Resolved(None, inner_path) = inner_path
&& let Res::SelfTyAlias { .. } = inner_path.res
{
// The following block is to determine the right span to delete for this bound
// that will leave valid code after the suggestion is applied.
let span = if let Some(hir::WherePredicate::BoundPredicate(next)) =
predicates.peek()
&& pred.origin == next.origin
{
// There's another bound, include the comma for the current one.
pred.span.until(next.span)
} else if let Some(prev) = prev
&& pred.origin == prev.origin
{
// Last bound, try to remove the previous comma.
prev.span.shrink_to_hi().to(pred.span)
} else if pred.origin == hir::PredicateOrigin::WhereClause {
pred.span.with_hi(generics.where_clause_span.hi())
} else {
pred.span
};
err.span_suggestion_verbose(
pred.span, // FIXME: include the trailing comma.
span,
"associated type for the current `impl` cannot be restricted in `where` \
clauses, remove this bound",
"",
@ -5168,6 +5190,7 @@ fn point_at_assoc_type_restriction(
);
}
}
prev = Some(pred);
}
}

View file

@ -24,7 +24,6 @@ LL | Self::A: Baz,
help: associated type for the current `impl` cannot be restricted in `where` clauses, remove this bound
|
LL - Self::A: Baz,
LL + ,
|
error: aborting due to 1 previous error

View file

@ -21,7 +21,7 @@ LL | Self::A: Copy,
help: associated type for the current `impl` cannot be restricted in `where` clauses, remove this bound
|
LL - Self::A: Copy,
LL + ,
LL +
|
error: aborting due to 1 previous error

View file

@ -0,0 +1,31 @@
// run-rustfix
trait Baz {}
impl Baz for () {}
impl<T> Baz for (T,) {}
trait Fiz {}
impl Fiz for bool {}
trait Grault {
type A;
type B;
}
impl Grault for () {
type A = ();
type B = bool;
}
impl<T> Grault for (T,)
//~^ ERROR overflow evaluating the requirement `<(T,) as Grault>::A == _`
where
T: Grault,
{
type A = ();
type B = bool;
}
fn main() {
let _: <((),) as Grault>::A = ();
}

View file

@ -0,0 +1,32 @@
// run-rustfix
trait Baz {}
impl Baz for () {}
impl<T> Baz for (T,) {}
trait Fiz {}
impl Fiz for bool {}
trait Grault {
type A;
type B;
}
impl Grault for () {
type A = ();
type B = bool;
}
impl<T> Grault for (T,)
//~^ ERROR overflow evaluating the requirement `<(T,) as Grault>::A == _`
where
T: Grault,
Self::A: Baz,
{
type A = ();
type B = bool;
}
fn main() {
let _: <((),) as Grault>::A = ();
}

View file

@ -0,0 +1,31 @@
error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
--> $DIR/impl-wf-cycle-5.rs:20:1
|
LL | / impl<T> Grault for (T,)
LL | |
LL | | where
LL | | T: Grault,
LL | | Self::A: Baz,
| |_________________^
LL | {
LL | type A = ();
| ------ associated type `<(T,) as Grault>::A` is specified here
|
note: required for `(T,)` to implement `Grault`
--> $DIR/impl-wf-cycle-5.rs:20:9
|
LL | impl<T> Grault for (T,)
| ^^^^^^ ^^^^
...
LL | Self::A: Baz,
| --- unsatisfied trait bound introduced here
help: associated type for the current `impl` cannot be restricted in `where` clauses, remove this bound
|
LL - T: Grault,
LL - Self::A: Baz,
LL + T: Grault,
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0275`.

View file

@ -0,0 +1,31 @@
// run-rustfix
trait Baz {}
impl Baz for () {}
impl<T> Baz for (T,) {}
trait Fiz {}
impl Fiz for bool {}
trait Grault {
type A;
type B;
}
impl Grault for () {
type A = ();
type B = bool;
}
impl<T: Grault> Grault for (T,)
//~^ ERROR overflow evaluating the requirement `<(T,) as Grault>::A == _`
where
{
type A = ();
type B = bool;
}
fn main() {
let _: <((),) as Grault>::A = ();
}

View file

@ -0,0 +1,31 @@
// run-rustfix
trait Baz {}
impl Baz for () {}
impl<T> Baz for (T,) {}
trait Fiz {}
impl Fiz for bool {}
trait Grault {
type A;
type B;
}
impl Grault for () {
type A = ();
type B = bool;
}
impl<T: Grault> Grault for (T,)
//~^ ERROR overflow evaluating the requirement `<(T,) as Grault>::A == _`
where
Self::A: Baz,
{
type A = ();
type B = bool;
}
fn main() {
let _: <((),) as Grault>::A = ();
}

View file

@ -0,0 +1,29 @@
error[E0275]: overflow evaluating the requirement `<(T,) as Grault>::A == _`
--> $DIR/impl-wf-cycle-6.rs:20:1
|
LL | / impl<T: Grault> Grault for (T,)
LL | |
LL | | where
LL | | Self::A: Baz,
| |_________________^
LL | {
LL | type A = ();
| ------ associated type `<(T,) as Grault>::A` is specified here
|
note: required for `(T,)` to implement `Grault`
--> $DIR/impl-wf-cycle-6.rs:20:17
|
LL | impl<T: Grault> Grault for (T,)
| ^^^^^^ ^^^^
...
LL | Self::A: Baz,
| --- unsatisfied trait bound introduced here
help: associated type for the current `impl` cannot be restricted in `where` clauses, remove this bound
|
LL - Self::A: Baz,
LL +
|
error: aborting due to 1 previous error
For more information about this error, try `rustc --explain E0275`.