@@ -1859,10 +1859,10 @@ fn type_annotation_spacing(config: &Config) -> (&str, &str) {
1859
1859
pub ( crate ) fn rewrite_struct_field_prefix (
1860
1860
context : & RewriteContext < ' _ > ,
1861
1861
field : & ast:: FieldDef ,
1862
- ) -> Option < String > {
1862
+ ) -> RewriteResult {
1863
1863
let vis = format_visibility ( context, & field. vis ) ;
1864
1864
let type_annotation_spacing = type_annotation_spacing ( context. config ) ;
1865
- Some ( match field. ident {
1865
+ Ok ( match field. ident {
1866
1866
Some ( name) => format ! (
1867
1867
"{}{}{}:" ,
1868
1868
vis,
@@ -1875,6 +1875,9 @@ pub(crate) fn rewrite_struct_field_prefix(
1875
1875
1876
1876
impl Rewrite for ast:: FieldDef {
1877
1877
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
1878
+ self . rewrite_result ( context, shape) . ok ( )
1879
+ }
1880
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
1878
1881
rewrite_struct_field ( context, self , shape, 0 )
1879
1882
}
1880
1883
}
@@ -1884,15 +1887,15 @@ pub(crate) fn rewrite_struct_field(
1884
1887
field : & ast:: FieldDef ,
1885
1888
shape : Shape ,
1886
1889
lhs_max_width : usize ,
1887
- ) -> Option < String > {
1890
+ ) -> RewriteResult {
1888
1891
if contains_skip ( & field. attrs ) {
1889
- return Some ( context. snippet ( field. span ( ) ) . to_owned ( ) ) ;
1892
+ return Ok ( context. snippet ( field. span ( ) ) . to_owned ( ) ) ;
1890
1893
}
1891
1894
1892
1895
let type_annotation_spacing = type_annotation_spacing ( context. config ) ;
1893
1896
let prefix = rewrite_struct_field_prefix ( context, field) ?;
1894
1897
1895
- let attrs_str = field. attrs . rewrite ( context, shape) ?;
1898
+ let attrs_str = field. attrs . rewrite_result ( context, shape) ?;
1896
1899
let attrs_extendable = field. ident . is_none ( ) && is_attributes_extendable ( & attrs_str) ;
1897
1900
let missing_span = if field. attrs . is_empty ( ) {
1898
1901
mk_sp ( field. span . lo ( ) , field. span . lo ( ) )
@@ -1912,7 +1915,8 @@ pub(crate) fn rewrite_struct_field(
1912
1915
missing_span,
1913
1916
shape,
1914
1917
attrs_extendable,
1915
- ) ?;
1918
+ )
1919
+ . ok_or_else ( || RewriteError :: Unknown ) ?;
1916
1920
let overhead = trimmed_last_line_width ( & attr_prefix) ;
1917
1921
let lhs_offset = lhs_max_width. saturating_sub ( overhead) ;
1918
1922
for _ in 0 ..lhs_offset {
@@ -1922,25 +1926,36 @@ pub(crate) fn rewrite_struct_field(
1922
1926
if prefix. is_empty ( ) && !attrs_str. is_empty ( ) && attrs_extendable && spacing. is_empty ( ) {
1923
1927
spacing. push ( ' ' ) ;
1924
1928
}
1925
- let orig_ty = shape
1926
- . offset_left ( overhead + spacing. len ( ) )
1927
- . and_then ( |ty_shape| field. ty . rewrite ( context, ty_shape) ) ;
1928
- if let Some ( ref ty) = orig_ty {
1929
+
1930
+ // Question. Why don't we immediately return None/Err with ?
1931
+ // let orig_ty = shape
1932
+ // .offset_left(overhead + spacing.len())
1933
+ // .and_then(|ty_shape| field.ty.rewrite(context, ty_shape));
1934
+ let ty_shape = shape. offset_left ( overhead + spacing. len ( ) ) . ok_or_else ( || {
1935
+ RewriteError :: ExceedsMaxWidth {
1936
+ configured_width : shape. width ,
1937
+ span : field. span ( ) ,
1938
+ }
1939
+ } ) ?;
1940
+ let orig_ty = field. ty . rewrite_result ( context, ty_shape) ;
1941
+ if let Ok ( ref ty) = orig_ty {
1929
1942
if !ty. contains ( '\n' ) && !contains_comment ( context. snippet ( missing_span) ) {
1930
- return Some ( attr_prefix + & spacing + ty) ;
1943
+ return Ok ( attr_prefix + & spacing + ty) ;
1931
1944
}
1932
1945
}
1933
1946
1934
1947
let is_prefix_empty = prefix. is_empty ( ) ;
1935
1948
// We must use multiline. We are going to put attributes and a field on different lines.
1936
- let field_str = rewrite_assign_rhs ( context, prefix, & * field. ty , & RhsAssignKind :: Ty , shape) ?;
1949
+ let field_str = rewrite_assign_rhs ( context, prefix, & * field. ty , & RhsAssignKind :: Ty , shape)
1950
+ . ok_or_else ( || RewriteError :: Unknown ) ?;
1937
1951
// Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
1938
1952
let field_str = if is_prefix_empty {
1939
1953
field_str. trim_start ( )
1940
1954
} else {
1941
1955
& field_str
1942
1956
} ;
1943
1957
combine_strs_with_missing_comments ( context, & attrs_str, field_str, missing_span, shape, false )
1958
+ . ok_or_else ( || RewriteError :: Unknown )
1944
1959
}
1945
1960
1946
1961
pub ( crate ) struct StaticParts < ' a > {
@@ -2211,7 +2226,6 @@ impl Rewrite for ast::Param {
2211
2226
shape,
2212
2227
has_multiple_attr_lines,
2213
2228
)
2214
- . ok_or_else ( || RewriteError :: Unknown )
2215
2229
} else if is_named_param ( self ) {
2216
2230
let param_name = & self
2217
2231
. pat
@@ -2291,58 +2305,62 @@ fn rewrite_explicit_self(
2291
2305
span : Span ,
2292
2306
shape : Shape ,
2293
2307
has_multiple_attr_lines : bool ,
2294
- ) -> Option < String > {
2308
+ ) -> RewriteResult {
2295
2309
match explicit_self. node {
2296
2310
ast:: SelfKind :: Region ( lt, m) => {
2297
2311
let mut_str = format_mutability ( m) ;
2298
2312
match lt {
2299
2313
Some ( ref l) => {
2300
- let lifetime_str = l. rewrite (
2314
+ let lifetime_str = l. rewrite_result (
2301
2315
context,
2302
2316
Shape :: legacy ( context. config . max_width ( ) , Indent :: empty ( ) ) ,
2303
2317
) ?;
2304
- Some ( combine_strs_with_missing_comments (
2318
+ Ok ( combine_strs_with_missing_comments (
2305
2319
context,
2306
2320
param_attrs,
2307
2321
& format ! ( "&{lifetime_str} {mut_str}self" ) ,
2308
2322
span,
2309
2323
shape,
2310
2324
!has_multiple_attr_lines,
2311
- ) ?)
2325
+ )
2326
+ . ok_or_else ( || RewriteError :: Unknown ) ?)
2312
2327
}
2313
- None => Some ( combine_strs_with_missing_comments (
2328
+ None => Ok ( combine_strs_with_missing_comments (
2314
2329
context,
2315
2330
param_attrs,
2316
2331
& format ! ( "&{mut_str}self" ) ,
2317
2332
span,
2318
2333
shape,
2319
2334
!has_multiple_attr_lines,
2320
- ) ?) ,
2335
+ )
2336
+ . ok_or_else ( || RewriteError :: Unknown ) ?) ,
2321
2337
}
2322
2338
}
2323
2339
ast:: SelfKind :: Explicit ( ref ty, mutability) => {
2324
- let type_str = ty. rewrite (
2340
+ let type_str = ty. rewrite_result (
2325
2341
context,
2326
2342
Shape :: legacy ( context. config . max_width ( ) , Indent :: empty ( ) ) ,
2327
2343
) ?;
2328
2344
2329
- Some ( combine_strs_with_missing_comments (
2345
+ Ok ( combine_strs_with_missing_comments (
2330
2346
context,
2331
2347
param_attrs,
2332
2348
& format ! ( "{}self: {}" , format_mutability( mutability) , type_str) ,
2333
2349
span,
2334
2350
shape,
2335
2351
!has_multiple_attr_lines,
2336
- ) ?)
2352
+ )
2353
+ . ok_or_else ( || RewriteError :: Unknown ) ?)
2337
2354
}
2338
- ast:: SelfKind :: Value ( mutability) => Some ( combine_strs_with_missing_comments (
2355
+ ast:: SelfKind :: Value ( mutability) => Ok ( combine_strs_with_missing_comments (
2339
2356
context,
2340
2357
param_attrs,
2341
2358
& format ! ( "{}self" , format_mutability( mutability) ) ,
2342
2359
span,
2343
2360
shape,
2344
2361
!has_multiple_attr_lines,
2345
- ) ?) ,
2362
+ )
2363
+ . ok_or_else ( || RewriteError :: Unknown ) ?) ,
2346
2364
}
2347
2365
}
2348
2366
0 commit comments