@@ -19,7 +19,7 @@ use crate::config::{Edition, IndentStyle, StyleEdition};
19
19
use crate :: lists:: {
20
20
definitive_tactic, itemize_list, write_list, ListFormatting , ListItem , Separator ,
21
21
} ;
22
- use crate :: rewrite:: { Rewrite , RewriteContext } ;
22
+ use crate :: rewrite:: { Rewrite , RewriteContext , RewriteErrorExt , RewriteResult } ;
23
23
use crate :: shape:: Shape ;
24
24
use crate :: source_map:: SpanUtils ;
25
25
use crate :: spanned:: Spanned ;
@@ -44,7 +44,8 @@ impl<'a> FmtVisitor<'a> {
44
44
Some ( item. span . lo ( ) ) ,
45
45
Some ( item. attrs . clone ( ) ) ,
46
46
)
47
- . rewrite_top_level ( & self . get_context ( ) , shape) ;
47
+ . rewrite_top_level ( & self . get_context ( ) , shape)
48
+ . ok ( ) ;
48
49
match rw {
49
50
Some ( ref s) if s. is_empty ( ) => {
50
51
// Format up to last newline
@@ -331,12 +332,17 @@ impl UseTree {
331
332
& self ,
332
333
context : & RewriteContext < ' _ > ,
333
334
shape : Shape ,
334
- ) -> Option < String > {
335
+ ) -> RewriteResult {
335
336
let vis = self . visibility . as_ref ( ) . map_or ( Cow :: from ( "" ) , |vis| {
336
337
crate :: utils:: format_visibility ( context, vis)
337
338
} ) ;
338
339
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
+ )
340
346
. map ( |s| {
341
347
if s. is_empty ( ) {
342
348
s
@@ -346,8 +352,8 @@ impl UseTree {
346
352
} ) ?;
347
353
match self . attrs {
348
354
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 ( ) ;
351
357
let hi = self . span . lo ( ) ;
352
358
let span = mk_sp ( lo, hi) ;
353
359
@@ -367,9 +373,8 @@ impl UseTree {
367
373
shape,
368
374
allow_extend,
369
375
)
370
- . ok ( )
371
376
}
372
- _ => Some ( use_str) ,
377
+ _ => Ok ( use_str) ,
373
378
}
374
379
}
375
380
@@ -1007,21 +1012,24 @@ fn rewrite_nested_use_tree(
1007
1012
context : & RewriteContext < ' _ > ,
1008
1013
use_tree_list : & [ UseTree ] ,
1009
1014
shape : Shape ,
1010
- ) -> Option < String > {
1015
+ ) -> RewriteResult {
1011
1016
let mut list_items = Vec :: with_capacity ( use_tree_list. len ( ) ) ;
1012
1017
let nested_shape = match context. config . imports_indent ( ) {
1013
1018
IndentStyle :: Block => shape
1014
1019
. block_indent ( context. config . tab_spaces ( ) )
1015
1020
. with_max_width ( context. config )
1016
- . sub_width ( 1 ) ?,
1021
+ . sub_width ( 1 )
1022
+ . unknown_error ( ) ?,
1017
1023
IndentStyle :: Visual => shape. visual_indent ( 0 ) ,
1018
1024
} ;
1019
1025
for use_tree in use_tree_list {
1020
1026
if let Some ( mut list_item) = use_tree. list_item . clone ( ) {
1021
1027
list_item. item = use_tree. rewrite_result ( context, nested_shape) ;
1022
1028
list_items. push ( list_item) ;
1023
1029
} 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
+ ) ) ;
1025
1033
}
1026
1034
}
1027
1035
let has_nested_list = use_tree_list. iter ( ) . any ( |use_segment| {
@@ -1057,7 +1065,7 @@ fn rewrite_nested_use_tree(
1057
1065
. preserve_newline ( true )
1058
1066
. nested ( has_nested_list) ;
1059
1067
1060
- let list_str = write_list ( & list_items, & fmt) . ok ( ) ?;
1068
+ let list_str = write_list ( & list_items, & fmt) ?;
1061
1069
1062
1070
let result = if ( list_str. contains ( '\n' )
1063
1071
|| list_str. len ( ) > remaining_width
@@ -1074,12 +1082,16 @@ fn rewrite_nested_use_tree(
1074
1082
format ! ( "{{{list_str}}}" )
1075
1083
} ;
1076
1084
1077
- Some ( result)
1085
+ Ok ( result)
1078
1086
}
1079
1087
1080
1088
impl Rewrite for UseSegment {
1081
1089
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 {
1083
1095
UseSegmentKind :: Ident ( ref ident, Some ( ref rename) ) => {
1084
1096
format ! ( "{ident} as {rename}" )
1085
1097
}
@@ -1091,31 +1103,42 @@ impl Rewrite for UseSegment {
1091
1103
UseSegmentKind :: Crate ( Some ( ref rename) ) => format ! ( "crate as {rename}" ) ,
1092
1104
UseSegmentKind :: Crate ( None ) => "crate" . to_owned ( ) ,
1093
1105
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
+ }
1100
1117
} )
1101
1118
}
1102
1119
}
1103
1120
1104
1121
impl Rewrite for UseTree {
1122
+ fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
1123
+ self . rewrite_result ( context, shape) . ok ( )
1124
+ }
1125
+
1105
1126
// 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 {
1107
1128
let mut result = String :: with_capacity ( 256 ) ;
1108
1129
let mut iter = self . path . iter ( ) . peekable ( ) ;
1109
1130
while let Some ( segment) = iter. next ( ) {
1110
- let segment_str = segment. rewrite ( context, shape) ?;
1131
+ let segment_str = segment. rewrite_result ( context, shape) ?;
1111
1132
result. push_str ( & segment_str) ;
1112
1133
if iter. peek ( ) . is_some ( ) {
1113
1134
result. push_str ( "::" ) ;
1114
1135
// 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 ( ) ) ?;
1116
1139
}
1117
1140
}
1118
- Some ( result)
1141
+ Ok ( result)
1119
1142
}
1120
1143
}
1121
1144
0 commit comments