@@ -5092,11 +5092,14 @@ fn point_at_assoc_type_restriction(
5092
5092
return ;
5093
5093
} ;
5094
5094
let name = tcx. item_name ( proj. projection_ty . def_id ) ;
5095
- for pred in generics. predicates {
5095
+ let mut predicates = generics. predicates . iter ( ) . peekable ( ) ;
5096
+ let mut prev: Option < & hir:: WhereBoundPredicate < ' _ > > = None ;
5097
+ while let Some ( pred) = predicates. next ( ) {
5096
5098
let hir:: WherePredicate :: BoundPredicate ( pred) = pred else {
5097
5099
continue ;
5098
5100
} ;
5099
- for bound in pred. bounds {
5101
+ let mut bounds = pred. bounds . iter ( ) . peekable ( ) ;
5102
+ while let Some ( bound) = bounds. next ( ) {
5100
5103
let Some ( trait_ref) = bound. trait_ref ( ) else {
5101
5104
continue ;
5102
5105
} ;
@@ -5110,8 +5113,27 @@ fn point_at_assoc_type_restriction(
5110
5113
&& let hir:: QPath :: Resolved ( None , inner_path) = inner_path
5111
5114
&& let Res :: SelfTyAlias { .. } = inner_path. res
5112
5115
{
5116
+ // The following block is to determine the right span to delete for this bound
5117
+ // that will leave valid code after the suggestion is applied.
5118
+ let span = if let Some ( hir:: WherePredicate :: BoundPredicate ( next) ) =
5119
+ predicates. peek ( )
5120
+ && pred. origin == next. origin
5121
+ {
5122
+ // There's another bound, include the comma for the current one.
5123
+ pred. span . until ( next. span )
5124
+ } else if let Some ( prev) = prev
5125
+ && pred. origin == prev. origin
5126
+ {
5127
+ // Last bound, try to remove the previous comma.
5128
+ prev. span . shrink_to_hi ( ) . to ( pred. span )
5129
+ } else if pred. origin == hir:: PredicateOrigin :: WhereClause {
5130
+ pred. span . with_hi ( generics. where_clause_span . hi ( ) )
5131
+ } else {
5132
+ pred. span
5133
+ } ;
5134
+
5113
5135
err. span_suggestion_verbose (
5114
- pred . span , // FIXME: include the trailing comma.
5136
+ span,
5115
5137
"associated type for the current `impl` cannot be restricted in `where` \
5116
5138
clauses, remove this bound",
5117
5139
"" ,
@@ -5160,6 +5182,7 @@ fn point_at_assoc_type_restriction(
5160
5182
) ;
5161
5183
}
5162
5184
}
5185
+ prev = Some ( pred) ;
5163
5186
}
5164
5187
}
5165
5188
0 commit comments