@@ -5100,11 +5100,14 @@ fn point_at_assoc_type_restriction(
51005100 return ;
51015101 } ;
51025102 let name = tcx. item_name ( proj. projection_ty . def_id ) ;
5103- for pred in generics. predicates {
5103+ let mut predicates = generics. predicates . iter ( ) . peekable ( ) ;
5104+ let mut prev: Option < & hir:: WhereBoundPredicate < ' _ > > = None ;
5105+ while let Some ( pred) = predicates. next ( ) {
51045106 let hir:: WherePredicate :: BoundPredicate ( pred) = pred else {
51055107 continue ;
51065108 } ;
5107- for bound in pred. bounds {
5109+ let mut bounds = pred. bounds . iter ( ) . peekable ( ) ;
5110+ while let Some ( bound) = bounds. next ( ) {
51085111 let Some ( trait_ref) = bound. trait_ref ( ) else {
51095112 continue ;
51105113 } ;
@@ -5118,8 +5121,27 @@ fn point_at_assoc_type_restriction(
51185121 && let hir:: QPath :: Resolved ( None , inner_path) = inner_path
51195122 && let Res :: SelfTyAlias { .. } = inner_path. res
51205123 {
5124+ // The following block is to determine the right span to delete for this bound
5125+ // that will leave valid code after the suggestion is applied.
5126+ let span = if let Some ( hir:: WherePredicate :: BoundPredicate ( next) ) =
5127+ predicates. peek ( )
5128+ && pred. origin == next. origin
5129+ {
5130+ // There's another bound, include the comma for the current one.
5131+ pred. span . until ( next. span )
5132+ } else if let Some ( prev) = prev
5133+ && pred. origin == prev. origin
5134+ {
5135+ // Last bound, try to remove the previous comma.
5136+ prev. span . shrink_to_hi ( ) . to ( pred. span )
5137+ } else if pred. origin == hir:: PredicateOrigin :: WhereClause {
5138+ pred. span . with_hi ( generics. where_clause_span . hi ( ) )
5139+ } else {
5140+ pred. span
5141+ } ;
5142+
51215143 err. span_suggestion_verbose (
5122- pred . span , // FIXME: include the trailing comma.
5144+ span,
51235145 "associated type for the current `impl` cannot be restricted in `where` \
51245146 clauses, remove this bound",
51255147 "" ,
@@ -5168,6 +5190,7 @@ fn point_at_assoc_type_restriction(
51685190 ) ;
51695191 }
51705192 }
5193+ prev = Some ( pred) ;
51715194 }
51725195}
51735196
0 commit comments