@@ -128,6 +128,7 @@ pub(crate) fn format_expr(
128128 expr. span ,
129129 shape,
130130 )
131+ . ok ( )
131132 }
132133 ast:: ExprKind :: Tup ( ref items) => {
133134 rewrite_tuple ( context, items. iter ( ) , expr. span , shape, items. len ( ) == 1 )
@@ -1603,7 +1604,7 @@ fn rewrite_struct_lit<'a>(
16031604 attrs : & [ ast:: Attribute ] ,
16041605 span : Span ,
16051606 shape : Shape ,
1606- ) -> Option < String > {
1607+ ) -> RewriteResult {
16071608 debug ! ( "rewrite_struct_lit: shape {:?}" , shape) ;
16081609
16091610 enum StructLitField < ' a > {
@@ -1613,20 +1614,21 @@ fn rewrite_struct_lit<'a>(
16131614 }
16141615
16151616 // 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) ?;
16181619
16191620 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} {{}}" ) ) ,
16211622 ast:: StructRest :: Rest ( _) if fields. is_empty ( ) => {
1622- return Some ( format ! ( "{path_str} {{ .. }}" ) ) ;
1623+ return Ok ( format ! ( "{path_str} {{ .. }}" ) ) ;
16231624 }
16241625 ast:: StructRest :: Rest ( _) | ast:: StructRest :: Base ( _) => true ,
16251626 _ => false ,
16261627 } ;
16271628
16281629 // 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) ?;
16301632
16311633 let one_line_width = h_shape. map_or ( 0 , |shape| shape. width ) ;
16321634 let body_lo = context. snippet_provider . span_after ( span, "{" ) ;
@@ -1639,7 +1641,8 @@ fn rewrite_struct_lit<'a>(
16391641 v_shape,
16401642 mk_sp ( body_lo, span. hi ( ) ) ,
16411643 one_line_width,
1642- ) ?
1644+ )
1645+ . unknown_error ( ) ?
16431646 } else {
16441647 let field_iter = fields. iter ( ) . map ( StructLitField :: Regular ) . chain (
16451648 match struct_rest {
@@ -1668,12 +1671,13 @@ fn rewrite_struct_lit<'a>(
16681671 let rewrite = |item : & StructLitField < ' _ > | match * item {
16691672 StructLitField :: Regular ( field) => {
16701673 // 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 ( )
16721676 }
16731677 StructLitField :: Base ( expr) => {
16741678 // 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) )
16771681 }
16781682 StructLitField :: Rest ( _) => Some ( ".." . to_owned ( ) ) ,
16791683 } ;
@@ -1705,12 +1709,12 @@ fn rewrite_struct_lit<'a>(
17051709 force_no_trailing_comma || has_base_or_rest || !context. use_block_indent ( ) ,
17061710 ) ;
17071711
1708- write_list ( & item_vec, & fmt) ?
1712+ write_list ( & item_vec, & fmt) . unknown_error ( ) ?
17091713 } ;
17101714
17111715 let fields_str =
17121716 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}}}" ) )
17141718
17151719 // FIXME if context.config.indent_style() == Visual, but we run out
17161720 // of space, we should fall back to BlockIndent.
@@ -1723,7 +1727,7 @@ pub(crate) fn wrap_struct_field(
17231727 shape : Shape ,
17241728 nested_shape : Shape ,
17251729 one_line_width : usize ,
1726- ) -> Option < String > {
1730+ ) -> RewriteResult {
17271731 let should_vertical = context. config . indent_style ( ) == IndentStyle :: Block
17281732 && ( fields_str. contains ( '\n' )
17291733 || !context. config . struct_lit_single_line ( )
@@ -1732,21 +1736,21 @@ pub(crate) fn wrap_struct_field(
17321736 let inner_attrs = & inner_attributes ( attrs) ;
17331737 if inner_attrs. is_empty ( ) {
17341738 if should_vertical {
1735- Some ( format ! (
1739+ Ok ( format ! (
17361740 "{}{}{}" ,
17371741 nested_shape. indent. to_string_with_newline( context. config) ,
17381742 fields_str,
17391743 shape. indent. to_string_with_newline( context. config)
17401744 ) )
17411745 } else {
17421746 // One liner or visual indent.
1743- Some ( format ! ( " {fields_str} " ) )
1747+ Ok ( format ! ( " {fields_str} " ) )
17441748 }
17451749 } else {
1746- Some ( format ! (
1750+ Ok ( format ! (
17471751 "{}{}{}{}{}" ,
17481752 nested_shape. indent. to_string_with_newline( context. config) ,
1749- inner_attrs. rewrite ( context, shape) ?,
1753+ inner_attrs. rewrite_result ( context, shape) ?,
17501754 nested_shape. indent. to_string_with_newline( context. config) ,
17511755 fields_str,
17521756 shape. indent. to_string_with_newline( context. config)
@@ -1763,38 +1767,40 @@ pub(crate) fn rewrite_field(
17631767 field : & ast:: ExprField ,
17641768 shape : Shape ,
17651769 prefix_max_width : usize ,
1766- ) -> Option < String > {
1770+ ) -> RewriteResult {
17671771 if contains_skip ( & field. attrs ) {
1768- return Some ( context. snippet ( field. span ( ) ) . to_owned ( ) ) ;
1772+ return Ok ( context. snippet ( field. span ( ) ) . to_owned ( ) ) ;
17691773 }
1770- let mut attrs_str = field. attrs . rewrite ( context, shape) ?;
1774+ let mut attrs_str = field. attrs . rewrite_result ( context, shape) ?;
17711775 if !attrs_str. is_empty ( ) {
17721776 attrs_str. push_str ( & shape. indent . to_string_with_newline ( context. config ) ) ;
17731777 } ;
17741778 let name = context. snippet ( field. ident . span ) ;
17751779 if field. is_shorthand {
1776- Some ( attrs_str + name)
1780+ Ok ( attrs_str + name)
17771781 } else {
17781782 let mut separator = String :: from ( struct_lit_field_separator ( context. config ) ) ;
17791783 for _ in 0 ..prefix_max_width. saturating_sub ( name. len ( ) ) {
17801784 separator. push ( ' ' ) ;
17811785 }
17821786 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) ;
17851791 let is_lit = matches ! ( field. expr. kind, ast:: ExprKind :: Lit ( _) ) ;
17861792 match expr {
1787- Some ( ref e)
1793+ Ok ( ref e)
17881794 if !is_lit && e. as_str ( ) == name && context. config . use_field_init_shorthand ( ) =>
17891795 {
1790- Some ( attrs_str + name)
1796+ Ok ( attrs_str + name)
17911797 }
1792- Some ( e) => Some ( format ! ( "{attrs_str}{name}{separator}{e}" ) ) ,
1793- None => {
1798+ Ok ( e) => Ok ( format ! ( "{attrs_str}{name}{separator}{e}" ) ) ,
1799+ Err ( _ ) => {
17941800 let expr_offset = shape. indent . block_indent ( context. config ) ;
17951801 let expr = field
17961802 . expr
1797- . rewrite ( context, Shape :: indented ( expr_offset, context. config ) ) ;
1803+ . rewrite_result ( context, Shape :: indented ( expr_offset, context. config ) ) ;
17981804 expr. map ( |s| {
17991805 format ! (
18001806 "{}{}:\n {}{}" ,
0 commit comments