@@ -13,7 +13,7 @@ use crate::expr::{
1313 ExprType , RhsTactics ,
1414} ;
1515use crate :: lists:: { itemize_list, write_list, ListFormatting } ;
16- use crate :: rewrite:: { Rewrite , RewriteContext } ;
16+ use crate :: rewrite:: { Rewrite , RewriteContext , RewriteError , RewriteErrorExt , RewriteResult } ;
1717use crate :: shape:: Shape ;
1818use crate :: source_map:: SpanUtils ;
1919use crate :: spanned:: Spanned ;
@@ -55,6 +55,10 @@ impl<'a> Spanned for ArmWrapper<'a> {
5555
5656impl < ' a > Rewrite for ArmWrapper < ' a > {
5757 fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
58+ self . rewrite_result ( context, shape) . ok ( )
59+ }
60+
61+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
5862 rewrite_match_arm (
5963 context,
6064 self . arm ,
@@ -73,7 +77,7 @@ pub(crate) fn rewrite_match(
7377 span : Span ,
7478 attrs : & [ ast:: Attribute ] ,
7579 match_kind : MatchKind ,
76- ) -> Option < String > {
80+ ) -> RewriteResult {
7781 // Do not take the rhs overhead from the upper expressions into account
7882 // when rewriting match condition.
7983 let cond_shape = Shape {
@@ -82,10 +86,14 @@ pub(crate) fn rewrite_match(
8286 } ;
8387 // 6 = `match `
8488 let cond_shape = match context. config . indent_style ( ) {
85- IndentStyle :: Visual => cond_shape. shrink_left ( 6 ) ?,
86- IndentStyle :: Block => cond_shape. offset_left ( 6 ) ?,
89+ IndentStyle :: Visual => cond_shape
90+ . shrink_left ( 6 )
91+ . max_width_error ( shape. width , span) ?,
92+ IndentStyle :: Block => cond_shape
93+ . offset_left ( 6 )
94+ . max_width_error ( shape. width , span) ?,
8795 } ;
88- let cond_str = cond. rewrite ( context, cond_shape) ?;
96+ let cond_str = cond. rewrite_result ( context, cond_shape) ?;
8997 let alt_block_sep = & shape. indent . to_string_with_newline ( context. config ) ;
9098 let block_sep = match context. config . control_brace_style ( ) {
9199 ControlBraceStyle :: AlwaysNextLine => alt_block_sep,
@@ -109,7 +117,7 @@ pub(crate) fn rewrite_match(
109117 _ => shape. block_indent ( context. config . tab_spaces ( ) ) ,
110118 } ;
111119 inner_attrs
112- . rewrite ( context, shape)
120+ . rewrite_result ( context, shape)
113121 . map ( |s| format ! ( "{}{}\n " , nested_indent_str, s) ) ?
114122 } ;
115123
@@ -129,16 +137,16 @@ pub(crate) fn rewrite_match(
129137 if arms. is_empty ( ) {
130138 let snippet = context. snippet ( mk_sp ( open_brace_pos, span. hi ( ) - BytePos ( 1 ) ) ) ;
131139 if snippet. trim ( ) . is_empty ( ) {
132- Some ( format ! ( "match {cond_str} {{}}" ) )
140+ Ok ( format ! ( "match {cond_str} {{}}" ) )
133141 } else {
134142 // Empty match with comments or inner attributes? We are not going to bother, sorry ;)
135- Some ( context. snippet ( span) . to_owned ( ) )
143+ Ok ( context. snippet ( span) . to_owned ( ) )
136144 }
137145 } else {
138146 let span_after_cond = mk_sp ( cond. span . hi ( ) , span. hi ( ) ) ;
139147
140148 match match_kind {
141- MatchKind :: Prefix => Some ( format ! (
149+ MatchKind :: Prefix => Ok ( format ! (
142150 "match {}{}{{\n {}{}{}\n {}}}" ,
143151 cond_str,
144152 block_sep,
@@ -147,7 +155,7 @@ pub(crate) fn rewrite_match(
147155 rewrite_match_arms( context, arms, shape, span_after_cond, open_brace_pos) ?,
148156 shape. indent. to_string( context. config) ,
149157 ) ) ,
150- MatchKind :: Postfix => Some ( format ! (
158+ MatchKind :: Postfix => Ok ( format ! (
151159 "{}.match{}{{\n {}{}{}\n {}}}" ,
152160 cond_str,
153161 block_sep,
@@ -197,7 +205,7 @@ fn rewrite_match_arms(
197205 shape : Shape ,
198206 span : Span ,
199207 open_brace_pos : BytePos ,
200- ) -> Option < String > {
208+ ) -> RewriteResult {
201209 let arm_shape = shape
202210 . block_indent ( context. config . tab_spaces ( ) )
203211 . with_max_width ( context. config ) ;
@@ -228,7 +236,7 @@ fn rewrite_match_arms(
228236 . separator ( "" )
229237 . preserve_newline ( true ) ;
230238
231- write_list ( & arms_vec, & fmt)
239+ write_list ( & arms_vec, & fmt) . unknown_error ( )
232240}
233241
234242fn rewrite_match_arm (
@@ -237,19 +245,19 @@ fn rewrite_match_arm(
237245 shape : Shape ,
238246 is_last : bool ,
239247 has_leading_pipe : bool ,
240- ) -> Option < String > {
248+ ) -> RewriteResult {
241249 let ( missing_span, attrs_str) = if !arm. attrs . is_empty ( ) {
242250 if contains_skip ( & arm. attrs ) {
243- let ( _, body) = flatten_arm_body ( context, arm. body . as_deref ( ) ?, None ) ;
251+ let ( _, body) = flatten_arm_body ( context, arm. body . as_deref ( ) . unknown_error ( ) ?, None ) ;
244252 // `arm.span()` does not include trailing comma, add it manually.
245- return Some ( format ! (
253+ return Ok ( format ! (
246254 "{}{}" ,
247255 context. snippet( arm. span( ) ) ,
248256 arm_comma( context. config, body, is_last) ,
249257 ) ) ;
250258 }
251259 let missing_span = mk_sp ( arm. attrs [ arm. attrs . len ( ) - 1 ] . span . hi ( ) , arm. pat . span . lo ( ) ) ;
252- ( missing_span, arm. attrs . rewrite ( context, shape) ?)
260+ ( missing_span, arm. attrs . rewrite_result ( context, shape) ?)
253261 } else {
254262 ( mk_sp ( arm. span ( ) . lo ( ) , arm. span ( ) . lo ( ) ) , String :: new ( ) )
255263 } ;
@@ -263,19 +271,27 @@ fn rewrite_match_arm(
263271 } ;
264272
265273 // Patterns
266- let pat_shape = match & arm. body . as_ref ( ) ?. kind {
274+ let pat_shape = match & arm. body . as_ref ( ) . unknown_error ( ) ?. kind {
267275 ast:: ExprKind :: Block ( _, Some ( label) ) => {
268276 // Some block with a label ` => 'label: {`
269277 // 7 = ` => : {`
270278 let label_len = label. ident . as_str ( ) . len ( ) ;
271- shape. sub_width ( 7 + label_len) ?. offset_left ( pipe_offset) ?
279+ shape
280+ . sub_width ( 7 + label_len)
281+ . max_width_error ( shape. width , arm. span ) ?
282+ . offset_left ( pipe_offset)
283+ . max_width_error ( shape. width , arm. span ) ?
272284 }
273285 _ => {
274286 // 5 = ` => {`
275- shape. sub_width ( 5 ) ?. offset_left ( pipe_offset) ?
287+ shape
288+ . sub_width ( 5 )
289+ . max_width_error ( shape. width , arm. span ) ?
290+ . offset_left ( pipe_offset)
291+ . max_width_error ( shape. width , arm. span ) ?
276292 }
277293 } ;
278- let pats_str = arm. pat . rewrite ( context, pat_shape) ?;
294+ let pats_str = arm. pat . rewrite_result ( context, pat_shape) ?;
279295
280296 // Guard
281297 let block_like_pat = trimmed_last_line_width ( & pats_str) <= context. config . tab_spaces ( ) ;
@@ -295,12 +311,16 @@ fn rewrite_match_arm(
295311 missing_span,
296312 shape,
297313 false ,
298- ) ?;
314+ )
315+ . unknown_error ( ) ?;
299316
300- let arrow_span = mk_sp ( arm. pat . span . hi ( ) , arm. body . as_ref ( ) ?. span ( ) . lo ( ) ) ;
317+ let arrow_span = mk_sp (
318+ arm. pat . span . hi ( ) ,
319+ arm. body . as_ref ( ) . unknown_error ( ) ?. span ( ) . lo ( ) ,
320+ ) ;
301321 rewrite_match_body (
302322 context,
303- arm. body . as_ref ( ) ?,
323+ arm. body . as_ref ( ) . unknown_error ( ) ?,
304324 & lhs_str,
305325 shape,
306326 guard_str. contains ( '\n' ) ,
@@ -381,7 +401,7 @@ fn rewrite_match_body(
381401 has_guard : bool ,
382402 arrow_span : Span ,
383403 is_last : bool ,
384- ) -> Option < String > {
404+ ) -> RewriteResult {
385405 let ( extend, body) = flatten_arm_body (
386406 context,
387407 body,
@@ -402,7 +422,7 @@ fn rewrite_match_body(
402422 _ => " " ,
403423 } ;
404424
405- Some ( format ! ( "{} =>{}{}{}" , pats_str, block_sep, body_str, comma) )
425+ Ok ( format ! ( "{} =>{}{}{}" , pats_str, block_sep, body_str, comma) )
406426 } ;
407427
408428 let next_line_indent = if !is_block || is_empty_block {
@@ -429,7 +449,7 @@ fn rewrite_match_body(
429449 if comment_str. is_empty ( ) {
430450 String :: new ( )
431451 } else {
432- rewrite_comment ( comment_str, false , shape, context. config ) ?
452+ rewrite_comment ( comment_str, false , shape, context. config ) . unknown_error ( ) ?
433453 }
434454 } ;
435455
@@ -446,7 +466,7 @@ fn rewrite_match_body(
446466 result. push_str ( & nested_indent_str) ;
447467 result. push_str ( body_str) ;
448468 result. push_str ( comma) ;
449- return Some ( result) ;
469+ return Ok ( result) ;
450470 }
451471
452472 let indent_str = shape. indent . to_string_with_newline ( context. config ) ;
@@ -489,7 +509,7 @@ fn rewrite_match_body(
489509 result. push_str ( & block_sep) ;
490510 result. push_str ( body_str) ;
491511 result. push_str ( & body_suffix) ;
492- Some ( result)
512+ Ok ( result)
493513 } ;
494514
495515 // Let's try and get the arm body on the same line as the condition.
@@ -539,7 +559,7 @@ fn rewrite_match_body(
539559 combine_next_line_body ( next_line_str)
540560 }
541561 ( None , Some ( ref next_line_str) ) => combine_next_line_body ( next_line_str) ,
542- ( None , None ) => None ,
562+ ( None , None ) => Err ( RewriteError :: Unknown ) ,
543563 ( Some ( ref orig_str) , _) => combine_orig_body ( orig_str) ,
544564 }
545565}
@@ -553,7 +573,7 @@ fn rewrite_guard(
553573 // the arm (excludes offset).
554574 pattern_width : usize ,
555575 multiline_pattern : bool ,
556- ) -> Option < String > {
576+ ) -> RewriteResult {
557577 if let Some ( ref guard) = * guard {
558578 // First try to fit the guard string on the same line as the pattern.
559579 // 4 = ` if `, 5 = ` => {`
@@ -562,9 +582,9 @@ fn rewrite_guard(
562582 . and_then ( |s| s. sub_width ( 5 ) ) ;
563583 if !multiline_pattern {
564584 if let Some ( cond_shape) = cond_shape {
565- if let Some ( cond_str) = guard. rewrite ( context, cond_shape) {
585+ if let Ok ( cond_str) = guard. rewrite_result ( context, cond_shape) {
566586 if !cond_str. contains ( '\n' ) || pattern_width <= context. config . tab_spaces ( ) {
567- return Some ( format ! ( " if {cond_str}" ) ) ;
587+ return Ok ( format ! ( " if {cond_str}" ) ) ;
568588 }
569589 }
570590 }
@@ -574,20 +594,16 @@ fn rewrite_guard(
574594 // 3 = `if `, 5 = ` => {`
575595 let cond_shape = Shape :: indented ( shape. indent . block_indent ( context. config ) , context. config )
576596 . offset_left ( 3 )
577- . and_then ( |s| s. sub_width ( 5 ) ) ;
578- if let Some ( cond_shape) = cond_shape {
579- if let Some ( cond_str) = guard. rewrite ( context, cond_shape) {
580- return Some ( format ! (
581- "{}if {}" ,
582- cond_shape. indent. to_string_with_newline( context. config) ,
583- cond_str
584- ) ) ;
585- }
586- }
587-
588- None
597+ . and_then ( |s| s. sub_width ( 5 ) )
598+ . max_width_error ( shape. width , guard. span ) ?;
599+ let cond_str = guard. rewrite_result ( context, cond_shape) ?;
600+ Ok ( format ! (
601+ "{}if {}" ,
602+ cond_shape. indent. to_string_with_newline( context. config) ,
603+ cond_str
604+ ) )
589605 } else {
590- Some ( String :: new ( ) )
606+ Ok ( String :: new ( ) )
591607 }
592608}
593609
0 commit comments