@@ -24,7 +24,9 @@ use crate::expr::{
24
24
use crate :: lists:: { ListFormatting , Separator , definitive_tactic, itemize_list, write_list} ;
25
25
use crate :: macros:: { MacroPosition , rewrite_macro} ;
26
26
use crate :: overflow;
27
- use crate :: rewrite:: { Rewrite , RewriteContext , RewriteError , RewriteErrorExt , RewriteResult } ;
27
+ use crate :: rewrite:: {
28
+ ExceedsMaxWidthError , Rewrite , RewriteContext , RewriteError , RewriteErrorExt , RewriteResult ,
29
+ } ;
28
30
use crate :: shape:: { Indent , Shape } ;
29
31
use crate :: source_map:: { LineRangeUtils , SpanUtils } ;
30
32
use crate :: spanned:: Spanned ;
@@ -789,7 +791,7 @@ pub(crate) fn format_impl(
789
791
item : & ast:: Item ,
790
792
iimpl : & ast:: Impl ,
791
793
offset : Indent ,
792
- ) -> Option < String > {
794
+ ) -> RewriteResult {
793
795
let ast:: Impl {
794
796
generics,
795
797
self_ty,
@@ -809,7 +811,7 @@ pub(crate) fn format_impl(
809
811
810
812
let mut option = WhereClauseOption :: snuggled ( & ref_and_type) ;
811
813
let snippet = context. snippet ( item. span ) ;
812
- let open_pos = snippet. find_uncommented ( "{" ) ? + 1 ;
814
+ let open_pos = snippet. find_uncommented ( "{" ) . unknown_error ( ) ? + 1 ;
813
815
if !contains_comment ( & snippet[ open_pos..] )
814
816
&& items. is_empty ( )
815
817
&& generics. where_clause . predicates . len ( ) == 1
@@ -833,8 +835,7 @@ pub(crate) fn format_impl(
833
835
where_span_end,
834
836
self_ty. span . hi ( ) ,
835
837
option,
836
- )
837
- . ok ( ) ?;
838
+ ) ?;
838
839
839
840
// If there is no where-clause, we may have missing comments between the trait name and
840
841
// the opening brace.
@@ -869,7 +870,7 @@ pub(crate) fn format_impl(
869
870
} else {
870
871
result. push_str ( " {}" ) ;
871
872
}
872
- return Some ( result) ;
873
+ return Ok ( result) ;
873
874
}
874
875
875
876
result. push_str ( & where_clause_str) ;
@@ -892,7 +893,7 @@ pub(crate) fn format_impl(
892
893
// this is an impl body snippet(impl SampleImpl { /* here */ })
893
894
let lo = max ( self_ty. span . hi ( ) , generics. where_clause . span . hi ( ) ) ;
894
895
let snippet = context. snippet ( mk_sp ( lo, item. span . hi ( ) ) ) ;
895
- let open_pos = snippet. find_uncommented ( "{" ) ? + 1 ;
896
+ let open_pos = snippet. find_uncommented ( "{" ) . unknown_error ( ) ? + 1 ;
896
897
897
898
if !items. is_empty ( ) || contains_comment ( & snippet[ open_pos..] ) {
898
899
let mut visitor = FmtVisitor :: from_context ( context) ;
@@ -917,7 +918,7 @@ pub(crate) fn format_impl(
917
918
918
919
result. push ( '}' ) ;
919
920
920
- Some ( result)
921
+ Ok ( result)
921
922
}
922
923
923
924
fn is_impl_single_line (
@@ -926,25 +927,23 @@ fn is_impl_single_line(
926
927
result : & str ,
927
928
where_clause_str : & str ,
928
929
item : & ast:: Item ,
929
- ) -> Option < bool > {
930
+ ) -> Result < bool , RewriteError > {
930
931
let snippet = context. snippet ( item. span ) ;
931
- let open_pos = snippet. find_uncommented ( "{" ) ? + 1 ;
932
+ let open_pos = snippet. find_uncommented ( "{" ) . unknown_error ( ) ? + 1 ;
932
933
933
- Some (
934
- context. config . empty_item_single_line ( )
935
- && items. is_empty ( )
936
- && !result. contains ( '\n' )
937
- && result. len ( ) + where_clause_str. len ( ) <= context. config . max_width ( )
938
- && !contains_comment ( & snippet[ open_pos..] ) ,
939
- )
934
+ Ok ( context. config . empty_item_single_line ( )
935
+ && items. is_empty ( )
936
+ && !result. contains ( '\n' )
937
+ && result. len ( ) + where_clause_str. len ( ) <= context. config . max_width ( )
938
+ && !contains_comment ( & snippet[ open_pos..] ) )
940
939
}
941
940
942
941
fn format_impl_ref_and_type (
943
942
context : & RewriteContext < ' _ > ,
944
943
item : & ast:: Item ,
945
944
iimpl : & ast:: Impl ,
946
945
offset : Indent ,
947
- ) -> Option < String > {
946
+ ) -> RewriteResult {
948
947
let ast:: Impl {
949
948
safety,
950
949
polarity,
@@ -968,9 +967,10 @@ fn format_impl_ref_and_type(
968
967
context. config ,
969
968
Shape :: indented ( offset + last_line_width ( & result) , context. config ) ,
970
969
0 ,
970
+ item. span ,
971
971
) ?
972
972
} ;
973
- let generics_str = rewrite_generics ( context, "impl" , generics, shape) . ok ( ) ?;
973
+ let generics_str = rewrite_generics ( context, "impl" , generics, shape) ?;
974
974
result. push_str ( & generics_str) ;
975
975
result. push_str ( format_constness_right ( constness) ) ;
976
976
@@ -1021,7 +1021,7 @@ fn format_impl_ref_and_type(
1021
1021
result. push_str ( polarity_str) ;
1022
1022
}
1023
1023
result. push_str ( & self_ty_str) ;
1024
- return Some ( result) ;
1024
+ return Ok ( result) ;
1025
1025
}
1026
1026
}
1027
1027
@@ -1040,8 +1040,8 @@ fn format_impl_ref_and_type(
1040
1040
IndentStyle :: Visual => new_line_offset + trait_ref_overhead,
1041
1041
IndentStyle :: Block => new_line_offset,
1042
1042
} ;
1043
- result. push_str ( & * self_ty. rewrite ( context, Shape :: legacy ( budget, type_offset) ) ?) ;
1044
- Some ( result)
1043
+ result. push_str ( & * self_ty. rewrite_result ( context, Shape :: legacy ( budget, type_offset) ) ?) ;
1044
+ Ok ( result)
1045
1045
}
1046
1046
1047
1047
fn rewrite_trait_ref (
@@ -1050,20 +1050,20 @@ fn rewrite_trait_ref(
1050
1050
offset : Indent ,
1051
1051
polarity_str : & str ,
1052
1052
result_len : usize ,
1053
- ) -> Option < String > {
1053
+ ) -> RewriteResult {
1054
1054
// 1 = space between generics and trait_ref
1055
1055
let used_space = 1 + polarity_str. len ( ) + result_len;
1056
1056
let shape = Shape :: indented ( offset + used_space, context. config ) ;
1057
- if let Some ( trait_ref_str) = trait_ref. rewrite ( context, shape) {
1057
+ if let Ok ( trait_ref_str) = trait_ref. rewrite_result ( context, shape) {
1058
1058
if !trait_ref_str. contains ( '\n' ) {
1059
- return Some ( format ! ( " {polarity_str}{trait_ref_str}" ) ) ;
1059
+ return Ok ( format ! ( " {polarity_str}{trait_ref_str}" ) ) ;
1060
1060
}
1061
1061
}
1062
1062
// We could not make enough space for trait_ref, so put it on new line.
1063
1063
let offset = offset. block_indent ( context. config ) ;
1064
1064
let shape = Shape :: indented ( offset, context. config ) ;
1065
- let trait_ref_str = trait_ref. rewrite ( context, shape) ?;
1066
- Some ( format ! (
1065
+ let trait_ref_str = trait_ref. rewrite_result ( context, shape) ?;
1066
+ Ok ( format ! (
1067
1067
"{}{}{}" ,
1068
1068
offset. to_string_with_newline( context. config) ,
1069
1069
polarity_str,
@@ -1152,18 +1152,16 @@ fn format_struct(
1152
1152
pub ( crate ) fn format_trait (
1153
1153
context : & RewriteContext < ' _ > ,
1154
1154
item : & ast:: Item ,
1155
+ trait_ : & ast:: Trait ,
1155
1156
offset : Indent ,
1156
- ) -> Option < String > {
1157
- let ast:: ItemKind :: Trait ( trait_kind) = & item. kind else {
1158
- unreachable ! ( ) ;
1159
- } ;
1157
+ ) -> RewriteResult {
1160
1158
let ast:: Trait {
1161
1159
is_auto,
1162
1160
safety,
1163
1161
ref generics,
1164
1162
ref bounds,
1165
1163
ref items,
1166
- } = * * trait_kind ;
1164
+ } = * trait_ ;
1167
1165
1168
1166
let mut result = String :: with_capacity ( 128 ) ;
1169
1167
let header = format ! (
@@ -1176,9 +1174,9 @@ pub(crate) fn format_trait(
1176
1174
1177
1175
let body_lo = context. snippet_provider . span_after ( item. span , "{" ) ;
1178
1176
1179
- let shape = Shape :: indented ( offset, context. config ) . offset_left_opt ( result. len ( ) ) ?;
1177
+ let shape = Shape :: indented ( offset, context. config ) . offset_left ( result. len ( ) , item . span ) ?;
1180
1178
let generics_str =
1181
- rewrite_generics ( context, rewrite_ident ( context, item. ident ) , generics, shape) . ok ( ) ?;
1179
+ rewrite_generics ( context, rewrite_ident ( context, item. ident ) , generics, shape) ?;
1182
1180
result. push_str ( & generics_str) ;
1183
1181
1184
1182
// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
@@ -1189,7 +1187,7 @@ pub(crate) fn format_trait(
1189
1187
let bound_hi = bounds. last ( ) . unwrap ( ) . span ( ) . hi ( ) ;
1190
1188
let snippet = context. snippet ( mk_sp ( ident_hi, bound_hi) ) ;
1191
1189
if contains_comment ( snippet) {
1192
- return None ;
1190
+ return Err ( RewriteError :: Unknown ) ;
1193
1191
}
1194
1192
1195
1193
result = rewrite_assign_rhs_with (
@@ -1199,8 +1197,7 @@ pub(crate) fn format_trait(
1199
1197
shape,
1200
1198
& RhsAssignKind :: Bounds ,
1201
1199
RhsTactics :: ForceNextLineWithoutIndent ,
1202
- )
1203
- . ok ( ) ?;
1200
+ ) ?;
1204
1201
}
1205
1202
1206
1203
// Rewrite where-clause.
@@ -1225,8 +1222,7 @@ pub(crate) fn format_trait(
1225
1222
None ,
1226
1223
pos_before_where,
1227
1224
option,
1228
- )
1229
- . ok ( ) ?;
1225
+ ) ?;
1230
1226
// If the where-clause cannot fit on the same line,
1231
1227
// put the where-clause on a new line
1232
1228
if !where_clause_str. contains ( '\n' )
@@ -1266,7 +1262,7 @@ pub(crate) fn format_trait(
1266
1262
1267
1263
let block_span = mk_sp ( generics. where_clause . span . hi ( ) , item. span . hi ( ) ) ;
1268
1264
let snippet = context. snippet ( block_span) ;
1269
- let open_pos = snippet. find_uncommented ( "{" ) ? + 1 ;
1265
+ let open_pos = snippet. find_uncommented ( "{" ) . unknown_error ( ) ? + 1 ;
1270
1266
1271
1267
match context. config . brace_style ( ) {
1272
1268
_ if last_line_contains_single_line_comment ( & result)
@@ -1280,7 +1276,7 @@ pub(crate) fn format_trait(
1280
1276
&& !contains_comment ( & snippet[ open_pos..] ) =>
1281
1277
{
1282
1278
result. push_str ( " {}" ) ;
1283
- return Some ( result) ;
1279
+ return Ok ( result) ;
1284
1280
}
1285
1281
BraceStyle :: AlwaysNextLine => {
1286
1282
result. push_str ( & offset. to_string_with_newline ( context. config ) ) ;
@@ -1321,7 +1317,7 @@ pub(crate) fn format_trait(
1321
1317
}
1322
1318
1323
1319
result. push ( '}' ) ;
1324
- Some ( result)
1320
+ Ok ( result)
1325
1321
}
1326
1322
1327
1323
pub ( crate ) struct TraitAliasBounds < ' a > {
@@ -1370,32 +1366,36 @@ impl<'a> Rewrite for TraitAliasBounds<'a> {
1370
1366
1371
1367
pub ( crate ) fn format_trait_alias (
1372
1368
context : & RewriteContext < ' _ > ,
1373
- ident : symbol:: Ident ,
1374
- vis : & ast:: Visibility ,
1369
+ item : & ast:: Item ,
1375
1370
generics : & ast:: Generics ,
1376
1371
generic_bounds : & ast:: GenericBounds ,
1377
1372
shape : Shape ,
1378
- ) -> Option < String > {
1373
+ ) -> RewriteResult {
1374
+ let ast:: Item {
1375
+ ident,
1376
+ ref vis,
1377
+ span,
1378
+ ..
1379
+ } = * item;
1379
1380
let alias = rewrite_ident ( context, ident) ;
1380
1381
// 6 = "trait ", 2 = " ="
1381
- let g_shape = shape. offset_left_opt ( 6 ) ?. sub_width_opt ( 2 ) ?;
1382
- let generics_str = rewrite_generics ( context, alias, generics, g_shape) . ok ( ) ?;
1382
+ let g_shape = shape. offset_left ( 6 , span ) ?. sub_width ( 2 , span ) ?;
1383
+ let generics_str = rewrite_generics ( context, alias, generics, g_shape) ?;
1383
1384
let vis_str = format_visibility ( context, vis) ;
1384
1385
let lhs = format ! ( "{vis_str}trait {generics_str} =" ) ;
1385
1386
// 1 = ";"
1386
1387
let trait_alias_bounds = TraitAliasBounds {
1387
1388
generic_bounds,
1388
1389
generics,
1389
1390
} ;
1390
- rewrite_assign_rhs (
1391
+ let result = rewrite_assign_rhs (
1391
1392
context,
1392
1393
lhs,
1393
1394
& trait_alias_bounds,
1394
1395
& RhsAssignKind :: Bounds ,
1395
- shape. sub_width_opt ( 1 ) ?,
1396
- )
1397
- . map ( |s| s + ";" )
1398
- . ok ( )
1396
+ shape. sub_width ( 1 , generics. span ) ?,
1397
+ ) ?;
1398
+ Ok ( result + ";" )
1399
1399
}
1400
1400
1401
1401
fn format_unit_struct (
@@ -2959,16 +2959,21 @@ fn rewrite_generics(
2959
2959
overflow:: rewrite_with_angle_brackets ( context, ident, params, shape, generics. span )
2960
2960
}
2961
2961
2962
- fn generics_shape_from_config ( config : & Config , shape : Shape , offset : usize ) -> Option < Shape > {
2962
+ fn generics_shape_from_config (
2963
+ config : & Config ,
2964
+ shape : Shape ,
2965
+ offset : usize ,
2966
+ span : Span ,
2967
+ ) -> Result < Shape , ExceedsMaxWidthError > {
2963
2968
match config. indent_style ( ) {
2964
- IndentStyle :: Visual => shape. visual_indent ( 1 + offset) . sub_width_opt ( offset + 2 ) ,
2969
+ IndentStyle :: Visual => shape. visual_indent ( 1 + offset) . sub_width ( offset + 2 , span ) ,
2965
2970
IndentStyle :: Block => {
2966
2971
// 1 = ","
2967
2972
shape
2968
2973
. block ( )
2969
2974
. block_indent ( config. tab_spaces ( ) )
2970
2975
. with_max_width ( config)
2971
- . sub_width_opt ( 1 )
2976
+ . sub_width ( 1 , span )
2972
2977
}
2973
2978
}
2974
2979
}
@@ -3502,9 +3507,9 @@ fn rewrite_attrs(
3502
3507
item : & ast:: Item ,
3503
3508
item_str : & str ,
3504
3509
shape : Shape ,
3505
- ) -> Option < String > {
3510
+ ) -> RewriteResult {
3506
3511
let attrs = filter_inline_attrs ( & item. attrs , item. span ( ) ) ;
3507
- let attrs_str = attrs. rewrite ( context, shape) ?;
3512
+ let attrs_str = attrs. rewrite_result ( context, shape) ?;
3508
3513
3509
3514
let missed_span = if attrs. is_empty ( ) {
3510
3515
mk_sp ( item. span . lo ( ) , item. span . lo ( ) )
@@ -3528,7 +3533,6 @@ fn rewrite_attrs(
3528
3533
shape,
3529
3534
allow_extend,
3530
3535
)
3531
- . ok ( )
3532
3536
}
3533
3537
3534
3538
/// Rewrite an inline mod.
@@ -3537,7 +3541,7 @@ pub(crate) fn rewrite_mod(
3537
3541
context : & RewriteContext < ' _ > ,
3538
3542
item : & ast:: Item ,
3539
3543
attrs_shape : Shape ,
3540
- ) -> Option < String > {
3544
+ ) -> RewriteResult {
3541
3545
let mut result = String :: with_capacity ( 32 ) ;
3542
3546
result. push_str ( & * format_visibility ( context, & item. vis ) ) ;
3543
3547
result. push_str ( "mod " ) ;
@@ -3552,7 +3556,7 @@ pub(crate) fn rewrite_extern_crate(
3552
3556
context : & RewriteContext < ' _ > ,
3553
3557
item : & ast:: Item ,
3554
3558
attrs_shape : Shape ,
3555
- ) -> Option < String > {
3559
+ ) -> RewriteResult {
3556
3560
assert ! ( is_extern_crate( item) ) ;
3557
3561
let new_str = context. snippet ( item. span ) ;
3558
3562
let item_str = if contains_comment ( new_str) {
0 commit comments