@@ -22,7 +22,7 @@ use crate::macros::{rewrite_macro, MacroPosition};
22
22
use crate :: matches:: rewrite_match;
23
23
use crate :: overflow:: { self , IntoOverflowableItem , OverflowableItem } ;
24
24
use crate :: pairs:: { rewrite_all_pairs, rewrite_pair, PairParts } ;
25
- use crate :: rewrite:: { Rewrite , RewriteContext } ;
25
+ use crate :: rewrite:: { Rewrite , RewriteContext , RewriteErrorExt , RewriteResult } ;
26
26
use crate :: shape:: { Indent , Shape } ;
27
27
use crate :: source_map:: { LineRangeUtils , SpanUtils } ;
28
28
use crate :: spanned:: Spanned ;
@@ -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 )
@@ -1595,7 +1596,7 @@ fn rewrite_struct_lit<'a>(
1595
1596
attrs : & [ ast:: Attribute ] ,
1596
1597
span : Span ,
1597
1598
shape : Shape ,
1598
- ) -> Option < String > {
1599
+ ) -> RewriteResult {
1599
1600
debug ! ( "rewrite_struct_lit: shape {:?}" , shape) ;
1600
1601
1601
1602
enum StructLitField < ' a > {
@@ -1605,20 +1606,21 @@ fn rewrite_struct_lit<'a>(
1605
1606
}
1606
1607
1607
1608
// 2 = " {".len()
1608
- let path_shape = shape. sub_width ( 2 ) ?;
1609
- let path_str = rewrite_path ( context, PathContext :: Expr , qself, path, path_shape) . ok ( ) ?;
1609
+ let path_shape = shape. sub_width ( 2 ) . max_width_error ( shape . width , span ) ?;
1610
+ let path_str = rewrite_path ( context, PathContext :: Expr , qself, path, path_shape) ?;
1610
1611
1611
1612
let has_base_or_rest = match struct_rest {
1612
- ast:: StructRest :: None if fields. is_empty ( ) => return Some ( format ! ( "{path_str} {{}}" ) ) ,
1613
+ ast:: StructRest :: None if fields. is_empty ( ) => return Ok ( format ! ( "{path_str} {{}}" ) ) ,
1613
1614
ast:: StructRest :: Rest ( _) if fields. is_empty ( ) => {
1614
- return Some ( format ! ( "{path_str} {{ .. }}" ) ) ;
1615
+ return Ok ( format ! ( "{path_str} {{ .. }}" ) ) ;
1615
1616
}
1616
1617
ast:: StructRest :: Rest ( _) | ast:: StructRest :: Base ( _) => true ,
1617
1618
_ => false ,
1618
1619
} ;
1619
1620
1620
1621
// Foo { a: Foo } - indent is +3, width is -5.
1621
- let ( h_shape, v_shape) = struct_lit_shape ( shape, context, path_str. len ( ) + 3 , 2 ) ?;
1622
+ let ( h_shape, v_shape) = struct_lit_shape ( shape, context, path_str. len ( ) + 3 , 2 )
1623
+ . max_width_error ( shape. width , span) ?;
1622
1624
1623
1625
let one_line_width = h_shape. map_or ( 0 , |shape| shape. width ) ;
1624
1626
let body_lo = context. snippet_provider . span_after ( span, "{" ) ;
@@ -1631,7 +1633,8 @@ fn rewrite_struct_lit<'a>(
1631
1633
v_shape,
1632
1634
mk_sp ( body_lo, span. hi ( ) ) ,
1633
1635
one_line_width,
1634
- ) ?
1636
+ )
1637
+ . unknown_error ( ) ?
1635
1638
} else {
1636
1639
let field_iter = fields. iter ( ) . map ( StructLitField :: Regular ) . chain (
1637
1640
match struct_rest {
@@ -1660,12 +1663,13 @@ fn rewrite_struct_lit<'a>(
1660
1663
let rewrite = |item : & StructLitField < ' _ > | match * item {
1661
1664
StructLitField :: Regular ( field) => {
1662
1665
// The 1 taken from the v_budget is for the comma.
1663
- rewrite_field ( context, field, v_shape. sub_width ( 1 ) ?, 0 )
1666
+ let v_shape = v_shape. sub_width ( 1 ) ?;
1667
+ rewrite_field ( context, field, v_shape, 0 ) . ok ( )
1664
1668
}
1665
1669
StructLitField :: Base ( expr) => {
1666
1670
// 2 = ..
1667
- expr . rewrite ( context , v_shape. offset_left ( 2 ) ?)
1668
- . map ( |s| format ! ( "..{}" , s) )
1671
+ let v_shape = v_shape . sub_width ( 2 ) ?;
1672
+ expr . rewrite ( context , v_shape ) . map ( |s| format ! ( "..{}" , s) )
1669
1673
}
1670
1674
StructLitField :: Rest ( _) => Some ( ".." . to_owned ( ) ) ,
1671
1675
} ;
@@ -1697,12 +1701,12 @@ fn rewrite_struct_lit<'a>(
1697
1701
force_no_trailing_comma || has_base_or_rest || !context. use_block_indent ( ) ,
1698
1702
) ;
1699
1703
1700
- write_list ( & item_vec, & fmt) ?
1704
+ write_list ( & item_vec, & fmt) . unknown_error ( ) ?
1701
1705
} ;
1702
1706
1703
1707
let fields_str =
1704
1708
wrap_struct_field ( context, attrs, & fields_str, shape, v_shape, one_line_width) ?;
1705
- Some ( format ! ( "{path_str} {{{fields_str}}}" ) )
1709
+ Ok ( format ! ( "{path_str} {{{fields_str}}}" ) )
1706
1710
1707
1711
// FIXME if context.config.indent_style() == Visual, but we run out
1708
1712
// of space, we should fall back to BlockIndent.
@@ -1715,7 +1719,7 @@ pub(crate) fn wrap_struct_field(
1715
1719
shape : Shape ,
1716
1720
nested_shape : Shape ,
1717
1721
one_line_width : usize ,
1718
- ) -> Option < String > {
1722
+ ) -> RewriteResult {
1719
1723
let should_vertical = context. config . indent_style ( ) == IndentStyle :: Block
1720
1724
&& ( fields_str. contains ( '\n' )
1721
1725
|| !context. config . struct_lit_single_line ( )
@@ -1724,21 +1728,21 @@ pub(crate) fn wrap_struct_field(
1724
1728
let inner_attrs = & inner_attributes ( attrs) ;
1725
1729
if inner_attrs. is_empty ( ) {
1726
1730
if should_vertical {
1727
- Some ( format ! (
1731
+ Ok ( format ! (
1728
1732
"{}{}{}" ,
1729
1733
nested_shape. indent. to_string_with_newline( context. config) ,
1730
1734
fields_str,
1731
1735
shape. indent. to_string_with_newline( context. config)
1732
1736
) )
1733
1737
} else {
1734
1738
// One liner or visual indent.
1735
- Some ( format ! ( " {fields_str} " ) )
1739
+ Ok ( format ! ( " {fields_str} " ) )
1736
1740
}
1737
1741
} else {
1738
- Some ( format ! (
1742
+ Ok ( format ! (
1739
1743
"{}{}{}{}{}" ,
1740
1744
nested_shape. indent. to_string_with_newline( context. config) ,
1741
- inner_attrs. rewrite ( context, shape) ?,
1745
+ inner_attrs. rewrite_result ( context, shape) ?,
1742
1746
nested_shape. indent. to_string_with_newline( context. config) ,
1743
1747
fields_str,
1744
1748
shape. indent. to_string_with_newline( context. config)
@@ -1755,38 +1759,40 @@ pub(crate) fn rewrite_field(
1755
1759
field : & ast:: ExprField ,
1756
1760
shape : Shape ,
1757
1761
prefix_max_width : usize ,
1758
- ) -> Option < String > {
1762
+ ) -> RewriteResult {
1759
1763
if contains_skip ( & field. attrs ) {
1760
- return Some ( context. snippet ( field. span ( ) ) . to_owned ( ) ) ;
1764
+ return Ok ( context. snippet ( field. span ( ) ) . to_owned ( ) ) ;
1761
1765
}
1762
- let mut attrs_str = field. attrs . rewrite ( context, shape) ?;
1766
+ let mut attrs_str = field. attrs . rewrite_result ( context, shape) ?;
1763
1767
if !attrs_str. is_empty ( ) {
1764
1768
attrs_str. push_str ( & shape. indent . to_string_with_newline ( context. config ) ) ;
1765
1769
} ;
1766
1770
let name = context. snippet ( field. ident . span ) ;
1767
1771
if field. is_shorthand {
1768
- Some ( attrs_str + name)
1772
+ Ok ( attrs_str + name)
1769
1773
} else {
1770
1774
let mut separator = String :: from ( struct_lit_field_separator ( context. config ) ) ;
1771
1775
for _ in 0 ..prefix_max_width. saturating_sub ( name. len ( ) ) {
1772
1776
separator. push ( ' ' ) ;
1773
1777
}
1774
1778
let overhead = name. len ( ) + separator. len ( ) ;
1775
- let expr_shape = shape. offset_left ( overhead) ?;
1776
- let expr = field. expr . rewrite ( context, expr_shape) ;
1779
+ let expr_shape = shape
1780
+ . offset_left ( overhead)
1781
+ . max_width_error ( shape. width , field. span ) ?;
1782
+ let expr = field. expr . rewrite_result ( context, expr_shape) ;
1777
1783
let is_lit = matches ! ( field. expr. kind, ast:: ExprKind :: Lit ( _) ) ;
1778
1784
match expr {
1779
- Some ( ref e)
1785
+ Ok ( ref e)
1780
1786
if !is_lit && e. as_str ( ) == name && context. config . use_field_init_shorthand ( ) =>
1781
1787
{
1782
- Some ( attrs_str + name)
1788
+ Ok ( attrs_str + name)
1783
1789
}
1784
- Some ( e) => Some ( format ! ( "{attrs_str}{name}{separator}{e}" ) ) ,
1785
- None => {
1790
+ Ok ( e) => Ok ( format ! ( "{attrs_str}{name}{separator}{e}" ) ) ,
1791
+ Err ( _ ) => {
1786
1792
let expr_offset = shape. indent . block_indent ( context. config ) ;
1787
1793
let expr = field
1788
1794
. expr
1789
- . rewrite ( context, Shape :: indented ( expr_offset, context. config ) ) ;
1795
+ . rewrite_result ( context, Shape :: indented ( expr_offset, context. config ) ) ;
1790
1796
expr. map ( |s| {
1791
1797
format ! (
1792
1798
"{}{}:\n {}{}" ,
0 commit comments