@@ -840,7 +840,8 @@ pub(crate) fn format_impl(
840840 where_span_end,
841841 self_ty. span . hi ( ) ,
842842 option,
843- ) ?;
843+ )
844+ . ok ( ) ?;
844845
845846 // If there is no where-clause, we may have missing comments between the trait name and
846847 // the opening brace.
@@ -1231,7 +1232,8 @@ pub(crate) fn format_trait(
12311232 None ,
12321233 pos_before_where,
12331234 option,
1234- ) ?;
1235+ )
1236+ . ok ( ) ?;
12351237 // If the where-clause cannot fit on the same line,
12361238 // put the where-clause on a new line
12371239 if !where_clause_str. contains ( '\n' )
@@ -1336,7 +1338,11 @@ pub(crate) struct TraitAliasBounds<'a> {
13361338
13371339impl < ' a > Rewrite for TraitAliasBounds < ' a > {
13381340 fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
1339- let generic_bounds_str = self . generic_bounds . rewrite ( context, shape) ?;
1341+ self . rewrite_result ( context, shape) . ok ( )
1342+ }
1343+
1344+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
1345+ let generic_bounds_str = self . generic_bounds . rewrite_result ( context, shape) ?;
13401346
13411347 let mut option = WhereClauseOption :: new ( true , WhereClauseSpace :: None ) ;
13421348 option. allow_single_line ( ) ;
@@ -1365,7 +1371,7 @@ impl<'a> Rewrite for TraitAliasBounds<'a> {
13651371 shape. indent . to_string_with_newline ( context. config )
13661372 } ;
13671373
1368- Some ( format ! ( "{generic_bounds_str}{space}{where_str}" ) )
1374+ Ok ( format ! ( "{generic_bounds_str}{space}{where_str}" ) )
13691375 }
13701376}
13711377
@@ -1623,7 +1629,8 @@ fn format_tuple_struct(
16231629 None ,
16241630 body_hi,
16251631 option,
1626- ) ?
1632+ )
1633+ . ok ( ) ?
16271634 }
16281635 None => "" . to_owned ( ) ,
16291636 } ;
@@ -1792,7 +1799,8 @@ fn rewrite_ty<R: Rewrite>(
17921799 None ,
17931800 generics. span . hi ( ) ,
17941801 option,
1795- ) ?;
1802+ )
1803+ . ok ( ) ?;
17961804 result. push_str ( & where_clause_str) ;
17971805
17981806 if let Some ( ty) = rhs {
@@ -2663,7 +2671,8 @@ fn rewrite_fn_base(
26632671 Some ( span. hi ( ) ) ,
26642672 pos_before_where,
26652673 option,
2666- ) ?;
2674+ )
2675+ . ok ( ) ?;
26672676 // If there are neither where-clause nor return type, we may be missing comments between
26682677 // params and `{`.
26692678 if where_clause_str. is_empty ( ) {
@@ -2939,7 +2948,7 @@ fn rewrite_where_clause_rfc_style(
29392948 span_end : Option < BytePos > ,
29402949 span_end_before_where : BytePos ,
29412950 where_clause_option : WhereClauseOption ,
2942- ) -> Option < String > {
2951+ ) -> RewriteResult {
29432952 let ( where_keyword, allow_single_line) = rewrite_where_keyword (
29442953 context,
29452954 predicates,
@@ -2953,8 +2962,9 @@ fn rewrite_where_clause_rfc_style(
29532962 let clause_shape = shape
29542963 . block ( )
29552964 . with_max_width ( context. config )
2956- . block_left ( context. config . tab_spaces ( ) ) ?
2957- . sub_width ( 1 ) ?;
2965+ . block_left ( context. config . tab_spaces ( ) )
2966+ . and_then ( |s| s. sub_width ( 1 ) )
2967+ . max_width_error ( shape. width , where_span) ?;
29582968 let force_single_line = context. config . where_single_line ( )
29592969 && predicates. len ( ) == 1
29602970 && !where_clause_option. veto_single_line ;
@@ -2979,7 +2989,7 @@ fn rewrite_where_clause_rfc_style(
29792989 clause_shape. indent . to_string_with_newline ( context. config )
29802990 } ;
29812991
2982- Some ( format ! ( "{where_keyword}{clause_sep}{preds_str}" ) )
2992+ Ok ( format ! ( "{where_keyword}{clause_sep}{preds_str}" ) )
29832993}
29842994
29852995/// Rewrite `where` and comment around it.
@@ -2990,12 +3000,13 @@ fn rewrite_where_keyword(
29903000 shape : Shape ,
29913001 span_end_before_where : BytePos ,
29923002 where_clause_option : WhereClauseOption ,
2993- ) -> Option < ( String , bool ) > {
3003+ ) -> Result < ( String , bool ) , RewriteError > {
29943004 let block_shape = shape. block ( ) . with_max_width ( context. config ) ;
29953005 // 1 = `,`
29963006 let clause_shape = block_shape
2997- . block_left ( context. config . tab_spaces ( ) ) ?
2998- . sub_width ( 1 ) ?;
3007+ . block_left ( context. config . tab_spaces ( ) )
3008+ . and_then ( |s| s. sub_width ( 1 ) )
3009+ . max_width_error ( block_shape. width , where_span) ?;
29993010
30003011 let comment_separator = |comment : & str , shape : Shape | {
30013012 if comment. is_empty ( ) {
@@ -3026,7 +3037,7 @@ fn rewrite_where_keyword(
30263037 && comment_before. is_empty ( )
30273038 && comment_after. is_empty ( ) ;
30283039
3029- Some ( ( result, allow_single_line) )
3040+ Ok ( ( result, allow_single_line) )
30303041}
30313042
30323043/// Rewrite bounds on a where clause.
@@ -3038,7 +3049,7 @@ fn rewrite_bounds_on_where_clause(
30383049 span_end : Option < BytePos > ,
30393050 where_clause_option : WhereClauseOption ,
30403051 force_single_line : bool ,
3041- ) -> Option < String > {
3052+ ) -> RewriteResult {
30423053 let span_start = predicates[ 0 ] . span ( ) . lo ( ) ;
30433054 // If we don't have the start of the next span, then use the end of the
30443055 // predicates, but that means we miss comments.
@@ -3077,7 +3088,7 @@ fn rewrite_bounds_on_where_clause(
30773088 . tactic ( shape_tactic)
30783089 . trailing_separator ( comma_tactic)
30793090 . preserve_newline ( preserve_newline) ;
3080- write_list ( & items. collect :: < Vec < _ > > ( ) , & fmt) . ok ( )
3091+ write_list ( & items. collect :: < Vec < _ > > ( ) , & fmt)
30813092}
30823093
30833094fn rewrite_where_clause (
@@ -3091,9 +3102,9 @@ fn rewrite_where_clause(
30913102 span_end : Option < BytePos > ,
30923103 span_end_before_where : BytePos ,
30933104 where_clause_option : WhereClauseOption ,
3094- ) -> Option < String > {
3105+ ) -> RewriteResult {
30953106 if predicates. is_empty ( ) {
3096- return Some ( String :: new ( ) ) ;
3107+ return Ok ( String :: new ( ) ) ;
30973108 }
30983109
30993110 if context. config . indent_style ( ) == IndentStyle :: Block {
@@ -3153,7 +3164,7 @@ fn rewrite_where_clause(
31533164 . trailing_separator ( comma_tactic)
31543165 . ends_with_newline ( tactic. ends_with_newline ( context. config . indent_style ( ) ) )
31553166 . preserve_newline ( true ) ;
3156- let preds_str = write_list ( & item_vec, & fmt) . ok ( ) ?;
3167+ let preds_str = write_list ( & item_vec, & fmt) ?;
31573168
31583169 let end_length = if terminator == "{" {
31593170 // If the brace is on the next line we don't need to count it otherwise it needs two
@@ -3171,13 +3182,13 @@ fn rewrite_where_clause(
31713182 || preds_str. contains ( '\n' )
31723183 || shape. indent . width ( ) + " where " . len ( ) + preds_str. len ( ) + end_length > shape. width
31733184 {
3174- Some ( format ! (
3185+ Ok ( format ! (
31753186 "\n {}where {}" ,
31763187 ( shape. indent + extra_indent) . to_string( context. config) ,
31773188 preds_str
31783189 ) )
31793190 } else {
3180- Some ( format ! ( " where {preds_str}" ) )
3191+ Ok ( format ! ( " where {preds_str}" ) )
31813192 }
31823193}
31833194
@@ -3198,15 +3209,14 @@ fn rewrite_comments_before_after_where(
31983209 span_before_where : Span ,
31993210 span_after_where : Span ,
32003211 shape : Shape ,
3201- ) -> Option < ( String , String ) > {
3202- let before_comment = rewrite_missing_comment ( span_before_where, shape, context) . ok ( ) ?;
3212+ ) -> Result < ( String , String ) , RewriteError > {
3213+ let before_comment = rewrite_missing_comment ( span_before_where, shape, context) ?;
32033214 let after_comment = rewrite_missing_comment (
32043215 span_after_where,
32053216 shape. block_indent ( context. config . tab_spaces ( ) ) ,
32063217 context,
3207- )
3208- . ok ( ) ?;
3209- Some ( ( before_comment, after_comment) )
3218+ ) ?;
3219+ Ok ( ( before_comment, after_comment) )
32103220}
32113221
32123222fn format_header (
@@ -3288,7 +3298,8 @@ fn format_generics(
32883298 Some ( span. hi ( ) ) ,
32893299 span_end_before_where,
32903300 option,
3291- ) ?;
3301+ )
3302+ . ok ( ) ?;
32923303 result. push_str ( & where_clause_str) ;
32933304 (
32943305 brace_pos == BracePos :: ForceSameLine || brace_style == BraceStyle :: PreferSameLine ,
0 commit comments