@@ -267,6 +267,8 @@ crate fn print_where_clause<'a, 'tcx: 'a>(
267
267
indent : usize ,
268
268
end_newline : bool ,
269
269
) -> impl fmt:: Display + ' a + Captures < ' tcx > {
270
+ use fmt:: Write ;
271
+
270
272
display_fn ( move |f| {
271
273
let mut where_predicates = gens. where_predicates . iter ( ) . filter ( |pred| {
272
274
!matches ! ( pred, clean:: WherePredicate :: BoundPredicate { bounds, .. } if bounds. is_empty( ) )
@@ -280,56 +282,44 @@ crate fn print_where_clause<'a, 'tcx: 'a>(
280
282
281
283
match pred {
282
284
clean:: WherePredicate :: BoundPredicate { ty, bounds, bound_params } => {
283
- let bounds = bounds;
284
- let for_prefix = if bound_params. is_empty ( ) {
285
- String :: new ( )
286
- } else if f. alternate ( ) {
287
- format ! (
288
- "for<{:#}> " ,
289
- comma_sep( bound_params. iter( ) . map( |lt| lt. print( ) ) , true )
290
- )
291
- } else {
292
- format ! (
293
- "for<{}> " ,
294
- comma_sep( bound_params. iter( ) . map( |lt| lt. print( ) ) , true )
295
- )
296
- } ;
285
+ let ty_cx = ty. print ( cx) ;
286
+ let generic_bounds = print_generic_bounds ( bounds, cx) ;
297
287
298
- if f. alternate ( ) {
299
- write ! (
300
- f,
301
- "{}{:#}: {:#}" ,
302
- for_prefix,
303
- ty. print( cx) ,
304
- print_generic_bounds( bounds, cx)
305
- )
288
+ if bound_params. is_empty ( ) {
289
+ if f. alternate ( ) {
290
+ write ! ( f, "{ty_cx:#}: {generic_bounds:#}" )
291
+ } else {
292
+ write ! ( f, "{ty_cx}: {generic_bounds}" )
293
+ }
306
294
} else {
307
- write ! (
308
- f,
309
- "{}{}: {}" ,
310
- for_prefix,
311
- ty. print( cx) ,
312
- print_generic_bounds( bounds, cx)
313
- )
295
+ if f. alternate ( ) {
296
+ write ! (
297
+ f,
298
+ "for<{:#}> {ty_cx:#}: {generic_bounds:#}" ,
299
+ comma_sep( bound_params. iter( ) . map( |lt| lt. print( ) ) , true )
300
+ )
301
+ } else {
302
+ write ! (
303
+ f,
304
+ "for<{}> {ty_cx}: {generic_bounds}" ,
305
+ comma_sep( bound_params. iter( ) . map( |lt| lt. print( ) ) , true )
306
+ )
307
+ }
314
308
}
315
309
}
316
310
clean:: WherePredicate :: RegionPredicate { lifetime, bounds } => {
317
- write ! (
318
- f,
319
- "{}: {}" ,
320
- lifetime. print( ) ,
321
- bounds
322
- . iter( )
323
- . map( |b| b. print( cx) . to_string( ) )
324
- . collect:: <Vec <_>>( )
325
- . join( " + " )
326
- )
311
+ let mut bounds_display = String :: new ( ) ;
312
+ for bound in bounds. iter ( ) . map ( |b| b. print ( cx) ) {
313
+ write ! ( bounds_display, "{bound} + " ) ?;
314
+ }
315
+ bounds_display. truncate ( bounds_display. len ( ) - " + " . len ( ) ) ;
316
+ write ! ( f, "{}: {bounds_display}" , lifetime. print( ) )
327
317
}
328
318
clean:: WherePredicate :: EqPredicate { lhs, rhs } => {
329
319
if f. alternate ( ) {
330
- write ! ( f, "{:#} == {:#}" , lhs. print( cx) , rhs. print( cx) , )
320
+ write ! ( f, "{:#} == {:#}" , lhs. print( cx) , rhs. print( cx) )
331
321
} else {
332
- write ! ( f, "{} == {}" , lhs. print( cx) , rhs. print( cx) , )
322
+ write ! ( f, "{} == {}" , lhs. print( cx) , rhs. print( cx) )
333
323
}
334
324
}
335
325
}
@@ -340,41 +330,43 @@ crate fn print_where_clause<'a, 'tcx: 'a>(
340
330
return Ok ( ( ) ) ;
341
331
}
342
332
343
- let mut clause = String :: new ( ) ;
344
-
345
- if f. alternate ( ) {
346
- clause. push_str ( " where" ) ;
347
- } else {
333
+ let where_preds = comma_sep ( where_predicates, false ) ;
334
+ let clause = if f. alternate ( ) {
348
335
if end_newline {
349
- clause. push_str ( " <span class=\" where fmt-newline\" >where" ) ;
336
+ // add a space so stripping <br> tags and breaking spaces still renders properly
337
+ format ! ( " where{where_preds}, " )
350
338
} else {
351
- clause . push_str ( " <span class= \" where\" >where" ) ;
339
+ format ! ( " where{where_preds}" )
352
340
}
353
- }
354
-
355
- clause. push_str ( & comma_sep ( where_predicates, false ) . to_string ( ) ) ;
356
-
357
- if end_newline {
358
- clause. push ( ',' ) ;
359
- // add a space so stripping <br> tags and breaking spaces still renders properly
360
- if f. alternate ( ) {
361
- clause. push ( ' ' ) ;
362
- } else {
363
- clause. push_str ( " " ) ;
341
+ } else {
342
+ let mut br_with_padding = String :: with_capacity ( 6 * indent + 28 ) ;
343
+ br_with_padding. push_str ( "<br>" ) ;
344
+ for _ in 0 ..indent + 4 {
345
+ br_with_padding. push_str ( " " ) ;
364
346
}
365
- }
347
+ let where_preds = where_preds . to_string ( ) . replace ( "<br>" , & br_with_padding ) ;
366
348
367
- if !f. alternate ( ) {
368
- clause. push_str ( "</span>" ) ;
369
- let padding = " " . repeat ( indent + 4 ) ;
370
- clause = clause. replace ( "<br>" , & format ! ( "<br>{}" , padding) ) ;
371
- clause. insert_str ( 0 , & " " . repeat ( indent. saturating_sub ( 1 ) ) ) ;
372
- if !end_newline {
373
- // we insert the <br> after a single space but before multiple spaces at the start
374
- clause. insert_str ( if indent == 0 { 1 } else { 0 } , "<br>" ) ;
349
+ if end_newline {
350
+ let mut clause = " " . repeat ( indent. saturating_sub ( 1 ) ) ;
351
+ // add a space so stripping <br> tags and breaking spaces still renders properly
352
+ write ! (
353
+ clause,
354
+ " <span class=\" where fmt-newline\" >where{where_preds}, </span>"
355
+ ) ?;
356
+ clause
357
+ } else {
358
+ // insert a <br> tag after a single space but before multiple spaces at the start
359
+ if indent == 0 {
360
+ format ! ( " <br><span class=\" where\" >where{where_preds}</span>" )
361
+ } else {
362
+ let mut clause = br_with_padding;
363
+ clause. truncate ( clause. len ( ) - 5 * " " . len ( ) ) ;
364
+ write ! ( clause, " <span class=\" where\" >where{where_preds}</span>" ) ?;
365
+ clause
366
+ }
375
367
}
376
- }
377
- write ! ( f, "{}" , clause )
368
+ } ;
369
+ write ! ( f, "{clause}" )
378
370
} )
379
371
}
380
372
0 commit comments