Skip to content

Commit

Permalink
Account for trailing comma in removal suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Jan 4, 2024
1 parent ba8be06 commit 48bcad6
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5047,11 +5047,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;
};
Expand All @@ -5065,8 +5068,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",
"",
Expand Down Expand Up @@ -5115,6 +5137,7 @@ fn point_at_assoc_type_restriction(
);
}
}
prev = Some(pred);
}
}

Expand Down
1 change: 0 additions & 1 deletion tests/ui/associated-types/impl-wf-cycle-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/associated-types/impl-wf-cycle-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions tests/ui/associated-types/impl-wf-cycle-5.fixed
Original file line number Diff line number Diff line change
@@ -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 = ();
}
32 changes: 32 additions & 0 deletions tests/ui/associated-types/impl-wf-cycle-5.rs
Original file line number Diff line number Diff line change
@@ -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 = ();
}
31 changes: 31 additions & 0 deletions tests/ui/associated-types/impl-wf-cycle-5.stderr
Original file line number Diff line number Diff line change
@@ -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`.
31 changes: 31 additions & 0 deletions tests/ui/associated-types/impl-wf-cycle-6.fixed
Original file line number Diff line number Diff line change
@@ -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 = ();
}
31 changes: 31 additions & 0 deletions tests/ui/associated-types/impl-wf-cycle-6.rs
Original file line number Diff line number Diff line change
@@ -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 = ();
}
29 changes: 29 additions & 0 deletions tests/ui/associated-types/impl-wf-cycle-6.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 48bcad6

Please sign in to comment.