@@ -5092,11 +5092,14 @@ fn point_at_assoc_type_restriction(
50925092 return ;
50935093 } ;
50945094 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 ( ) {
50965098 let hir:: WherePredicate :: BoundPredicate ( pred) = pred else {
50975099 continue ;
50985100 } ;
5099- for bound in pred. bounds {
5101+ let mut bounds = pred. bounds . iter ( ) . peekable ( ) ;
5102+ while let Some ( bound) = bounds. next ( ) {
51005103 let Some ( trait_ref) = bound. trait_ref ( ) else {
51015104 continue ;
51025105 } ;
@@ -5110,8 +5113,27 @@ fn point_at_assoc_type_restriction(
51105113 && let hir:: QPath :: Resolved ( None , inner_path) = inner_path
51115114 && let Res :: SelfTyAlias { .. } = inner_path. res
51125115 {
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+
51135135 err. span_suggestion_verbose (
5114- pred . span , // FIXME: include the trailing comma.
5136+ span,
51155137 "associated type for the current `impl` cannot be restricted in `where` \
51165138 clauses, remove this bound",
51175139 "" ,
@@ -5160,6 +5182,7 @@ fn point_at_assoc_type_restriction(
51605182 ) ;
51615183 }
51625184 }
5185+ prev = Some ( pred) ;
51635186 }
51645187}
51655188
0 commit comments