@@ -24,7 +24,9 @@ use crate::expr::{
2424use crate :: lists:: { ListFormatting , Separator , definitive_tactic, itemize_list, write_list} ;
2525use crate :: macros:: { MacroPosition , rewrite_macro} ;
2626use crate :: overflow;
27- use crate :: rewrite:: { Rewrite , RewriteContext , RewriteError , RewriteErrorExt , RewriteResult } ;
27+ use crate :: rewrite:: {
28+ ExceedsMaxWidthError , Rewrite , RewriteContext , RewriteError , RewriteErrorExt , RewriteResult ,
29+ } ;
2830use crate :: shape:: { Indent , Shape } ;
2931use crate :: source_map:: { LineRangeUtils , SpanUtils } ;
3032use crate :: spanned:: Spanned ;
@@ -789,7 +791,7 @@ pub(crate) fn format_impl(
789791 item : & ast:: Item ,
790792 iimpl : & ast:: Impl ,
791793 offset : Indent ,
792- ) -> Option < String > {
794+ ) -> RewriteResult {
793795 let ast:: Impl {
794796 generics,
795797 self_ty,
@@ -809,7 +811,7 @@ pub(crate) fn format_impl(
809811
810812 let mut option = WhereClauseOption :: snuggled ( & ref_and_type) ;
811813 let snippet = context. snippet ( item. span ) ;
812- let open_pos = snippet. find_uncommented ( "{" ) ? + 1 ;
814+ let open_pos = snippet. find_uncommented ( "{" ) . unknown_error ( ) ? + 1 ;
813815 if !contains_comment ( & snippet[ open_pos..] )
814816 && items. is_empty ( )
815817 && generics. where_clause . predicates . len ( ) == 1
@@ -833,8 +835,7 @@ pub(crate) fn format_impl(
833835 where_span_end,
834836 self_ty. span . hi ( ) ,
835837 option,
836- )
837- . ok ( ) ?;
838+ ) ?;
838839
839840 // If there is no where-clause, we may have missing comments between the trait name and
840841 // the opening brace.
@@ -869,7 +870,7 @@ pub(crate) fn format_impl(
869870 } else {
870871 result. push_str ( " {}" ) ;
871872 }
872- return Some ( result) ;
873+ return Ok ( result) ;
873874 }
874875
875876 result. push_str ( & where_clause_str) ;
@@ -892,7 +893,7 @@ pub(crate) fn format_impl(
892893 // this is an impl body snippet(impl SampleImpl { /* here */ })
893894 let lo = max ( self_ty. span . hi ( ) , generics. where_clause . span . hi ( ) ) ;
894895 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 ;
896897
897898 if !items. is_empty ( ) || contains_comment ( & snippet[ open_pos..] ) {
898899 let mut visitor = FmtVisitor :: from_context ( context) ;
@@ -917,7 +918,7 @@ pub(crate) fn format_impl(
917918
918919 result. push ( '}' ) ;
919920
920- Some ( result)
921+ Ok ( result)
921922}
922923
923924fn is_impl_single_line (
@@ -926,25 +927,23 @@ fn is_impl_single_line(
926927 result : & str ,
927928 where_clause_str : & str ,
928929 item : & ast:: Item ,
929- ) -> Option < bool > {
930+ ) -> Result < bool , RewriteError > {
930931 let snippet = context. snippet ( item. span ) ;
931- let open_pos = snippet. find_uncommented ( "{" ) ? + 1 ;
932+ let open_pos = snippet. find_uncommented ( "{" ) . unknown_error ( ) ? + 1 ;
932933
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..] ) )
940939}
941940
942941fn format_impl_ref_and_type (
943942 context : & RewriteContext < ' _ > ,
944943 item : & ast:: Item ,
945944 iimpl : & ast:: Impl ,
946945 offset : Indent ,
947- ) -> Option < String > {
946+ ) -> RewriteResult {
948947 let ast:: Impl {
949948 safety,
950949 polarity,
@@ -968,9 +967,10 @@ fn format_impl_ref_and_type(
968967 context. config ,
969968 Shape :: indented ( offset + last_line_width ( & result) , context. config ) ,
970969 0 ,
970+ item. span ,
971971 ) ?
972972 } ;
973- let generics_str = rewrite_generics ( context, "impl" , generics, shape) . ok ( ) ?;
973+ let generics_str = rewrite_generics ( context, "impl" , generics, shape) ?;
974974 result. push_str ( & generics_str) ;
975975 result. push_str ( format_constness_right ( constness) ) ;
976976
@@ -1021,7 +1021,7 @@ fn format_impl_ref_and_type(
10211021 result. push_str ( polarity_str) ;
10221022 }
10231023 result. push_str ( & self_ty_str) ;
1024- return Some ( result) ;
1024+ return Ok ( result) ;
10251025 }
10261026 }
10271027
@@ -1040,8 +1040,8 @@ fn format_impl_ref_and_type(
10401040 IndentStyle :: Visual => new_line_offset + trait_ref_overhead,
10411041 IndentStyle :: Block => new_line_offset,
10421042 } ;
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)
10451045}
10461046
10471047fn rewrite_trait_ref (
@@ -1050,20 +1050,20 @@ fn rewrite_trait_ref(
10501050 offset : Indent ,
10511051 polarity_str : & str ,
10521052 result_len : usize ,
1053- ) -> Option < String > {
1053+ ) -> RewriteResult {
10541054 // 1 = space between generics and trait_ref
10551055 let used_space = 1 + polarity_str. len ( ) + result_len;
10561056 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) {
10581058 if !trait_ref_str. contains ( '\n' ) {
1059- return Some ( format ! ( " {polarity_str}{trait_ref_str}" ) ) ;
1059+ return Ok ( format ! ( " {polarity_str}{trait_ref_str}" ) ) ;
10601060 }
10611061 }
10621062 // We could not make enough space for trait_ref, so put it on new line.
10631063 let offset = offset. block_indent ( context. config ) ;
10641064 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 ! (
10671067 "{}{}{}" ,
10681068 offset. to_string_with_newline( context. config) ,
10691069 polarity_str,
@@ -1152,18 +1152,16 @@ fn format_struct(
11521152pub ( crate ) fn format_trait (
11531153 context : & RewriteContext < ' _ > ,
11541154 item : & ast:: Item ,
1155+ trait_ : & ast:: Trait ,
11551156 offset : Indent ,
1156- ) -> Option < String > {
1157- let ast:: ItemKind :: Trait ( trait_kind) = & item. kind else {
1158- unreachable ! ( ) ;
1159- } ;
1157+ ) -> RewriteResult {
11601158 let ast:: Trait {
11611159 is_auto,
11621160 safety,
11631161 ref generics,
11641162 ref bounds,
11651163 ref items,
1166- } = * * trait_kind ;
1164+ } = * trait_ ;
11671165
11681166 let mut result = String :: with_capacity ( 128 ) ;
11691167 let header = format ! (
@@ -1176,9 +1174,9 @@ pub(crate) fn format_trait(
11761174
11771175 let body_lo = context. snippet_provider . span_after ( item. span , "{" ) ;
11781176
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 ) ?;
11801178 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) ?;
11821180 result. push_str ( & generics_str) ;
11831181
11841182 // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
@@ -1189,7 +1187,7 @@ pub(crate) fn format_trait(
11891187 let bound_hi = bounds. last ( ) . unwrap ( ) . span ( ) . hi ( ) ;
11901188 let snippet = context. snippet ( mk_sp ( ident_hi, bound_hi) ) ;
11911189 if contains_comment ( snippet) {
1192- return None ;
1190+ return Err ( RewriteError :: Unknown ) ;
11931191 }
11941192
11951193 result = rewrite_assign_rhs_with (
@@ -1199,8 +1197,7 @@ pub(crate) fn format_trait(
11991197 shape,
12001198 & RhsAssignKind :: Bounds ,
12011199 RhsTactics :: ForceNextLineWithoutIndent ,
1202- )
1203- . ok ( ) ?;
1200+ ) ?;
12041201 }
12051202
12061203 // Rewrite where-clause.
@@ -1225,8 +1222,7 @@ pub(crate) fn format_trait(
12251222 None ,
12261223 pos_before_where,
12271224 option,
1228- )
1229- . ok ( ) ?;
1225+ ) ?;
12301226 // If the where-clause cannot fit on the same line,
12311227 // put the where-clause on a new line
12321228 if !where_clause_str. contains ( '\n' )
@@ -1266,7 +1262,7 @@ pub(crate) fn format_trait(
12661262
12671263 let block_span = mk_sp ( generics. where_clause . span . hi ( ) , item. span . hi ( ) ) ;
12681264 let snippet = context. snippet ( block_span) ;
1269- let open_pos = snippet. find_uncommented ( "{" ) ? + 1 ;
1265+ let open_pos = snippet. find_uncommented ( "{" ) . unknown_error ( ) ? + 1 ;
12701266
12711267 match context. config . brace_style ( ) {
12721268 _ if last_line_contains_single_line_comment ( & result)
@@ -1280,7 +1276,7 @@ pub(crate) fn format_trait(
12801276 && !contains_comment ( & snippet[ open_pos..] ) =>
12811277 {
12821278 result. push_str ( " {}" ) ;
1283- return Some ( result) ;
1279+ return Ok ( result) ;
12841280 }
12851281 BraceStyle :: AlwaysNextLine => {
12861282 result. push_str ( & offset. to_string_with_newline ( context. config ) ) ;
@@ -1321,7 +1317,7 @@ pub(crate) fn format_trait(
13211317 }
13221318
13231319 result. push ( '}' ) ;
1324- Some ( result)
1320+ Ok ( result)
13251321}
13261322
13271323pub ( crate ) struct TraitAliasBounds < ' a > {
@@ -1370,32 +1366,36 @@ impl<'a> Rewrite for TraitAliasBounds<'a> {
13701366
13711367pub ( crate ) fn format_trait_alias (
13721368 context : & RewriteContext < ' _ > ,
1373- ident : symbol:: Ident ,
1374- vis : & ast:: Visibility ,
1369+ item : & ast:: Item ,
13751370 generics : & ast:: Generics ,
13761371 generic_bounds : & ast:: GenericBounds ,
13771372 shape : Shape ,
1378- ) -> Option < String > {
1373+ ) -> RewriteResult {
1374+ let ast:: Item {
1375+ ident,
1376+ ref vis,
1377+ span,
1378+ ..
1379+ } = * item;
13791380 let alias = rewrite_ident ( context, ident) ;
13801381 // 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) ?;
13831384 let vis_str = format_visibility ( context, vis) ;
13841385 let lhs = format ! ( "{vis_str}trait {generics_str} =" ) ;
13851386 // 1 = ";"
13861387 let trait_alias_bounds = TraitAliasBounds {
13871388 generic_bounds,
13881389 generics,
13891390 } ;
1390- rewrite_assign_rhs (
1391+ let result = rewrite_assign_rhs (
13911392 context,
13921393 lhs,
13931394 & trait_alias_bounds,
13941395 & 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 + ";" )
13991399}
14001400
14011401fn format_unit_struct (
@@ -2959,16 +2959,21 @@ fn rewrite_generics(
29592959 overflow:: rewrite_with_angle_brackets ( context, ident, params, shape, generics. span )
29602960}
29612961
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 > {
29632968 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 ) ,
29652970 IndentStyle :: Block => {
29662971 // 1 = ","
29672972 shape
29682973 . block ( )
29692974 . block_indent ( config. tab_spaces ( ) )
29702975 . with_max_width ( config)
2971- . sub_width_opt ( 1 )
2976+ . sub_width ( 1 , span )
29722977 }
29732978 }
29742979}
@@ -3502,9 +3507,9 @@ fn rewrite_attrs(
35023507 item : & ast:: Item ,
35033508 item_str : & str ,
35043509 shape : Shape ,
3505- ) -> Option < String > {
3510+ ) -> RewriteResult {
35063511 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) ?;
35083513
35093514 let missed_span = if attrs. is_empty ( ) {
35103515 mk_sp ( item. span . lo ( ) , item. span . lo ( ) )
@@ -3528,7 +3533,6 @@ fn rewrite_attrs(
35283533 shape,
35293534 allow_extend,
35303535 )
3531- . ok ( )
35323536}
35333537
35343538/// Rewrite an inline mod.
@@ -3537,7 +3541,7 @@ pub(crate) fn rewrite_mod(
35373541 context : & RewriteContext < ' _ > ,
35383542 item : & ast:: Item ,
35393543 attrs_shape : Shape ,
3540- ) -> Option < String > {
3544+ ) -> RewriteResult {
35413545 let mut result = String :: with_capacity ( 32 ) ;
35423546 result. push_str ( & * format_visibility ( context, & item. vis ) ) ;
35433547 result. push_str ( "mod " ) ;
@@ -3552,7 +3556,7 @@ pub(crate) fn rewrite_extern_crate(
35523556 context : & RewriteContext < ' _ > ,
35533557 item : & ast:: Item ,
35543558 attrs_shape : Shape ,
3555- ) -> Option < String > {
3559+ ) -> RewriteResult {
35563560 assert ! ( is_extern_crate( item) ) ;
35573561 let new_str = context. snippet ( item. span ) ;
35583562 let item_str = if contains_comment ( new_str) {
0 commit comments