@@ -306,22 +306,20 @@ impl<'a> FnSig<'a> {
306306 defaultness : ast:: Defaultness ,
307307 ) -> FnSig < ' a > {
308308 match * fn_kind {
309- visit:: FnKind :: Fn ( fn_ctxt, _, fn_sig, vis, generics, _) => match fn_ctxt {
310- visit:: FnCtxt :: Assoc ( ..) => {
311- let mut fn_sig = FnSig :: from_method_sig ( fn_sig, generics, vis) ;
312- fn_sig. defaultness = defaultness;
313- fn_sig
314- }
315- _ => FnSig {
316- decl,
317- generics,
318- ext : fn_sig. header . ext ,
319- constness : fn_sig. header . constness ,
320- is_async : Cow :: Borrowed ( & fn_sig. header . asyncness ) ,
321- defaultness,
322- unsafety : fn_sig. header . unsafety ,
323- visibility : vis,
324- } ,
309+ visit:: FnKind :: Fn ( visit:: FnCtxt :: Assoc ( ..) , _, fn_sig, vis, generics, _) => {
310+ let mut fn_sig = FnSig :: from_method_sig ( fn_sig, generics, vis) ;
311+ fn_sig. defaultness = defaultness;
312+ fn_sig
313+ }
314+ visit:: FnKind :: Fn ( _, _, fn_sig, vis, generics, _) => FnSig {
315+ decl,
316+ generics,
317+ ext : fn_sig. header . ext ,
318+ constness : fn_sig. header . constness ,
319+ is_async : Cow :: Borrowed ( & fn_sig. header . asyncness ) ,
320+ defaultness,
321+ unsafety : fn_sig. header . unsafety ,
322+ visibility : vis,
325323 } ,
326324 _ => unreachable ! ( ) ,
327325 }
@@ -1118,172 +1116,172 @@ pub(crate) fn format_trait(
11181116 item : & ast:: Item ,
11191117 offset : Indent ,
11201118) -> Option < String > {
1121- if let ast:: ItemKind :: Trait ( trait_kind) = & item. kind {
1122- let ast:: Trait {
1123- is_auto,
1124- unsafety,
1125- ref generics,
1126- ref bounds,
1127- ref items,
1128- } = * * trait_kind;
1129- let mut result = String :: with_capacity ( 128 ) ;
1130- let header = format ! (
1131- "{}{}{}trait " ,
1132- format_visibility( context, & item. vis) ,
1133- format_unsafety( unsafety) ,
1134- format_auto( is_auto) ,
1135- ) ;
1136- result. push_str ( & header) ;
1119+ let ast:: ItemKind :: Trait ( trait_kind) = & item. kind else {
1120+ unreachable ! ( ) ;
1121+ } ;
1122+ let ast:: Trait {
1123+ is_auto,
1124+ unsafety,
1125+ ref generics,
1126+ ref bounds,
1127+ ref items,
1128+ } = * * trait_kind;
11371129
1138- let body_lo = context. snippet_provider . span_after ( item. span , "{" ) ;
1130+ let mut result = String :: with_capacity ( 128 ) ;
1131+ let header = format ! (
1132+ "{}{}{}trait " ,
1133+ format_visibility( context, & item. vis) ,
1134+ format_unsafety( unsafety) ,
1135+ format_auto( is_auto) ,
1136+ ) ;
1137+ result. push_str ( & header) ;
11391138
1140- let shape = Shape :: indented ( offset, context. config ) . offset_left ( result. len ( ) ) ?;
1141- let generics_str =
1142- rewrite_generics ( context, rewrite_ident ( context, item. ident ) , generics, shape) ?;
1143- result. push_str ( & generics_str) ;
1139+ let body_lo = context. snippet_provider . span_after ( item. span , "{" ) ;
11441140
1145- // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
1146- if !bounds. is_empty ( ) {
1147- let ident_hi = context
1148- . snippet_provider
1149- . span_after ( item. span , item. ident . as_str ( ) ) ;
1150- let bound_hi = bounds. last ( ) . unwrap ( ) . span ( ) . hi ( ) ;
1151- let snippet = context. snippet ( mk_sp ( ident_hi, bound_hi) ) ;
1152- if contains_comment ( snippet) {
1153- return None ;
1154- }
1141+ let shape = Shape :: indented ( offset, context. config ) . offset_left ( result. len ( ) ) ?;
1142+ let generics_str =
1143+ rewrite_generics ( context, rewrite_ident ( context, item. ident ) , generics, shape) ?;
1144+ result. push_str ( & generics_str) ;
11551145
1156- result = rewrite_assign_rhs_with (
1157- context,
1158- result + ":" ,
1159- bounds,
1160- shape,
1161- & RhsAssignKind :: Bounds ,
1162- RhsTactics :: ForceNextLineWithoutIndent ,
1163- ) ?;
1146+ // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
1147+ if !bounds. is_empty ( ) {
1148+ let ident_hi = context
1149+ . snippet_provider
1150+ . span_after ( item. span , item. ident . as_str ( ) ) ;
1151+ let bound_hi = bounds. last ( ) . unwrap ( ) . span ( ) . hi ( ) ;
1152+ let snippet = context. snippet ( mk_sp ( ident_hi, bound_hi) ) ;
1153+ if contains_comment ( snippet) {
1154+ return None ;
11641155 }
11651156
1166- // Rewrite where-clause.
1167- if !generics. where_clause . predicates . is_empty ( ) {
1168- let where_on_new_line = context. config . indent_style ( ) != IndentStyle :: Block ;
1157+ result = rewrite_assign_rhs_with (
1158+ context,
1159+ result + ":" ,
1160+ bounds,
1161+ shape,
1162+ & RhsAssignKind :: Bounds ,
1163+ RhsTactics :: ForceNextLineWithoutIndent ,
1164+ ) ?;
1165+ }
1166+
1167+ // Rewrite where-clause.
1168+ if !generics. where_clause . predicates . is_empty ( ) {
1169+ let where_on_new_line = context. config . indent_style ( ) != IndentStyle :: Block ;
11691170
1170- let where_budget = context. budget ( last_line_width ( & result) ) ;
1171- let pos_before_where = if bounds. is_empty ( ) {
1172- generics. where_clause . span . lo ( )
1171+ let where_budget = context. budget ( last_line_width ( & result) ) ;
1172+ let pos_before_where = if bounds. is_empty ( ) {
1173+ generics. where_clause . span . lo ( )
1174+ } else {
1175+ bounds[ bounds. len ( ) - 1 ] . span ( ) . hi ( )
1176+ } ;
1177+ let option = WhereClauseOption :: snuggled ( & generics_str) ;
1178+ let where_clause_str = rewrite_where_clause (
1179+ context,
1180+ & generics. where_clause . predicates ,
1181+ generics. where_clause . span ,
1182+ context. config . brace_style ( ) ,
1183+ Shape :: legacy ( where_budget, offset. block_only ( ) ) ,
1184+ where_on_new_line,
1185+ "{" ,
1186+ None ,
1187+ pos_before_where,
1188+ option,
1189+ ) ?;
1190+ // If the where-clause cannot fit on the same line,
1191+ // put the where-clause on a new line
1192+ if !where_clause_str. contains ( '\n' )
1193+ && last_line_width ( & result) + where_clause_str. len ( ) + offset. width ( )
1194+ > context. config . comment_width ( )
1195+ {
1196+ let width = offset. block_indent + context. config . tab_spaces ( ) - 1 ;
1197+ let where_indent = Indent :: new ( 0 , width) ;
1198+ result. push_str ( & where_indent. to_string_with_newline ( context. config ) ) ;
1199+ }
1200+ result. push_str ( & where_clause_str) ;
1201+ } else {
1202+ let item_snippet = context. snippet ( item. span ) ;
1203+ if let Some ( lo) = item_snippet. find ( '/' ) {
1204+ // 1 = `{`
1205+ let comment_hi = if generics. params . len ( ) > 0 {
1206+ generics. span . lo ( ) - BytePos ( 1 )
11731207 } else {
1174- bounds [ bounds . len ( ) - 1 ] . span ( ) . hi ( )
1208+ body_lo - BytePos ( 1 )
11751209 } ;
1176- let option = WhereClauseOption :: snuggled ( & generics_str) ;
1177- let where_clause_str = rewrite_where_clause (
1178- context,
1179- & generics. where_clause . predicates ,
1180- generics. where_clause . span ,
1181- context. config . brace_style ( ) ,
1182- Shape :: legacy ( where_budget, offset. block_only ( ) ) ,
1183- where_on_new_line,
1184- "{" ,
1185- None ,
1186- pos_before_where,
1187- option,
1188- ) ?;
1189- // If the where-clause cannot fit on the same line,
1190- // put the where-clause on a new line
1191- if !where_clause_str. contains ( '\n' )
1192- && last_line_width ( & result) + where_clause_str. len ( ) + offset. width ( )
1193- > context. config . comment_width ( )
1194- {
1195- let width = offset. block_indent + context. config . tab_spaces ( ) - 1 ;
1196- let where_indent = Indent :: new ( 0 , width) ;
1197- result. push_str ( & where_indent. to_string_with_newline ( context. config ) ) ;
1198- }
1199- result. push_str ( & where_clause_str) ;
1200- } else {
1201- let item_snippet = context. snippet ( item. span ) ;
1202- if let Some ( lo) = item_snippet. find ( '/' ) {
1203- // 1 = `{`
1204- let comment_hi = if generics. params . len ( ) > 0 {
1205- generics. span . lo ( ) - BytePos ( 1 )
1206- } else {
1207- body_lo - BytePos ( 1 )
1208- } ;
1209- let comment_lo = item. span . lo ( ) + BytePos ( lo as u32 ) ;
1210- if comment_lo < comment_hi {
1211- match recover_missing_comment_in_span (
1212- mk_sp ( comment_lo, comment_hi) ,
1213- Shape :: indented ( offset, context. config ) ,
1214- context,
1215- last_line_width ( & result) ,
1216- ) {
1217- Some ( ref missing_comment) if !missing_comment. is_empty ( ) => {
1218- result. push_str ( missing_comment) ;
1219- }
1220- _ => ( ) ,
1210+ let comment_lo = item. span . lo ( ) + BytePos ( lo as u32 ) ;
1211+ if comment_lo < comment_hi {
1212+ match recover_missing_comment_in_span (
1213+ mk_sp ( comment_lo, comment_hi) ,
1214+ Shape :: indented ( offset, context. config ) ,
1215+ context,
1216+ last_line_width ( & result) ,
1217+ ) {
1218+ Some ( ref missing_comment) if !missing_comment. is_empty ( ) => {
1219+ result. push_str ( missing_comment) ;
12211220 }
1221+ _ => ( ) ,
12221222 }
12231223 }
12241224 }
1225+ }
12251226
1226- let block_span = mk_sp ( generics. where_clause . span . hi ( ) , item. span . hi ( ) ) ;
1227- let snippet = context. snippet ( block_span) ;
1228- let open_pos = snippet. find_uncommented ( "{" ) ? + 1 ;
1227+ let block_span = mk_sp ( generics. where_clause . span . hi ( ) , item. span . hi ( ) ) ;
1228+ let snippet = context. snippet ( block_span) ;
1229+ let open_pos = snippet. find_uncommented ( "{" ) ? + 1 ;
12291230
1230- match context. config . brace_style ( ) {
1231- _ if last_line_contains_single_line_comment ( & result)
1232- || last_line_width ( & result) + 2 > context. budget ( offset. width ( ) ) =>
1233- {
1234- result. push_str ( & offset. to_string_with_newline ( context. config ) ) ;
1235- }
1236- _ if context. config . empty_item_single_line ( )
1237- && items. is_empty ( )
1238- && !result. contains ( '\n' )
1239- && !contains_comment ( & snippet[ open_pos..] ) =>
1231+ match context. config . brace_style ( ) {
1232+ _ if last_line_contains_single_line_comment ( & result)
1233+ || last_line_width ( & result) + 2 > context. budget ( offset. width ( ) ) =>
1234+ {
1235+ result. push_str ( & offset. to_string_with_newline ( context. config ) ) ;
1236+ }
1237+ _ if context. config . empty_item_single_line ( )
1238+ && items. is_empty ( )
1239+ && !result. contains ( '\n' )
1240+ && !contains_comment ( & snippet[ open_pos..] ) =>
1241+ {
1242+ result. push_str ( " {}" ) ;
1243+ return Some ( result) ;
1244+ }
1245+ BraceStyle :: AlwaysNextLine => {
1246+ result. push_str ( & offset. to_string_with_newline ( context. config ) ) ;
1247+ }
1248+ BraceStyle :: PreferSameLine => result. push ( ' ' ) ,
1249+ BraceStyle :: SameLineWhere => {
1250+ if result. contains ( '\n' )
1251+ || ( !generics. where_clause . predicates . is_empty ( ) && !items. is_empty ( ) )
12401252 {
1241- result. push_str ( " {}" ) ;
1242- return Some ( result) ;
1243- }
1244- BraceStyle :: AlwaysNextLine => {
12451253 result. push_str ( & offset. to_string_with_newline ( context. config ) ) ;
1246- }
1247- BraceStyle :: PreferSameLine => result. push ( ' ' ) ,
1248- BraceStyle :: SameLineWhere => {
1249- if result. contains ( '\n' )
1250- || ( !generics. where_clause . predicates . is_empty ( ) && !items. is_empty ( ) )
1251- {
1252- result. push_str ( & offset. to_string_with_newline ( context. config ) ) ;
1253- } else {
1254- result. push ( ' ' ) ;
1255- }
1254+ } else {
1255+ result. push ( ' ' ) ;
12561256 }
12571257 }
1258- result. push ( '{' ) ;
1259-
1260- let outer_indent_str = offset. block_only ( ) . to_string_with_newline ( context. config ) ;
1258+ }
1259+ result. push ( '{' ) ;
12611260
1262- if !items. is_empty ( ) || contains_comment ( & snippet[ open_pos..] ) {
1263- let mut visitor = FmtVisitor :: from_context ( context) ;
1264- visitor. block_indent = offset. block_only ( ) . block_indent ( context. config ) ;
1265- visitor. last_pos = block_span. lo ( ) + BytePos ( open_pos as u32 ) ;
1261+ let outer_indent_str = offset. block_only ( ) . to_string_with_newline ( context. config ) ;
12661262
1267- for item in items {
1268- visitor. visit_trait_item ( item) ;
1269- }
1263+ if !items. is_empty ( ) || contains_comment ( & snippet[ open_pos..] ) {
1264+ let mut visitor = FmtVisitor :: from_context ( context) ;
1265+ visitor. block_indent = offset. block_only ( ) . block_indent ( context. config ) ;
1266+ visitor. last_pos = block_span. lo ( ) + BytePos ( open_pos as u32 ) ;
12701267
1271- visitor. format_missing ( item. span . hi ( ) - BytePos ( 1 ) ) ;
1268+ for item in items {
1269+ visitor. visit_trait_item ( item) ;
1270+ }
12721271
1273- let inner_indent_str = visitor. block_indent . to_string_with_newline ( context . config ) ;
1272+ visitor. format_missing ( item . span . hi ( ) - BytePos ( 1 ) ) ;
12741273
1275- result. push_str ( & inner_indent_str) ;
1276- result. push_str ( visitor. buffer . trim ( ) ) ;
1277- result. push_str ( & outer_indent_str) ;
1278- } else if result. contains ( '\n' ) {
1279- result. push_str ( & outer_indent_str) ;
1280- }
1274+ let inner_indent_str = visitor. block_indent . to_string_with_newline ( context. config ) ;
12811275
1282- result. push ( '}' ) ;
1283- Some ( result)
1284- } else {
1285- unreachable ! ( ) ;
1276+ result. push_str ( & inner_indent_str) ;
1277+ result. push_str ( visitor. buffer . trim ( ) ) ;
1278+ result. push_str ( & outer_indent_str) ;
1279+ } else if result. contains ( '\n' ) {
1280+ result. push_str ( & outer_indent_str) ;
12861281 }
1282+
1283+ result. push ( '}' ) ;
1284+ Some ( result)
12871285}
12881286
12891287pub ( crate ) struct TraitAliasBounds < ' a > {
0 commit comments