@@ -801,17 +801,12 @@ fn format_impl_ref_and_type(
801
801
result. push_str ( format_defaultness ( defaultness) ) ;
802
802
result. push_str ( format_unsafety ( unsafety) ) ;
803
803
804
- let lo = context. snippet_provider . span_after ( item. span , "impl" ) ;
805
- let hi = match * trait_ref {
806
- Some ( ref tr) => tr. path . span . lo ( ) ,
807
- None => self_ty. span . lo ( ) ,
808
- } ;
809
804
let shape = generics_shape_from_config (
810
805
context. config ,
811
806
Shape :: indented ( offset + last_line_width ( & result) , context. config ) ,
812
807
0 ,
813
808
) ?;
814
- let generics_str = rewrite_generics ( context, "impl" , generics, shape, mk_sp ( lo , hi ) ) ?;
809
+ let generics_str = rewrite_generics ( context, "impl" , generics, shape) ?;
815
810
result. push_str ( & generics_str) ;
816
811
817
812
let polarity_str = if polarity == ast:: ImplPolarity :: Negative {
@@ -986,13 +981,8 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
986
981
let body_lo = context. snippet_provider . span_after ( item. span , "{" ) ;
987
982
988
983
let shape = Shape :: indented ( offset, context. config ) . offset_left ( result. len ( ) ) ?;
989
- let generics_str = rewrite_generics (
990
- context,
991
- rewrite_ident ( context, item. ident ) ,
992
- generics,
993
- shape,
994
- mk_sp ( item. span . lo ( ) , body_lo) ,
995
- ) ?;
984
+ let generics_str =
985
+ rewrite_generics ( context, rewrite_ident ( context, item. ident ) , generics, shape) ?;
996
986
result. push_str ( & generics_str) ;
997
987
998
988
// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
@@ -1138,7 +1128,7 @@ pub fn format_trait_alias(
1138
1128
let alias = rewrite_ident ( context, ident) ;
1139
1129
// 6 = "trait ", 2 = " ="
1140
1130
let g_shape = shape. offset_left ( 6 ) ?. sub_width ( 2 ) ?;
1141
- let generics_str = rewrite_generics ( context, & alias, generics, g_shape, generics . span ) ?;
1131
+ let generics_str = rewrite_generics ( context, & alias, generics, g_shape) ?;
1142
1132
let lhs = format ! ( "trait {} =" , generics_str) ;
1143
1133
// 1 = ";"
1144
1134
rewrite_assign_rhs ( context, lhs, generic_bounds, shape. sub_width ( 1 ) ?) . map ( |s| s + ";" )
@@ -1340,8 +1330,7 @@ fn format_tuple_struct(
1340
1330
Some ( generics) => {
1341
1331
let budget = context. budget ( last_line_width ( & header_str) ) ;
1342
1332
let shape = Shape :: legacy ( budget, offset) ;
1343
- let g_span = mk_sp ( span. lo ( ) , body_lo) ;
1344
- let generics_str = rewrite_generics ( context, "" , generics, shape, g_span) ?;
1333
+ let generics_str = rewrite_generics ( context, "" , generics, shape) ?;
1345
1334
result. push_str ( & generics_str) ;
1346
1335
1347
1336
let where_budget = context. budget ( last_line_width ( & result) ) ;
@@ -1400,36 +1389,27 @@ fn format_tuple_struct(
1400
1389
Some ( result)
1401
1390
}
1402
1391
1403
- pub fn rewrite_type_alias (
1392
+ fn rewrite_type_prefix (
1404
1393
context : & RewriteContext ,
1405
1394
indent : Indent ,
1395
+ prefix : & str ,
1406
1396
ident : ast:: Ident ,
1407
- ty : & ast:: Ty ,
1408
1397
generics : & ast:: Generics ,
1409
- vis : & ast:: Visibility ,
1410
- span : Span ,
1411
1398
) -> Option < String > {
1412
1399
let mut result = String :: with_capacity ( 128 ) ;
1413
-
1414
- result. push_str ( & format_visibility ( context, vis) ) ;
1415
- result. push_str ( "type " ) ;
1400
+ result. push_str ( prefix) ;
1401
+ let ident_str = rewrite_ident ( context, ident) ;
1416
1402
1417
1403
// 2 = `= `
1418
- let g_shape = Shape :: indented ( indent, context. config )
1419
- . offset_left ( result. len ( ) ) ?
1420
- . sub_width ( 2 ) ?;
1421
- let g_span = mk_sp (
1422
- context. snippet_provider . span_after ( span, "type" ) ,
1423
- ty. span . lo ( ) ,
1424
- ) ;
1425
- let generics_str = rewrite_generics (
1426
- context,
1427
- rewrite_ident ( context, ident) ,
1428
- generics,
1429
- g_shape,
1430
- g_span,
1431
- ) ?;
1432
- result. push_str ( & generics_str) ;
1404
+ if generics. params . is_empty ( ) {
1405
+ result. push_str ( ident_str)
1406
+ } else {
1407
+ let g_shape = Shape :: indented ( indent, context. config )
1408
+ . offset_left ( result. len ( ) ) ?
1409
+ . sub_width ( 2 ) ?;
1410
+ let generics_str = rewrite_generics ( context, ident_str, generics, g_shape) ?;
1411
+ result. push_str ( & generics_str) ;
1412
+ }
1433
1413
1434
1414
let where_budget = context. budget ( last_line_width ( & result) ) ;
1435
1415
let option = WhereClauseOption :: snuggled ( & result) ;
@@ -1440,24 +1420,76 @@ pub fn rewrite_type_alias(
1440
1420
Shape :: legacy ( where_budget, indent) ,
1441
1421
Density :: Vertical ,
1442
1422
"=" ,
1443
- Some ( span . hi ( ) ) ,
1423
+ None ,
1444
1424
generics. span . hi ( ) ,
1445
1425
option,
1446
1426
false ,
1447
1427
) ?;
1448
1428
result. push_str ( & where_clause_str) ;
1449
- if where_clause_str. is_empty ( ) {
1450
- result. push_str ( " =" ) ;
1429
+
1430
+ Some ( result)
1431
+ }
1432
+
1433
+ fn rewrite_type_item < R : Rewrite > (
1434
+ context : & RewriteContext ,
1435
+ indent : Indent ,
1436
+ prefix : & str ,
1437
+ suffix : & str ,
1438
+ ident : ast:: Ident ,
1439
+ rhs : & R ,
1440
+ generics : & ast:: Generics ,
1441
+ vis : & ast:: Visibility ,
1442
+ ) -> Option < String > {
1443
+ let mut result = String :: with_capacity ( 128 ) ;
1444
+ result. push_str ( & rewrite_type_prefix (
1445
+ context,
1446
+ indent,
1447
+ & format ! ( "{}{} " , format_visibility( context, vis) , prefix) ,
1448
+ ident,
1449
+ generics,
1450
+ ) ?) ;
1451
+
1452
+ if generics. where_clause . predicates . is_empty ( ) {
1453
+ result. push_str ( suffix) ;
1451
1454
} else {
1452
- result. push_str ( & format ! (
1453
- "{}=" ,
1454
- indent. to_string_with_newline( context. config)
1455
- ) ) ;
1455
+ result. push_str ( & indent. to_string_with_newline ( context. config ) ) ;
1456
+ result. push_str ( suffix. trim_left ( ) ) ;
1456
1457
}
1457
1458
1458
1459
// 1 = ";"
1459
- let ty_shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1460
- rewrite_assign_rhs ( context, result, ty, ty_shape) . map ( |s| s + ";" )
1460
+ let rhs_shape = Shape :: indented ( indent, context. config ) . sub_width ( 1 ) ?;
1461
+ rewrite_assign_rhs ( context, result, rhs, rhs_shape) . map ( |s| s + ";" )
1462
+ }
1463
+
1464
+ pub fn rewrite_type_alias (
1465
+ context : & RewriteContext ,
1466
+ indent : Indent ,
1467
+ ident : ast:: Ident ,
1468
+ ty : & ast:: Ty ,
1469
+ generics : & ast:: Generics ,
1470
+ vis : & ast:: Visibility ,
1471
+ ) -> Option < String > {
1472
+ rewrite_type_item ( context, indent, "type" , " =" , ident, ty, generics, vis)
1473
+ }
1474
+
1475
+ pub fn rewrite_existential_type (
1476
+ context : & RewriteContext ,
1477
+ indent : Indent ,
1478
+ ident : ast:: Ident ,
1479
+ generic_bounds : & ast:: GenericBounds ,
1480
+ generics : & ast:: Generics ,
1481
+ vis : & ast:: Visibility ,
1482
+ ) -> Option < String > {
1483
+ rewrite_type_item (
1484
+ context,
1485
+ indent,
1486
+ "existential type" ,
1487
+ ":" ,
1488
+ ident,
1489
+ generic_bounds,
1490
+ generics,
1491
+ vis,
1492
+ )
1461
1493
}
1462
1494
1463
1495
fn type_annotation_spacing ( config : & Config ) -> ( & str , & str ) {
@@ -1706,6 +1738,16 @@ pub fn rewrite_associated_type(
1706
1738
}
1707
1739
}
1708
1740
1741
+ pub fn rewrite_existential_impl_type (
1742
+ context : & RewriteContext ,
1743
+ ident : ast:: Ident ,
1744
+ generic_bounds : & ast:: GenericBounds ,
1745
+ indent : Indent ,
1746
+ ) -> Option < String > {
1747
+ rewrite_associated_type ( ident, None , Some ( generic_bounds) , context, indent)
1748
+ . map ( |s| format ! ( "existential {}" , s) )
1749
+ }
1750
+
1709
1751
pub fn rewrite_associated_impl_type (
1710
1752
ident : ast:: Ident ,
1711
1753
defaultness : ast:: Defaultness ,
@@ -1889,13 +1931,11 @@ fn rewrite_fn_base(
1889
1931
offset : used_width,
1890
1932
} ;
1891
1933
let fd = fn_sig. decl ;
1892
- let g_span = mk_sp ( span. lo ( ) , fd. output . span ( ) . lo ( ) ) ;
1893
1934
let generics_str = rewrite_generics (
1894
1935
context,
1895
1936
rewrite_ident ( context, ident) ,
1896
1937
fn_sig. generics ,
1897
1938
shape,
1898
- g_span,
1899
1939
) ?;
1900
1940
result. push_str ( & generics_str) ;
1901
1941
@@ -2413,7 +2453,6 @@ fn rewrite_generics(
2413
2453
ident : & str ,
2414
2454
generics : & ast:: Generics ,
2415
2455
shape : Shape ,
2416
- span : Span ,
2417
2456
) -> Option < String > {
2418
2457
// FIXME: convert bounds to where clauses where they get too big or if
2419
2458
// there is a where clause at all.
@@ -2423,7 +2462,7 @@ fn rewrite_generics(
2423
2462
}
2424
2463
2425
2464
let params = & generics. params . iter ( ) . map ( |e| & * e) . collect :: < Vec < _ > > ( ) ;
2426
- overflow:: rewrite_with_angle_brackets ( context, ident, params, shape, span)
2465
+ overflow:: rewrite_with_angle_brackets ( context, ident, params, shape, generics . span )
2427
2466
}
2428
2467
2429
2468
pub fn generics_shape_from_config ( config : & Config , shape : Shape , offset : usize ) -> Option < Shape > {
@@ -2711,7 +2750,7 @@ fn format_generics(
2711
2750
used_width : usize ,
2712
2751
) -> Option < String > {
2713
2752
let shape = Shape :: legacy ( context. budget ( used_width + offset. width ( ) ) , offset) ;
2714
- let mut result = rewrite_generics ( context, "" , generics, shape, span ) ?;
2753
+ let mut result = rewrite_generics ( context, "" , generics, shape) ?;
2715
2754
2716
2755
let same_line_brace = if !generics. where_clause . predicates . is_empty ( ) || result. contains ( '\n' ) {
2717
2756
let budget = context. budget ( last_line_used_width ( & result, offset. width ( ) ) ) ;
0 commit comments