@@ -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,
@@ -1510,19 +1510,15 @@ fn rewrite_index(
1510
1510
}
1511
1511
}
1512
1512
1513
- fn struct_lit_can_be_aligned ( fields : & [ ast:: Field ] , base : Option < & ast:: Expr > ) -> bool {
1514
- if base. is_some ( ) {
1515
- return false ;
1516
- }
1517
-
1518
- fields. iter ( ) . all ( |field| !field. is_shorthand )
1513
+ fn struct_lit_can_be_aligned ( fields : & [ ast:: Field ] , has_base : bool ) -> bool {
1514
+ !has_base && fields. iter ( ) . all ( |field| !field. is_shorthand )
1519
1515
}
1520
1516
1521
1517
fn rewrite_struct_lit < ' a > (
1522
1518
context : & RewriteContext < ' _ > ,
1523
1519
path : & ast:: Path ,
1524
1520
fields : & ' a [ ast:: Field ] ,
1525
- base : Option < & ' a ast:: Expr > ,
1521
+ struct_rest : & ast:: StructRest ,
1526
1522
attrs : & [ ast:: Attribute ] ,
1527
1523
span : Span ,
1528
1524
shape : Shape ,
@@ -1532,22 +1528,29 @@ fn rewrite_struct_lit<'a>(
1532
1528
enum StructLitField < ' a > {
1533
1529
Regular ( & ' a ast:: Field ) ,
1534
1530
Base ( & ' a ast:: Expr ) ,
1531
+ Rest ( & ' a Span ) ,
1535
1532
}
1536
1533
1537
1534
// 2 = " {".len()
1538
1535
let path_shape = shape. sub_width ( 2 ) ?;
1539
1536
let path_str = rewrite_path ( context, PathContext :: Expr , None , path, path_shape) ?;
1540
1537
1541
- if fields. is_empty ( ) && base. is_none ( ) {
1542
- return Some ( format ! ( "{} {{}}" , path_str) ) ;
1543
- }
1538
+
1539
+ let has_base = match struct_rest {
1540
+ ast:: StructRest :: None if fields. is_empty ( ) => return Some ( format ! ( "{} {{}}" , path_str) ) ,
1541
+ ast:: StructRest :: Rest ( _) if fields. is_empty ( ) => {
1542
+ return Some ( format ! ( "{} {{ .. }}" , path_str) ) ;
1543
+ }
1544
+ ast:: StructRest :: Base ( _) => true ,
1545
+ _ => false ,
1546
+ } ;
1544
1547
1545
1548
// Foo { a: Foo } - indent is +3, width is -5.
1546
1549
let ( h_shape, v_shape) = struct_lit_shape ( shape, context, path_str. len ( ) + 3 , 2 ) ?;
1547
1550
1548
1551
let one_line_width = h_shape. map_or ( 0 , |shape| shape. width ) ;
1549
1552
let body_lo = context. snippet_provider . span_after ( span, "{" ) ;
1550
- let fields_str = if struct_lit_can_be_aligned ( fields, base )
1553
+ let fields_str = if struct_lit_can_be_aligned ( fields, has_base )
1551
1554
&& context. config . struct_field_align_threshold ( ) > 0
1552
1555
{
1553
1556
rewrite_with_alignment (
@@ -1558,10 +1561,14 @@ fn rewrite_struct_lit<'a>(
1558
1561
one_line_width,
1559
1562
) ?
1560
1563
} else {
1561
- let field_iter = fields
1562
- . iter ( )
1563
- . map ( StructLitField :: Regular )
1564
- . chain ( base. into_iter ( ) . map ( StructLitField :: Base ) ) ;
1564
+ let field_iter = fields. iter ( ) . map ( StructLitField :: Regular ) . chain (
1565
+ match struct_rest {
1566
+ ast:: StructRest :: Base ( expr) => Some ( StructLitField :: Base ( & * * expr) ) ,
1567
+ ast:: StructRest :: Rest ( span) => Some ( StructLitField :: Rest ( span) ) ,
1568
+ ast:: StructRest :: None => None ,
1569
+ }
1570
+ . into_iter ( ) ,
1571
+ ) ;
1565
1572
1566
1573
let span_lo = |item : & StructLitField < ' _ > | match * item {
1567
1574
StructLitField :: Regular ( field) => field. span ( ) . lo ( ) ,
@@ -1571,10 +1578,12 @@ fn rewrite_struct_lit<'a>(
1571
1578
let pos = snippet. find_uncommented ( ".." ) . unwrap ( ) ;
1572
1579
last_field_hi + BytePos ( pos as u32 )
1573
1580
}
1581
+ StructLitField :: Rest ( span) => span. lo ( ) ,
1574
1582
} ;
1575
1583
let span_hi = |item : & StructLitField < ' _ > | match * item {
1576
1584
StructLitField :: Regular ( field) => field. span ( ) . hi ( ) ,
1577
1585
StructLitField :: Base ( expr) => expr. span . hi ( ) ,
1586
+ StructLitField :: Rest ( span) => span. hi ( ) ,
1578
1587
} ;
1579
1588
let rewrite = |item : & StructLitField < ' _ > | match * item {
1580
1589
StructLitField :: Regular ( field) => {
@@ -1586,6 +1595,7 @@ fn rewrite_struct_lit<'a>(
1586
1595
expr. rewrite ( context, v_shape. offset_left ( 2 ) ?)
1587
1596
. map ( |s| format ! ( "..{}" , s) )
1588
1597
}
1598
+ StructLitField :: Rest ( _) => Some ( ".." . to_owned ( ) ) ,
1589
1599
} ;
1590
1600
1591
1601
let items = itemize_list (
@@ -1612,7 +1622,7 @@ fn rewrite_struct_lit<'a>(
1612
1622
nested_shape,
1613
1623
tactic,
1614
1624
context,
1615
- force_no_trailing_comma || base . is_some ( ) || !context. use_block_indent ( ) ,
1625
+ force_no_trailing_comma || has_base || !context. use_block_indent ( ) ,
1616
1626
) ;
1617
1627
1618
1628
write_list ( & item_vec, & fmt) ?
0 commit comments