@@ -5047,11 +5047,14 @@ fn point_at_assoc_type_restriction(
50475047 return ;
50485048 } ;
50495049 let name = tcx. item_name ( proj. projection_ty . def_id ) ;
5050- for pred in generics. predicates {
5050+ let mut predicates = generics. predicates . iter ( ) . peekable ( ) ;
5051+ let mut prev: Option < & hir:: WhereBoundPredicate < ' _ > > = None ;
5052+ while let Some ( pred) = predicates. next ( ) {
50515053 let hir:: WherePredicate :: BoundPredicate ( pred) = pred else {
50525054 continue ;
50535055 } ;
5054- for bound in pred. bounds {
5056+ let mut bounds = pred. bounds . iter ( ) . peekable ( ) ;
5057+ while let Some ( bound) = bounds. next ( ) {
50555058 let Some ( trait_ref) = bound. trait_ref ( ) else {
50565059 continue ;
50575060 } ;
@@ -5065,8 +5068,27 @@ fn point_at_assoc_type_restriction(
50655068 && let hir:: QPath :: Resolved ( None , inner_path) = inner_path
50665069 && let Res :: SelfTyAlias { .. } = inner_path. res
50675070 {
5071+ // The following block is to determine the right span to delete for this bound
5072+ // that will leave valid code after the suggestion is applied.
5073+ let span = if let Some ( hir:: WherePredicate :: BoundPredicate ( next) ) =
5074+ predicates. peek ( )
5075+ && pred. origin == next. origin
5076+ {
5077+ // There's another bound, include the comma for the current one.
5078+ pred. span . until ( next. span )
5079+ } else if let Some ( prev) = prev
5080+ && pred. origin == prev. origin
5081+ {
5082+ // Last bound, try to remove the previous comma.
5083+ prev. span . shrink_to_hi ( ) . to ( pred. span )
5084+ } else if pred. origin == hir:: PredicateOrigin :: WhereClause {
5085+ pred. span . with_hi ( generics. where_clause_span . hi ( ) )
5086+ } else {
5087+ pred. span
5088+ } ;
5089+
50685090 err. span_suggestion_verbose (
5069- pred . span , // FIXME: include the trailing comma.
5091+ span,
50705092 "associated type for the current `impl` cannot be restricted in `where` \
50715093 clauses, remove this bound",
50725094 "" ,
@@ -5115,6 +5137,7 @@ fn point_at_assoc_type_restriction(
51155137 ) ;
51165138 }
51175139 }
5140+ prev = Some ( pred) ;
51185141 }
51195142}
51205143
0 commit comments