@@ -106,11 +106,11 @@ pub(crate) fn format_expr(
106
106
} )
107
107
}
108
108
ast:: ExprKind :: Unary ( op, ref subexpr) => rewrite_unary_op ( context, op, subexpr, shape) ,
109
- ast:: ExprKind :: Struct ( ref path, ref fields, ref base ) => rewrite_struct_lit (
109
+ ast:: ExprKind :: Struct ( ref path, ref fields, ref struct_rest ) => rewrite_struct_lit (
110
110
context,
111
111
path,
112
112
fields,
113
- base . as_ref ( ) . map ( |e| & * * e ) ,
113
+ struct_rest ,
114
114
& expr. attrs ,
115
115
expr. span ,
116
116
shape,
@@ -1589,19 +1589,15 @@ fn rewrite_index(
1589
1589
}
1590
1590
}
1591
1591
1592
- fn struct_lit_can_be_aligned ( fields : & [ ast:: Field ] , base : Option < & ast:: Expr > ) -> bool {
1593
- if base. is_some ( ) {
1594
- return false ;
1595
- }
1596
-
1597
- fields. iter ( ) . all ( |field| !field. is_shorthand )
1592
+ fn struct_lit_can_be_aligned ( fields : & [ ast:: Field ] , has_base : bool ) -> bool {
1593
+ !has_base && fields. iter ( ) . all ( |field| !field. is_shorthand )
1598
1594
}
1599
1595
1600
1596
fn rewrite_struct_lit < ' a > (
1601
1597
context : & RewriteContext < ' _ > ,
1602
1598
path : & ast:: Path ,
1603
1599
fields : & ' a [ ast:: Field ] ,
1604
- base : Option < & ' a ast:: Expr > ,
1600
+ struct_rest : & ast:: StructRest ,
1605
1601
attrs : & [ ast:: Attribute ] ,
1606
1602
span : Span ,
1607
1603
shape : Shape ,
@@ -1611,22 +1607,29 @@ fn rewrite_struct_lit<'a>(
1611
1607
enum StructLitField < ' a > {
1612
1608
Regular ( & ' a ast:: Field ) ,
1613
1609
Base ( & ' a ast:: Expr ) ,
1610
+ Rest ( & ' a Span ) ,
1614
1611
}
1615
1612
1616
1613
// 2 = " {".len()
1617
1614
let path_shape = shape. sub_width ( 2 ) ?;
1618
1615
let path_str = rewrite_path ( context, PathContext :: Expr , None , path, path_shape) ?;
1619
1616
1620
- if fields. is_empty ( ) && base. is_none ( ) {
1621
- return Some ( format ! ( "{} {{}}" , path_str) ) ;
1622
- }
1617
+
1618
+ let has_base = match struct_rest {
1619
+ ast:: StructRest :: None if fields. is_empty ( ) => return Some ( format ! ( "{} {{}}" , path_str) ) ,
1620
+ ast:: StructRest :: Rest ( _) if fields. is_empty ( ) => {
1621
+ return Some ( format ! ( "{} {{ .. }}" , path_str) ) ;
1622
+ }
1623
+ ast:: StructRest :: Base ( _) => true ,
1624
+ _ => false ,
1625
+ } ;
1623
1626
1624
1627
// Foo { a: Foo } - indent is +3, width is -5.
1625
1628
let ( h_shape, v_shape) = struct_lit_shape ( shape, context, path_str. len ( ) + 3 , 2 ) ?;
1626
1629
1627
1630
let one_line_width = h_shape. map_or ( 0 , |shape| shape. width ) ;
1628
1631
let body_lo = context. snippet_provider . span_after ( span, "{" ) ;
1629
- let fields_str = if struct_lit_can_be_aligned ( fields, base )
1632
+ let fields_str = if struct_lit_can_be_aligned ( fields, has_base )
1630
1633
&& context. config . struct_field_align_threshold ( ) > 0
1631
1634
{
1632
1635
rewrite_with_alignment (
@@ -1637,10 +1640,14 @@ fn rewrite_struct_lit<'a>(
1637
1640
one_line_width,
1638
1641
) ?
1639
1642
} else {
1640
- let field_iter = fields
1641
- . iter ( )
1642
- . map ( StructLitField :: Regular )
1643
- . chain ( base. into_iter ( ) . map ( StructLitField :: Base ) ) ;
1643
+ let field_iter = fields. iter ( ) . map ( StructLitField :: Regular ) . chain (
1644
+ match struct_rest {
1645
+ ast:: StructRest :: Base ( expr) => Some ( StructLitField :: Base ( & * * expr) ) ,
1646
+ ast:: StructRest :: Rest ( span) => Some ( StructLitField :: Rest ( span) ) ,
1647
+ ast:: StructRest :: None => None ,
1648
+ }
1649
+ . into_iter ( ) ,
1650
+ ) ;
1644
1651
1645
1652
let span_lo = |item : & StructLitField < ' _ > | match * item {
1646
1653
StructLitField :: Regular ( field) => field. span ( ) . lo ( ) ,
@@ -1650,10 +1657,12 @@ fn rewrite_struct_lit<'a>(
1650
1657
let pos = snippet. find_uncommented ( ".." ) . unwrap ( ) ;
1651
1658
last_field_hi + BytePos ( pos as u32 )
1652
1659
}
1660
+ StructLitField :: Rest ( span) => span. lo ( ) ,
1653
1661
} ;
1654
1662
let span_hi = |item : & StructLitField < ' _ > | match * item {
1655
1663
StructLitField :: Regular ( field) => field. span ( ) . hi ( ) ,
1656
1664
StructLitField :: Base ( expr) => expr. span . hi ( ) ,
1665
+ StructLitField :: Rest ( span) => span. hi ( ) ,
1657
1666
} ;
1658
1667
let rewrite = |item : & StructLitField < ' _ > | match * item {
1659
1668
StructLitField :: Regular ( field) => {
@@ -1665,6 +1674,7 @@ fn rewrite_struct_lit<'a>(
1665
1674
expr. rewrite ( context, v_shape. offset_left ( 2 ) ?)
1666
1675
. map ( |s| format ! ( "..{}" , s) )
1667
1676
}
1677
+ StructLitField :: Rest ( _) => Some ( ".." . to_owned ( ) ) ,
1668
1678
} ;
1669
1679
1670
1680
let items = itemize_list (
@@ -1691,7 +1701,7 @@ fn rewrite_struct_lit<'a>(
1691
1701
nested_shape,
1692
1702
tactic,
1693
1703
context,
1694
- force_no_trailing_comma || base . is_some ( ) || !context. use_block_indent ( ) ,
1704
+ force_no_trailing_comma || has_base || !context. use_block_indent ( ) ,
1695
1705
) ;
1696
1706
1697
1707
write_list ( & item_vec, & fmt) ?
0 commit comments