@@ -801,17 +801,12 @@ fn format_impl_ref_and_type(
801801 result. push_str ( format_defaultness ( defaultness) ) ;
802802 result. push_str ( format_unsafety ( unsafety) ) ;
803803
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- } ;
809804 let shape = generics_shape_from_config (
810805 context. config ,
811806 Shape :: indented ( offset + last_line_width ( & result) , context. config ) ,
812807 0 ,
813808 ) ?;
814- let generics_str = rewrite_generics ( context, "impl" , generics, shape, mk_sp ( lo , hi ) ) ?;
809+ let generics_str = rewrite_generics ( context, "impl" , generics, shape) ?;
815810 result. push_str ( & generics_str) ;
816811
817812 let polarity_str = if polarity == ast:: ImplPolarity :: Negative {
@@ -986,13 +981,8 @@ pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent)
986981 let body_lo = context. snippet_provider . span_after ( item. span , "{" ) ;
987982
988983 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) ?;
996986 result. push_str ( & generics_str) ;
997987
998988 // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
@@ -1138,7 +1128,7 @@ pub fn format_trait_alias(
11381128 let alias = rewrite_ident ( context, ident) ;
11391129 // 6 = "trait ", 2 = " ="
11401130 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) ?;
11421132 let lhs = format ! ( "trait {} =" , generics_str) ;
11431133 // 1 = ";"
11441134 rewrite_assign_rhs ( context, lhs, generic_bounds, shape. sub_width ( 1 ) ?) . map ( |s| s + ";" )
@@ -1340,8 +1330,7 @@ fn format_tuple_struct(
13401330 Some ( generics) => {
13411331 let budget = context. budget ( last_line_width ( & header_str) ) ;
13421332 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) ?;
13451334 result. push_str ( & generics_str) ;
13461335
13471336 let where_budget = context. budget ( last_line_width ( & result) ) ;
@@ -1400,36 +1389,27 @@ fn format_tuple_struct(
14001389 Some ( result)
14011390}
14021391
1403- pub fn rewrite_type_alias (
1392+ fn rewrite_type_prefix (
14041393 context : & RewriteContext ,
14051394 indent : Indent ,
1395+ prefix : & str ,
14061396 ident : ast:: Ident ,
1407- ty : & ast:: Ty ,
14081397 generics : & ast:: Generics ,
1409- vis : & ast:: Visibility ,
1410- span : Span ,
14111398) -> Option < String > {
14121399 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) ;
14161402
14171403 // 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+ }
14331413
14341414 let where_budget = context. budget ( last_line_width ( & result) ) ;
14351415 let option = WhereClauseOption :: snuggled ( & result) ;
@@ -1440,24 +1420,76 @@ pub fn rewrite_type_alias(
14401420 Shape :: legacy ( where_budget, indent) ,
14411421 Density :: Vertical ,
14421422 "=" ,
1443- Some ( span . hi ( ) ) ,
1423+ None ,
14441424 generics. span . hi ( ) ,
14451425 option,
14461426 false ,
14471427 ) ?;
14481428 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) ;
14511454 } 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 ( ) ) ;
14561457 }
14571458
14581459 // 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+ )
14611493}
14621494
14631495fn type_annotation_spacing ( config : & Config ) -> ( & str , & str ) {
@@ -1706,6 +1738,16 @@ pub fn rewrite_associated_type(
17061738 }
17071739}
17081740
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+
17091751pub fn rewrite_associated_impl_type (
17101752 ident : ast:: Ident ,
17111753 defaultness : ast:: Defaultness ,
@@ -1889,13 +1931,11 @@ fn rewrite_fn_base(
18891931 offset : used_width,
18901932 } ;
18911933 let fd = fn_sig. decl ;
1892- let g_span = mk_sp ( span. lo ( ) , fd. output . span ( ) . lo ( ) ) ;
18931934 let generics_str = rewrite_generics (
18941935 context,
18951936 rewrite_ident ( context, ident) ,
18961937 fn_sig. generics ,
18971938 shape,
1898- g_span,
18991939 ) ?;
19001940 result. push_str ( & generics_str) ;
19011941
@@ -2413,7 +2453,6 @@ fn rewrite_generics(
24132453 ident : & str ,
24142454 generics : & ast:: Generics ,
24152455 shape : Shape ,
2416- span : Span ,
24172456) -> Option < String > {
24182457 // FIXME: convert bounds to where clauses where they get too big or if
24192458 // there is a where clause at all.
@@ -2423,7 +2462,7 @@ fn rewrite_generics(
24232462 }
24242463
24252464 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 )
24272466}
24282467
24292468pub fn generics_shape_from_config ( config : & Config , shape : Shape , offset : usize ) -> Option < Shape > {
@@ -2711,7 +2750,7 @@ fn format_generics(
27112750 used_width : usize ,
27122751) -> Option < String > {
27132752 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) ?;
27152754
27162755 let same_line_brace = if !generics. where_clause . predicates . is_empty ( ) || result. contains ( '\n' ) {
27172756 let budget = context. budget ( last_line_used_width ( & result, offset. width ( ) ) ) ;
0 commit comments