@@ -1136,30 +1136,42 @@ pub(crate) fn format_trait(
11361136 ) ;
11371137 result. push_str ( & header) ;
11381138
1139+ // FIXME: rustfmt fails to format when there are comments before the ident.
1140+ if contains_comment ( context. snippet ( mk_sp ( item. span . lo ( ) , generics. span . lo ( ) ) ) ) {
1141+ return None ;
1142+ }
1143+
11391144 let body_lo = context. snippet_provider . span_after ( item. span , "{" ) ;
11401145
11411146 let shape = Shape :: indented ( offset, context. config ) . offset_left ( result. len ( ) ) ?;
11421147 let generics_str =
11431148 rewrite_generics ( context, rewrite_ident ( context, item. ident ) , generics, shape) ?;
11441149 result. push_str ( & generics_str) ;
11451150
1146- // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
1151+ // FIXME(#2055): rustfmt fails to format when there are comments within trait bounds.
11471152 if !generic_bounds. is_empty ( ) {
1148- let ident_hi = context
1149- . snippet_provider
1150- . span_after ( item. span , & item. ident . as_str ( ) ) ;
1153+ let bound_lo = generic_bounds. first ( ) . unwrap ( ) . span ( ) . lo ( ) ;
11511154 let bound_hi = generic_bounds. last ( ) . unwrap ( ) . span ( ) . hi ( ) ;
1152- let snippet = context. snippet ( mk_sp ( ident_hi , bound_hi) ) ;
1155+ let snippet = context. snippet ( mk_sp ( bound_lo , bound_hi) ) ;
11531156 if contains_comment ( snippet) {
11541157 return None ;
11551158 }
11561159
1157- result = rewrite_assign_rhs_with (
1160+ // Rewrite rhs and combine lhs with pre-bound comment
1161+ let ident_hi = context
1162+ . snippet_provider
1163+ . span_after ( item. span , & item. ident . as_str ( ) ) ;
1164+ let ident_hi = context
1165+ . snippet_provider
1166+ . span_after ( mk_sp ( ident_hi, item. span . hi ( ) ) , ":" ) ;
1167+ result = rewrite_assign_rhs_with_comments (
11581168 context,
11591169 result + ":" ,
11601170 generic_bounds,
11611171 shape,
11621172 RhsTactics :: ForceNextLineWithoutIndent ,
1173+ mk_sp ( ident_hi, bound_lo) ,
1174+ true ,
11631175 ) ?;
11641176 }
11651177
@@ -1199,6 +1211,8 @@ pub(crate) fn format_trait(
11991211 }
12001212 let pre_block_span = if !generics. where_clause . predicates . is_empty ( ) {
12011213 mk_sp ( generics. where_clause . span . hi ( ) , item. span . hi ( ) )
1214+ } else if !generic_bounds. is_empty ( ) {
1215+ mk_sp ( generic_bounds. last ( ) . unwrap ( ) . span ( ) . hi ( ) , item. span . hi ( ) )
12021216 } else {
12031217 item. span
12041218 } ;
0 commit comments