@@ -1131,30 +1131,42 @@ pub(crate) fn format_trait(
11311131 ) ;
11321132 result. push_str ( & header) ;
11331133
1134+ // FIXME: rustfmt fails to format when there are comments before the ident.
1135+ if contains_comment ( context. snippet ( mk_sp ( item. span . lo ( ) , generics. span . lo ( ) ) ) ) {
1136+ return None ;
1137+ }
1138+
11341139 let body_lo = context. snippet_provider . span_after ( item. span , "{" ) ;
11351140
11361141 let shape = Shape :: indented ( offset, context. config ) . offset_left ( result. len ( ) ) ?;
11371142 let generics_str =
11381143 rewrite_generics ( context, rewrite_ident ( context, item. ident ) , generics, shape) ?;
11391144 result. push_str ( & generics_str) ;
11401145
1141- // 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.
11421147 if !generic_bounds. is_empty ( ) {
1143- let ident_hi = context
1144- . snippet_provider
1145- . span_after ( item. span , & item. ident . as_str ( ) ) ;
1148+ let bound_lo = generic_bounds. first ( ) . unwrap ( ) . span ( ) . lo ( ) ;
11461149 let bound_hi = generic_bounds. last ( ) . unwrap ( ) . span ( ) . hi ( ) ;
1147- let snippet = context. snippet ( mk_sp ( ident_hi , bound_hi) ) ;
1150+ let snippet = context. snippet ( mk_sp ( bound_lo , bound_hi) ) ;
11481151 if contains_comment ( snippet) {
11491152 return None ;
11501153 }
11511154
1152- 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 (
11531163 context,
11541164 result + ":" ,
11551165 generic_bounds,
11561166 shape,
11571167 RhsTactics :: ForceNextLineWithoutIndent ,
1168+ mk_sp ( ident_hi, bound_lo) ,
1169+ true ,
11581170 ) ?;
11591171 }
11601172
@@ -1194,6 +1206,8 @@ pub(crate) fn format_trait(
11941206 }
11951207 let pre_block_span = if !generics. where_clause . predicates . is_empty ( ) {
11961208 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 ( ) )
11971211 } else {
11981212 item. span
11991213 } ;
0 commit comments