@@ -128,6 +128,7 @@ pub(crate) fn format_expr(
128
128
expr. span ,
129
129
shape,
130
130
)
131
+ . ok ( )
131
132
}
132
133
ast:: ExprKind :: Tup ( ref items) => {
133
134
rewrite_tuple ( context, items. iter ( ) , expr. span , shape, items. len ( ) == 1 )
@@ -1603,7 +1604,7 @@ fn rewrite_struct_lit<'a>(
1603
1604
attrs : & [ ast:: Attribute ] ,
1604
1605
span : Span ,
1605
1606
shape : Shape ,
1606
- ) -> Option < String > {
1607
+ ) -> RewriteResult {
1607
1608
debug ! ( "rewrite_struct_lit: shape {:?}" , shape) ;
1608
1609
1609
1610
enum StructLitField < ' a > {
@@ -1613,20 +1614,21 @@ fn rewrite_struct_lit<'a>(
1613
1614
}
1614
1615
1615
1616
// 2 = " {".len()
1616
- let path_shape = shape. sub_width ( 2 ) ?;
1617
- let path_str = rewrite_path ( context, PathContext :: Expr , qself, path, path_shape) . ok ( ) ?;
1617
+ let path_shape = shape. sub_width ( 2 ) . max_width_error ( shape . width , span ) ?;
1618
+ let path_str = rewrite_path ( context, PathContext :: Expr , qself, path, path_shape) ?;
1618
1619
1619
1620
let has_base_or_rest = match struct_rest {
1620
- ast:: StructRest :: None if fields. is_empty ( ) => return Some ( format ! ( "{path_str} {{}}" ) ) ,
1621
+ ast:: StructRest :: None if fields. is_empty ( ) => return Ok ( format ! ( "{path_str} {{}}" ) ) ,
1621
1622
ast:: StructRest :: Rest ( _) if fields. is_empty ( ) => {
1622
- return Some ( format ! ( "{path_str} {{ .. }}" ) ) ;
1623
+ return Ok ( format ! ( "{path_str} {{ .. }}" ) ) ;
1623
1624
}
1624
1625
ast:: StructRest :: Rest ( _) | ast:: StructRest :: Base ( _) => true ,
1625
1626
_ => false ,
1626
1627
} ;
1627
1628
1628
1629
// Foo { a: Foo } - indent is +3, width is -5.
1629
- let ( h_shape, v_shape) = struct_lit_shape ( shape, context, path_str. len ( ) + 3 , 2 ) ?;
1630
+ let ( h_shape, v_shape) = struct_lit_shape ( shape, context, path_str. len ( ) + 3 , 2 )
1631
+ . max_width_error ( shape. width , span) ?;
1630
1632
1631
1633
let one_line_width = h_shape. map_or ( 0 , |shape| shape. width ) ;
1632
1634
let body_lo = context. snippet_provider . span_after ( span, "{" ) ;
@@ -1639,7 +1641,8 @@ fn rewrite_struct_lit<'a>(
1639
1641
v_shape,
1640
1642
mk_sp ( body_lo, span. hi ( ) ) ,
1641
1643
one_line_width,
1642
- ) ?
1644
+ )
1645
+ . unknown_error ( ) ?
1643
1646
} else {
1644
1647
let field_iter = fields. iter ( ) . map ( StructLitField :: Regular ) . chain (
1645
1648
match struct_rest {
@@ -1668,12 +1671,13 @@ fn rewrite_struct_lit<'a>(
1668
1671
let rewrite = |item : & StructLitField < ' _ > | match * item {
1669
1672
StructLitField :: Regular ( field) => {
1670
1673
// The 1 taken from the v_budget is for the comma.
1671
- rewrite_field ( context, field, v_shape. sub_width ( 1 ) ?, 0 )
1674
+ let v_shape = v_shape. sub_width ( 1 ) ?;
1675
+ rewrite_field ( context, field, v_shape, 0 ) . ok ( )
1672
1676
}
1673
1677
StructLitField :: Base ( expr) => {
1674
1678
// 2 = ..
1675
- expr . rewrite ( context , v_shape. offset_left ( 2 ) ?)
1676
- . map ( |s| format ! ( "..{}" , s) )
1679
+ let v_shape = v_shape . sub_width ( 2 ) ?;
1680
+ expr . rewrite ( context , v_shape ) . map ( |s| format ! ( "..{}" , s) )
1677
1681
}
1678
1682
StructLitField :: Rest ( _) => Some ( ".." . to_owned ( ) ) ,
1679
1683
} ;
@@ -1705,12 +1709,12 @@ fn rewrite_struct_lit<'a>(
1705
1709
force_no_trailing_comma || has_base_or_rest || !context. use_block_indent ( ) ,
1706
1710
) ;
1707
1711
1708
- write_list ( & item_vec, & fmt) ?
1712
+ write_list ( & item_vec, & fmt) . unknown_error ( ) ?
1709
1713
} ;
1710
1714
1711
1715
let fields_str =
1712
1716
wrap_struct_field ( context, attrs, & fields_str, shape, v_shape, one_line_width) ?;
1713
- Some ( format ! ( "{path_str} {{{fields_str}}}" ) )
1717
+ Ok ( format ! ( "{path_str} {{{fields_str}}}" ) )
1714
1718
1715
1719
// FIXME if context.config.indent_style() == Visual, but we run out
1716
1720
// of space, we should fall back to BlockIndent.
@@ -1723,7 +1727,7 @@ pub(crate) fn wrap_struct_field(
1723
1727
shape : Shape ,
1724
1728
nested_shape : Shape ,
1725
1729
one_line_width : usize ,
1726
- ) -> Option < String > {
1730
+ ) -> RewriteResult {
1727
1731
let should_vertical = context. config . indent_style ( ) == IndentStyle :: Block
1728
1732
&& ( fields_str. contains ( '\n' )
1729
1733
|| !context. config . struct_lit_single_line ( )
@@ -1732,21 +1736,21 @@ pub(crate) fn wrap_struct_field(
1732
1736
let inner_attrs = & inner_attributes ( attrs) ;
1733
1737
if inner_attrs. is_empty ( ) {
1734
1738
if should_vertical {
1735
- Some ( format ! (
1739
+ Ok ( format ! (
1736
1740
"{}{}{}" ,
1737
1741
nested_shape. indent. to_string_with_newline( context. config) ,
1738
1742
fields_str,
1739
1743
shape. indent. to_string_with_newline( context. config)
1740
1744
) )
1741
1745
} else {
1742
1746
// One liner or visual indent.
1743
- Some ( format ! ( " {fields_str} " ) )
1747
+ Ok ( format ! ( " {fields_str} " ) )
1744
1748
}
1745
1749
} else {
1746
- Some ( format ! (
1750
+ Ok ( format ! (
1747
1751
"{}{}{}{}{}" ,
1748
1752
nested_shape. indent. to_string_with_newline( context. config) ,
1749
- inner_attrs. rewrite ( context, shape) ?,
1753
+ inner_attrs. rewrite_result ( context, shape) ?,
1750
1754
nested_shape. indent. to_string_with_newline( context. config) ,
1751
1755
fields_str,
1752
1756
shape. indent. to_string_with_newline( context. config)
@@ -1763,38 +1767,40 @@ pub(crate) fn rewrite_field(
1763
1767
field : & ast:: ExprField ,
1764
1768
shape : Shape ,
1765
1769
prefix_max_width : usize ,
1766
- ) -> Option < String > {
1770
+ ) -> RewriteResult {
1767
1771
if contains_skip ( & field. attrs ) {
1768
- return Some ( context. snippet ( field. span ( ) ) . to_owned ( ) ) ;
1772
+ return Ok ( context. snippet ( field. span ( ) ) . to_owned ( ) ) ;
1769
1773
}
1770
- let mut attrs_str = field. attrs . rewrite ( context, shape) ?;
1774
+ let mut attrs_str = field. attrs . rewrite_result ( context, shape) ?;
1771
1775
if !attrs_str. is_empty ( ) {
1772
1776
attrs_str. push_str ( & shape. indent . to_string_with_newline ( context. config ) ) ;
1773
1777
} ;
1774
1778
let name = context. snippet ( field. ident . span ) ;
1775
1779
if field. is_shorthand {
1776
- Some ( attrs_str + name)
1780
+ Ok ( attrs_str + name)
1777
1781
} else {
1778
1782
let mut separator = String :: from ( struct_lit_field_separator ( context. config ) ) ;
1779
1783
for _ in 0 ..prefix_max_width. saturating_sub ( name. len ( ) ) {
1780
1784
separator. push ( ' ' ) ;
1781
1785
}
1782
1786
let overhead = name. len ( ) + separator. len ( ) ;
1783
- let expr_shape = shape. offset_left ( overhead) ?;
1784
- let expr = field. expr . rewrite ( context, expr_shape) ;
1787
+ let expr_shape = shape
1788
+ . offset_left ( overhead)
1789
+ . max_width_error ( shape. width , field. span ) ?;
1790
+ let expr = field. expr . rewrite_result ( context, expr_shape) ;
1785
1791
let is_lit = matches ! ( field. expr. kind, ast:: ExprKind :: Lit ( _) ) ;
1786
1792
match expr {
1787
- Some ( ref e)
1793
+ Ok ( ref e)
1788
1794
if !is_lit && e. as_str ( ) == name && context. config . use_field_init_shorthand ( ) =>
1789
1795
{
1790
- Some ( attrs_str + name)
1796
+ Ok ( attrs_str + name)
1791
1797
}
1792
- Some ( e) => Some ( format ! ( "{attrs_str}{name}{separator}{e}" ) ) ,
1793
- None => {
1798
+ Ok ( e) => Ok ( format ! ( "{attrs_str}{name}{separator}{e}" ) ) ,
1799
+ Err ( _ ) => {
1794
1800
let expr_offset = shape. indent . block_indent ( context. config ) ;
1795
1801
let expr = field
1796
1802
. expr
1797
- . rewrite ( context, Shape :: indented ( expr_offset, context. config ) ) ;
1803
+ . rewrite_result ( context, Shape :: indented ( expr_offset, context. config ) ) ;
1798
1804
expr. map ( |s| {
1799
1805
format ! (
1800
1806
"{}{}:\n {}{}" ,
0 commit comments