@@ -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,25 @@ 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+ . and_then ( |s| s. offset_left ( pipe_offset) )
282+ . max_width_error ( shape. width , arm. span ) ?
272283 }
273284 _ => {
274285 // 5 = ` => {`
275- shape. sub_width ( 5 ) ?. offset_left ( pipe_offset) ?
286+ shape
287+ . sub_width ( 5 )
288+ . and_then ( |s| s. offset_left ( pipe_offset) )
289+ . max_width_error ( shape. width , arm. span ) ?
276290 }
277291 } ;
278- let pats_str = arm. pat . rewrite ( context, pat_shape) ?;
292+ let pats_str = arm. pat . rewrite_result ( context, pat_shape) ?;
279293
280294 // Guard
281295 let block_like_pat = trimmed_last_line_width ( & pats_str) <= context. config . tab_spaces ( ) ;
@@ -295,12 +309,16 @@ fn rewrite_match_arm(
295309 missing_span,
296310 shape,
297311 false ,
298- ) ?;
312+ )
313+ . unknown_error ( ) ?;
299314
300- let arrow_span = mk_sp ( arm. pat . span . hi ( ) , arm. body . as_ref ( ) ?. span ( ) . lo ( ) ) ;
315+ let arrow_span = mk_sp (
316+ arm. pat . span . hi ( ) ,
317+ arm. body . as_ref ( ) . unknown_error ( ) ?. span ( ) . lo ( ) ,
318+ ) ;
301319 rewrite_match_body (
302320 context,
303- arm. body . as_ref ( ) ?,
321+ arm. body . as_ref ( ) . unknown_error ( ) ?,
304322 & lhs_str,
305323 shape,
306324 guard_str. contains ( '\n' ) ,
@@ -381,7 +399,7 @@ fn rewrite_match_body(
381399 has_guard : bool ,
382400 arrow_span : Span ,
383401 is_last : bool ,
384- ) -> Option < String > {
402+ ) -> RewriteResult {
385403 let ( extend, body) = flatten_arm_body (
386404 context,
387405 body,
@@ -402,7 +420,7 @@ fn rewrite_match_body(
402420 _ => " " ,
403421 } ;
404422
405- Some ( format ! ( "{} =>{}{}{}" , pats_str, block_sep, body_str, comma) )
423+ Ok ( format ! ( "{} =>{}{}{}" , pats_str, block_sep, body_str, comma) )
406424 } ;
407425
408426 let next_line_indent = if !is_block || is_empty_block {
@@ -429,7 +447,7 @@ fn rewrite_match_body(
429447 if comment_str. is_empty ( ) {
430448 String :: new ( )
431449 } else {
432- rewrite_comment ( comment_str, false , shape, context. config ) ?
450+ rewrite_comment ( comment_str, false , shape, context. config ) . unknown_error ( ) ?
433451 }
434452 } ;
435453
@@ -446,7 +464,7 @@ fn rewrite_match_body(
446464 result. push_str ( & nested_indent_str) ;
447465 result. push_str ( body_str) ;
448466 result. push_str ( comma) ;
449- return Some ( result) ;
467+ return Ok ( result) ;
450468 }
451469
452470 let indent_str = shape. indent . to_string_with_newline ( context. config ) ;
@@ -489,7 +507,7 @@ fn rewrite_match_body(
489507 result. push_str ( & block_sep) ;
490508 result. push_str ( body_str) ;
491509 result. push_str ( & body_suffix) ;
492- Some ( result)
510+ Ok ( result)
493511 } ;
494512
495513 // Let's try and get the arm body on the same line as the condition.
@@ -539,7 +557,7 @@ fn rewrite_match_body(
539557 combine_next_line_body ( next_line_str)
540558 }
541559 ( None , Some ( ref next_line_str) ) => combine_next_line_body ( next_line_str) ,
542- ( None , None ) => None ,
560+ ( None , None ) => Err ( RewriteError :: Unknown ) ,
543561 ( Some ( ref orig_str) , _) => combine_orig_body ( orig_str) ,
544562 }
545563}
@@ -553,7 +571,7 @@ fn rewrite_guard(
553571 // the arm (excludes offset).
554572 pattern_width : usize ,
555573 multiline_pattern : bool ,
556- ) -> Option < String > {
574+ ) -> RewriteResult {
557575 if let Some ( ref guard) = * guard {
558576 // First try to fit the guard string on the same line as the pattern.
559577 // 4 = ` if `, 5 = ` => {`
@@ -562,9 +580,9 @@ fn rewrite_guard(
562580 . and_then ( |s| s. sub_width ( 5 ) ) ;
563581 if !multiline_pattern {
564582 if let Some ( cond_shape) = cond_shape {
565- if let Some ( cond_str) = guard. rewrite ( context, cond_shape) {
583+ if let Ok ( cond_str) = guard. rewrite_result ( context, cond_shape) {
566584 if !cond_str. contains ( '\n' ) || pattern_width <= context. config . tab_spaces ( ) {
567- return Some ( format ! ( " if {cond_str}" ) ) ;
585+ return Ok ( format ! ( " if {cond_str}" ) ) ;
568586 }
569587 }
570588 }
@@ -574,20 +592,16 @@ fn rewrite_guard(
574592 // 3 = `if `, 5 = ` => {`
575593 let cond_shape = Shape :: indented ( shape. indent . block_indent ( context. config ) , context. config )
576594 . 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
595+ . and_then ( |s| s. sub_width ( 5 ) )
596+ . max_width_error ( shape. width , guard. span ) ?;
597+ let cond_str = guard. rewrite_result ( context, cond_shape) ?;
598+ Ok ( format ! (
599+ "{}if {}" ,
600+ cond_shape. indent. to_string_with_newline( context. config) ,
601+ cond_str
602+ ) )
589603 } else {
590- Some ( String :: new ( ) )
604+ Ok ( String :: new ( ) )
591605 }
592606}
593607
0 commit comments