@@ -19,7 +19,7 @@ use crate::config::{Edition, IndentStyle, StyleEdition};
1919use crate :: lists:: {
2020 definitive_tactic, itemize_list, write_list, ListFormatting , ListItem , Separator ,
2121} ;
22- use crate :: rewrite:: { Rewrite , RewriteContext } ;
22+ use crate :: rewrite:: { Rewrite , RewriteContext , RewriteErrorExt , RewriteResult } ;
2323use crate :: shape:: Shape ;
2424use crate :: source_map:: SpanUtils ;
2525use crate :: spanned:: Spanned ;
@@ -44,7 +44,8 @@ impl<'a> FmtVisitor<'a> {
4444 Some ( item. span . lo ( ) ) ,
4545 Some ( item. attrs . clone ( ) ) ,
4646 )
47- . rewrite_top_level ( & self . get_context ( ) , shape) ;
47+ . rewrite_top_level ( & self . get_context ( ) , shape)
48+ . ok ( ) ;
4849 match rw {
4950 Some ( ref s) if s. is_empty ( ) => {
5051 // Format up to last newline
@@ -331,12 +332,17 @@ impl UseTree {
331332 & self ,
332333 context : & RewriteContext < ' _ > ,
333334 shape : Shape ,
334- ) -> Option < String > {
335+ ) -> RewriteResult {
335336 let vis = self . visibility . as_ref ( ) . map_or ( Cow :: from ( "" ) , |vis| {
336337 crate :: utils:: format_visibility ( context, vis)
337338 } ) ;
338339 let use_str = self
339- . rewrite ( context, shape. offset_left ( vis. len ( ) ) ?)
340+ . rewrite_result (
341+ context,
342+ shape
343+ . offset_left ( vis. len ( ) )
344+ . max_width_error ( shape. width , self . span ( ) ) ?,
345+ )
340346 . map ( |s| {
341347 if s. is_empty ( ) {
342348 s
@@ -346,8 +352,8 @@ impl UseTree {
346352 } ) ?;
347353 match self . attrs {
348354 Some ( ref attrs) if !attrs. is_empty ( ) => {
349- let attr_str = attrs. rewrite ( context, shape) ?;
350- let lo = attrs. last ( ) . as_ref ( ) ?. span . hi ( ) ;
355+ let attr_str = attrs. rewrite_result ( context, shape) ?;
356+ let lo = attrs. last ( ) . unknown_error ( ) ?. span . hi ( ) ;
351357 let hi = self . span . lo ( ) ;
352358 let span = mk_sp ( lo, hi) ;
353359
@@ -367,9 +373,8 @@ impl UseTree {
367373 shape,
368374 allow_extend,
369375 )
370- . ok ( )
371376 }
372- _ => Some ( use_str) ,
377+ _ => Ok ( use_str) ,
373378 }
374379 }
375380
@@ -1007,21 +1012,24 @@ fn rewrite_nested_use_tree(
10071012 context : & RewriteContext < ' _ > ,
10081013 use_tree_list : & [ UseTree ] ,
10091014 shape : Shape ,
1010- ) -> Option < String > {
1015+ ) -> RewriteResult {
10111016 let mut list_items = Vec :: with_capacity ( use_tree_list. len ( ) ) ;
10121017 let nested_shape = match context. config . imports_indent ( ) {
10131018 IndentStyle :: Block => shape
10141019 . block_indent ( context. config . tab_spaces ( ) )
10151020 . with_max_width ( context. config )
1016- . sub_width ( 1 ) ?,
1021+ . sub_width ( 1 )
1022+ . unknown_error ( ) ?,
10171023 IndentStyle :: Visual => shape. visual_indent ( 0 ) ,
10181024 } ;
10191025 for use_tree in use_tree_list {
10201026 if let Some ( mut list_item) = use_tree. list_item . clone ( ) {
10211027 list_item. item = use_tree. rewrite_result ( context, nested_shape) ;
10221028 list_items. push ( list_item) ;
10231029 } else {
1024- list_items. push ( ListItem :: from_str ( use_tree. rewrite ( context, nested_shape) ?) ) ;
1030+ list_items. push ( ListItem :: from_str (
1031+ use_tree. rewrite_result ( context, nested_shape) ?,
1032+ ) ) ;
10251033 }
10261034 }
10271035 let has_nested_list = use_tree_list. iter ( ) . any ( |use_segment| {
@@ -1057,7 +1065,7 @@ fn rewrite_nested_use_tree(
10571065 . preserve_newline ( true )
10581066 . nested ( has_nested_list) ;
10591067
1060- let list_str = write_list ( & list_items, & fmt) . ok ( ) ?;
1068+ let list_str = write_list ( & list_items, & fmt) ?;
10611069
10621070 let result = if ( list_str. contains ( '\n' )
10631071 || list_str. len ( ) > remaining_width
@@ -1074,12 +1082,16 @@ fn rewrite_nested_use_tree(
10741082 format ! ( "{{{list_str}}}" )
10751083 } ;
10761084
1077- Some ( result)
1085+ Ok ( result)
10781086}
10791087
10801088impl Rewrite for UseSegment {
10811089 fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
1082- Some ( match self . kind {
1090+ self . rewrite_result ( context, shape) . ok ( )
1091+ }
1092+
1093+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
1094+ Ok ( match self . kind {
10831095 UseSegmentKind :: Ident ( ref ident, Some ( ref rename) ) => {
10841096 format ! ( "{ident} as {rename}" )
10851097 }
@@ -1091,31 +1103,42 @@ impl Rewrite for UseSegment {
10911103 UseSegmentKind :: Crate ( Some ( ref rename) ) => format ! ( "crate as {rename}" ) ,
10921104 UseSegmentKind :: Crate ( None ) => "crate" . to_owned ( ) ,
10931105 UseSegmentKind :: Glob => "*" . to_owned ( ) ,
1094- UseSegmentKind :: List ( ref use_tree_list) => rewrite_nested_use_tree (
1095- context,
1096- use_tree_list,
1097- // 1 = "{" and "}"
1098- shape. offset_left ( 1 ) ?. sub_width ( 1 ) ?,
1099- ) ?,
1106+ UseSegmentKind :: List ( ref use_tree_list) => {
1107+ rewrite_nested_use_tree (
1108+ context,
1109+ use_tree_list,
1110+ // 1 = "{" and "}"
1111+ shape
1112+ . offset_left ( 1 )
1113+ . and_then ( |s| s. sub_width ( 1 ) )
1114+ . unknown_error ( ) ?,
1115+ ) ?
1116+ }
11001117 } )
11011118 }
11021119}
11031120
11041121impl Rewrite for UseTree {
1122+ fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
1123+ self . rewrite_result ( context, shape) . ok ( )
1124+ }
1125+
11051126 // This does NOT format attributes and visibility or add a trailing `;`.
1106- fn rewrite ( & self , context : & RewriteContext < ' _ > , mut shape : Shape ) -> Option < String > {
1127+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , mut shape : Shape ) -> RewriteResult {
11071128 let mut result = String :: with_capacity ( 256 ) ;
11081129 let mut iter = self . path . iter ( ) . peekable ( ) ;
11091130 while let Some ( segment) = iter. next ( ) {
1110- let segment_str = segment. rewrite ( context, shape) ?;
1131+ let segment_str = segment. rewrite_result ( context, shape) ?;
11111132 result. push_str ( & segment_str) ;
11121133 if iter. peek ( ) . is_some ( ) {
11131134 result. push_str ( "::" ) ;
11141135 // 2 = "::"
1115- shape = shape. offset_left ( 2 + segment_str. len ( ) ) ?;
1136+ shape = shape
1137+ . offset_left ( 2 + segment_str. len ( ) )
1138+ . max_width_error ( shape. width , self . span ( ) ) ?;
11161139 }
11171140 }
1118- Some ( result)
1141+ Ok ( result)
11191142 }
11201143}
11211144
0 commit comments