@@ -24,7 +24,7 @@ use crate::expr::{
24
24
use crate :: lists:: { definitive_tactic, itemize_list, write_list, ListFormatting , Separator } ;
25
25
use crate :: macros:: { rewrite_macro, MacroPosition } ;
26
26
use crate :: overflow;
27
- use crate :: rewrite:: { Rewrite , RewriteContext } ;
27
+ use crate :: rewrite:: { Rewrite , RewriteContext , RewriteError } ;
28
28
use crate :: shape:: { Indent , Shape } ;
29
29
use crate :: source_map:: { LineRangeUtils , SpanUtils } ;
30
30
use crate :: spanned:: Spanned ;
@@ -48,18 +48,26 @@ fn type_annotation_separator(config: &Config) -> &str {
48
48
// let pat: ty = init; or let pat: ty = init else { .. };
49
49
impl Rewrite for ast:: Local {
50
50
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
51
+ self . rewrite_result ( context, shape) . ok ( )
52
+ }
53
+
54
+ fn rewrite_result (
55
+ & self ,
56
+ context : & RewriteContext < ' _ > ,
57
+ shape : Shape ,
58
+ ) -> Result < String , RewriteError > {
51
59
debug ! (
52
60
"Local::rewrite {:?} {} {:?}" ,
53
61
self , shape. width, shape. indent
54
62
) ;
55
63
56
- skip_out_of_file_lines_range ! ( context, self . span) ;
64
+ skip_out_of_file_lines_range_err ! ( context, self . span) ;
57
65
58
66
if contains_skip ( & self . attrs ) {
59
- return None ;
67
+ return Err ( RewriteError :: SkipFormatting ) ;
60
68
}
61
69
62
- let attrs_str = self . attrs . rewrite ( context, shape) ?;
70
+ let attrs_str = self . attrs . rewrite_result ( context, shape) ?;
63
71
let mut result = if attrs_str. is_empty ( ) {
64
72
"let " . to_owned ( )
65
73
} else {
@@ -73,15 +81,27 @@ impl Rewrite for ast::Local {
73
81
) ,
74
82
shape,
75
83
false ,
76
- ) ?
84
+ )
85
+ . ok_or_else ( || RewriteError :: Unknown ) ?
77
86
} ;
78
87
let let_kw_offset = result. len ( ) - "let " . len ( ) ;
79
88
80
89
// 4 = "let ".len()
81
- let pat_shape = shape. offset_left ( 4 ) ?;
90
+ let pat_shape = shape. offset_left ( 4 ) . ok_or_else ( || RewriteError :: Unknown ) ?;
82
91
// 1 = ;
83
- let pat_shape = pat_shape. sub_width ( 1 ) ?;
84
- let pat_str = self . pat . rewrite ( context, pat_shape) ?;
92
+ let pat_shape = pat_shape
93
+ . sub_width ( 1 )
94
+ . ok_or_else ( || RewriteError :: ExceedsMaxWidth {
95
+ configured_width : shape. width ,
96
+ span : self . span ( ) ,
97
+ } ) ?;
98
+ let pat_str =
99
+ self . pat
100
+ . rewrite ( context, pat_shape)
101
+ . ok_or_else ( || RewriteError :: ExceedsMaxWidth {
102
+ configured_width : shape. width ,
103
+ span : self . span ( ) ,
104
+ } ) ?;
85
105
result. push_str ( & pat_str) ;
86
106
87
107
// String that is placed within the assignment pattern and expression.
@@ -95,11 +115,19 @@ impl Rewrite for ast::Local {
95
115
} else {
96
116
shape
97
117
}
98
- . offset_left ( last_line_width ( & result) + separator. len ( ) ) ?
118
+ . offset_left ( last_line_width ( & result) + separator. len ( ) )
119
+ . ok_or_else ( || RewriteError :: ExceedsMaxWidth {
120
+ configured_width : shape. width ,
121
+ span : self . span ( ) ,
122
+ } ) ?
99
123
// 2 = ` =`
100
- . sub_width ( 2 ) ?;
124
+ . sub_width ( 2 )
125
+ . ok_or_else ( || RewriteError :: ExceedsMaxWidth {
126
+ configured_width : shape. width ,
127
+ span : self . span ( ) ,
128
+ } ) ?;
101
129
102
- let rewrite = ty. rewrite ( context, ty_shape) ?;
130
+ let rewrite = ty. rewrite_result ( context, ty_shape) ?;
103
131
104
132
infix. push_str ( separator) ;
105
133
infix. push_str ( & rewrite) ;
@@ -116,15 +144,16 @@ impl Rewrite for ast::Local {
116
144
117
145
if let Some ( ( init, else_block) ) = self . kind . init_else_opt ( ) {
118
146
// 1 = trailing semicolon;
119
- let nested_shape = shape. sub_width ( 1 ) ?;
147
+ let nested_shape = shape. sub_width ( 1 ) . ok_or_else ( || RewriteError :: Unknown ) ?;
120
148
121
149
result = rewrite_assign_rhs (
122
150
context,
123
151
result,
124
152
init,
125
153
& RhsAssignKind :: Expr ( & init. kind , init. span ) ,
126
154
nested_shape,
127
- ) ?;
155
+ )
156
+ . ok_or_else ( || RewriteError :: Unknown ) ?;
128
157
129
158
if let Some ( block) = else_block {
130
159
let else_kw_span = init. span . between ( block. span ) ;
@@ -166,7 +195,8 @@ impl Rewrite for ast::Local {
166
195
&& allow_single_line_let_else_block ( assign_str_with_else_kw, block) ;
167
196
168
197
let mut rw_else_block =
169
- rewrite_let_else_block ( block, allow_single_line, context, shape) ?;
198
+ rewrite_let_else_block ( block, allow_single_line, context, shape)
199
+ . ok_or_else ( || RewriteError :: Unknown ) ?;
170
200
171
201
let single_line_else = !rw_else_block. contains ( '\n' ) ;
172
202
// +1 for the trailing `;`
@@ -175,15 +205,16 @@ impl Rewrite for ast::Local {
175
205
if allow_single_line && single_line_else && else_block_exceeds_width {
176
206
// writing this on one line would exceed the available width
177
207
// so rewrite the else block over multiple lines.
178
- rw_else_block = rewrite_let_else_block ( block, false , context, shape) ?;
208
+ rw_else_block = rewrite_let_else_block ( block, false , context, shape)
209
+ . ok_or_else ( || RewriteError :: Unknown ) ?;
179
210
}
180
211
181
212
result. push_str ( & rw_else_block) ;
182
213
} ;
183
214
}
184
215
185
216
result. push ( ';' ) ;
186
- Some ( result)
217
+ Ok ( result)
187
218
}
188
219
}
189
220
@@ -1845,7 +1876,15 @@ pub(crate) fn rewrite_struct_field_prefix(
1845
1876
1846
1877
impl Rewrite for ast:: FieldDef {
1847
1878
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
1848
- rewrite_struct_field ( context, self , shape, 0 )
1879
+ self . rewrite_result ( context, shape) . ok ( )
1880
+ }
1881
+
1882
+ fn rewrite_result (
1883
+ & self ,
1884
+ context : & RewriteContext < ' _ > ,
1885
+ shape : Shape ,
1886
+ ) -> Result < String , RewriteError > {
1887
+ rewrite_struct_field ( context, self , shape, 0 ) . ok_or_else ( || RewriteError :: Unknown )
1849
1888
}
1850
1889
}
1851
1890
@@ -2071,20 +2110,40 @@ impl<'a> Rewrite for OpaqueType<'a> {
2071
2110
2072
2111
impl Rewrite for ast:: FnRetTy {
2073
2112
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
2113
+ self . rewrite_result ( context, shape) . ok ( )
2114
+ }
2115
+ fn rewrite_result (
2116
+ & self ,
2117
+ context : & RewriteContext < ' _ > ,
2118
+ shape : Shape ,
2119
+ ) -> Result < String , RewriteError > {
2074
2120
match * self {
2075
- ast:: FnRetTy :: Default ( _) => Some ( String :: new ( ) ) ,
2121
+ ast:: FnRetTy :: Default ( _) => Ok ( String :: new ( ) ) ,
2076
2122
ast:: FnRetTy :: Ty ( ref ty) => {
2077
2123
if context. config . version ( ) == Version :: One
2078
2124
|| context. config . indent_style ( ) == IndentStyle :: Visual
2079
2125
{
2080
- let inner_width = shape. width . checked_sub ( 3 ) ?;
2126
+ let inner_width = shape. width . checked_sub ( 3 ) . ok_or_else ( || {
2127
+ RewriteError :: ExceedsMaxWidth {
2128
+ configured_width : shape. width ,
2129
+ span : self . span ( ) ,
2130
+ }
2131
+ } ) ?;
2081
2132
return ty
2082
- . rewrite ( context, Shape :: legacy ( inner_width, shape. indent + 3 ) )
2133
+ . rewrite_result ( context, Shape :: legacy ( inner_width, shape. indent + 3 ) )
2083
2134
. map ( |r| format ! ( "-> {}" , r) ) ;
2084
2135
}
2085
2136
2086
- ty. rewrite ( context, shape. offset_left ( 3 ) ?)
2087
- . map ( |s| format ! ( "-> {}" , s) )
2137
+ ty. rewrite_result (
2138
+ context,
2139
+ shape
2140
+ . offset_left ( 3 )
2141
+ . ok_or_else ( || RewriteError :: ExceedsMaxWidth {
2142
+ configured_width : shape. width ,
2143
+ span : self . span ( ) ,
2144
+ } ) ?,
2145
+ )
2146
+ . map ( |s| format ! ( "-> {}" , s) )
2088
2147
}
2089
2148
}
2090
2149
}
@@ -2135,9 +2194,17 @@ fn get_missing_param_comments(
2135
2194
2136
2195
impl Rewrite for ast:: Param {
2137
2196
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
2197
+ self . rewrite_result ( context, shape) . ok ( )
2198
+ }
2199
+
2200
+ fn rewrite_result (
2201
+ & self ,
2202
+ context : & RewriteContext < ' _ > ,
2203
+ shape : Shape ,
2204
+ ) -> Result < String , RewriteError > {
2138
2205
let param_attrs_result = self
2139
2206
. attrs
2140
- . rewrite ( context, Shape :: legacy ( shape. width , shape. indent ) ) ?;
2207
+ . rewrite_result ( context, Shape :: legacy ( shape. width , shape. indent ) ) ?;
2141
2208
// N.B. Doc comments aren't typically valid syntax, but could appear
2142
2209
// in the presence of certain macros - https://github.com/rust-lang/rustfmt/issues/4936
2143
2210
let ( span, has_multiple_attr_lines, has_doc_comments) = if !self . attrs . is_empty ( ) {
@@ -2160,18 +2227,20 @@ impl Rewrite for ast::Param {
2160
2227
shape,
2161
2228
has_multiple_attr_lines,
2162
2229
)
2230
+ . ok_or_else ( || RewriteError :: Unknown )
2163
2231
} else if is_named_param ( self ) {
2164
2232
let param_name = & self
2165
2233
. pat
2166
- . rewrite ( context, Shape :: legacy ( shape. width , shape. indent ) ) ?;
2234
+ . rewrite_result ( context, Shape :: legacy ( shape. width , shape. indent ) ) ?;
2167
2235
let mut result = combine_strs_with_missing_comments (
2168
2236
context,
2169
2237
& param_attrs_result,
2170
2238
param_name,
2171
2239
span,
2172
2240
shape,
2173
2241
!has_multiple_attr_lines && !has_doc_comments,
2174
- ) ?;
2242
+ )
2243
+ . ok_or_else ( || RewriteError :: Unknown ) ?;
2175
2244
2176
2245
if !is_empty_infer ( & * self . ty , self . pat . span ) {
2177
2246
let ( before_comment, after_comment) =
@@ -2180,10 +2249,15 @@ impl Rewrite for ast::Param {
2180
2249
result. push_str ( colon_spaces ( context. config ) ) ;
2181
2250
result. push_str ( & after_comment) ;
2182
2251
let overhead = last_line_width ( & result) ;
2183
- let max_width = shape. width . checked_sub ( overhead) ?;
2184
- if let Some ( ty_str) = self
2252
+ let max_width = shape. width . checked_sub ( overhead) . ok_or_else ( || {
2253
+ RewriteError :: ExceedsMaxWidth {
2254
+ configured_width : shape. width ,
2255
+ span : self . span ( ) ,
2256
+ }
2257
+ } ) ?;
2258
+ if let Ok ( ty_str) = self
2185
2259
. ty
2186
- . rewrite ( context, Shape :: legacy ( max_width, shape. indent ) )
2260
+ . rewrite_result ( context, Shape :: legacy ( max_width, shape. indent ) )
2187
2261
{
2188
2262
result. push_str ( & ty_str) ;
2189
2263
} else {
@@ -2200,22 +2274,28 @@ impl Rewrite for ast::Param {
2200
2274
span,
2201
2275
shape,
2202
2276
!has_multiple_attr_lines,
2203
- ) ?;
2277
+ )
2278
+ . ok_or_else ( || RewriteError :: Unknown ) ?;
2204
2279
result. push_str ( & before_comment) ;
2205
2280
result. push_str ( colon_spaces ( context. config ) ) ;
2206
2281
result. push_str ( & after_comment) ;
2207
2282
let overhead = last_line_width ( & result) ;
2208
- let max_width = shape. width . checked_sub ( overhead) ?;
2283
+ let max_width = shape. width . checked_sub ( overhead) . ok_or_else ( || {
2284
+ RewriteError :: ExceedsMaxWidth {
2285
+ configured_width : shape. width ,
2286
+ span : self . span ( ) ,
2287
+ }
2288
+ } ) ?;
2209
2289
let ty_str = self
2210
2290
. ty
2211
- . rewrite ( context, Shape :: legacy ( max_width, shape. indent ) ) ?;
2291
+ . rewrite_result ( context, Shape :: legacy ( max_width, shape. indent ) ) ?;
2212
2292
result. push_str ( & ty_str) ;
2213
2293
}
2214
2294
}
2215
2295
2216
- Some ( result)
2296
+ Ok ( result)
2217
2297
} else {
2218
- self . ty . rewrite ( context, shape)
2298
+ self . ty . rewrite_result ( context, shape)
2219
2299
}
2220
2300
}
2221
2301
}
0 commit comments