Skip to content

Commit 29bdf9e

Browse files
committed
Account for single where bound being removed
1 parent c85bb27 commit 29bdf9e

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -5123,8 +5123,24 @@ fn point_at_assoc_type_restriction(
51235123
{
51245124
// The following block is to determine the right span to delete for this bound
51255125
// that will leave valid code after the suggestion is applied.
5126-
let span = if let Some(hir::WherePredicate::BoundPredicate(next)) =
5127-
predicates.peek()
5126+
let span = if pred.origin == hir::PredicateOrigin::WhereClause
5127+
&& generics
5128+
.predicates
5129+
.iter()
5130+
.filter(|p| {
5131+
matches!(
5132+
p,
5133+
hir::WherePredicate::BoundPredicate(p)
5134+
if hir::PredicateOrigin::WhereClause == p.origin
5135+
)
5136+
})
5137+
.count()
5138+
== 1
5139+
{
5140+
// There's only one `where` bound, that needs to be removed. Remove the whole
5141+
// `where` clause.
5142+
generics.where_clause_span
5143+
} else if let Some(hir::WherePredicate::BoundPredicate(next)) = predicates.peek()
51285144
&& pred.origin == next.origin
51295145
{
51305146
// There's another bound, include the comma for the current one.
@@ -5174,7 +5190,7 @@ fn point_at_assoc_type_restriction(
51745190
err.span_suggestion_verbose(
51755191
path.span,
51765192
"replace the associated type with the type specified in this `impl`",
5177-
format!("{}", tcx.type_of(new.def_id).skip_binder(),),
5193+
tcx.type_of(new.def_id).skip_binder().to_string(),
51785194
Applicability::MachineApplicable,
51795195
);
51805196
}

tests/ui/associated-types/impl-wf-cycle-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ LL | Self::A: Copy,
2020
| ---- unsatisfied trait bound introduced here
2121
help: associated type for the current `impl` cannot be restricted in `where` clauses, remove this bound
2222
|
23+
LL - where
2324
LL - Self::A: Copy,
24-
LL +
2525
|
2626

2727
error: aborting due to 1 previous error

tests/ui/associated-types/impl-wf-cycle-6.fixed

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ impl Grault for () {
1919

2020
impl<T: Grault> Grault for (T,)
2121
//~^ ERROR overflow evaluating the requirement `<(T,) as Grault>::A == _`
22-
where
23-
22+
2423
{
2524
type A = ();
2625
type B = bool;

tests/ui/associated-types/impl-wf-cycle-6.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ LL | Self::A: Baz,
2020
| --- unsatisfied trait bound introduced here
2121
help: associated type for the current `impl` cannot be restricted in `where` clauses, remove this bound
2222
|
23+
LL - where
2324
LL - Self::A: Baz,
24-
LL +
2525
|
2626

2727
error: aborting due to 1 previous error

0 commit comments

Comments
 (0)