@@ -1143,23 +1143,30 @@ pub(crate) fn format_trait(
11431143 rewrite_generics ( context, rewrite_ident ( context, item. ident ) , generics, shape) ?;
11441144 result. push_str ( & generics_str) ;
11451145
1146- // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
1146+ // FIXME(#2055): rustfmt fails to format when there are comments within trait bounds.
11471147 if !generic_bounds. is_empty ( ) {
1148- let ident_hi = context
1149- . snippet_provider
1150- . span_after ( item. span , & item. ident . as_str ( ) ) ;
1148+ let bound_lo = generic_bounds. first ( ) . unwrap ( ) . span ( ) . lo ( ) ;
11511149 let bound_hi = generic_bounds. last ( ) . unwrap ( ) . span ( ) . hi ( ) ;
1152- let snippet = context. snippet ( mk_sp ( ident_hi , bound_hi) ) ;
1150+ let snippet = context. snippet ( mk_sp ( bound_lo , bound_hi) ) ;
11531151 if contains_comment ( snippet) {
11541152 return None ;
11551153 }
11561154
1157- result = rewrite_assign_rhs_with (
1155+ // Rewrite rhs and combine lhs with pre-bound comment
1156+ let ident_hi = context
1157+ . snippet_provider
1158+ . span_after ( item. span , & item. ident . as_str ( ) ) ;
1159+ let ident_hi = context
1160+ . snippet_provider
1161+ . span_after ( mk_sp ( ident_hi, item. span . hi ( ) ) , ":" ) ;
1162+ result = rewrite_assign_rhs_with_comments (
11581163 context,
11591164 result + ":" ,
11601165 generic_bounds,
11611166 shape,
11621167 RhsTactics :: ForceNextLineWithoutIndent ,
1168+ mk_sp ( ident_hi, bound_lo) ,
1169+ true ,
11631170 ) ?;
11641171 }
11651172
@@ -1199,6 +1206,8 @@ pub(crate) fn format_trait(
11991206 }
12001207 let pre_block_span = if !generics. where_clause . predicates . is_empty ( ) {
12011208 mk_sp ( generics. where_clause . span . hi ( ) , item. span . hi ( ) )
1209+ } else if !generic_bounds. is_empty ( ) {
1210+ mk_sp ( generic_bounds. last ( ) . unwrap ( ) . span ( ) . hi ( ) , item. span . hi ( ) )
12021211 } else {
12031212 item. span
12041213 } ;
0 commit comments