@@ -11,7 +11,8 @@ use crate::config::IndentStyle;
11
11
use crate :: expr:: rewrite_literal;
12
12
use crate :: lists:: { definitive_tactic, itemize_list, write_list, ListFormatting , Separator } ;
13
13
use crate :: overflow;
14
- use crate :: rewrite:: { Rewrite , RewriteContext } ;
14
+ use crate :: rewrite:: RewriteErrorExt ;
15
+ use crate :: rewrite:: { Rewrite , RewriteContext , RewriteResult } ;
15
16
use crate :: shape:: Shape ;
16
17
use crate :: source_map:: SpanUtils ;
17
18
use crate :: types:: { rewrite_path, PathContext } ;
@@ -244,8 +245,14 @@ fn rewrite_initial_doc_comments(
244
245
245
246
impl Rewrite for ast:: NestedMetaItem {
246
247
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
248
+ self . rewrite_result ( context, shape) . ok ( )
249
+ }
250
+
251
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
247
252
match self {
248
- ast:: NestedMetaItem :: MetaItem ( ref meta_item) => meta_item. rewrite ( context, shape) ,
253
+ ast:: NestedMetaItem :: MetaItem ( ref meta_item) => {
254
+ meta_item. rewrite_result ( context, shape)
255
+ }
249
256
ast:: NestedMetaItem :: Lit ( ref l) => {
250
257
rewrite_literal ( context, l. as_token_lit ( ) , l. span , shape)
251
258
}
@@ -274,42 +281,47 @@ fn has_newlines_before_after_comment(comment: &str) -> (&str, &str) {
274
281
275
282
impl Rewrite for ast:: MetaItem {
276
283
fn rewrite ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> Option < String > {
277
- Some ( match self . kind {
284
+ self . rewrite_result ( context, shape) . ok ( )
285
+ }
286
+
287
+ fn rewrite_result ( & self , context : & RewriteContext < ' _ > , shape : Shape ) -> RewriteResult {
288
+ Ok ( match self . kind {
278
289
ast:: MetaItemKind :: Word => {
279
- rewrite_path ( context, PathContext :: Type , & None , & self . path , shape) . ok ( ) ?
290
+ rewrite_path ( context, PathContext :: Type , & None , & self . path , shape) ?
280
291
}
281
292
ast:: MetaItemKind :: List ( ref list) => {
282
- let path =
283
- rewrite_path ( context, PathContext :: Type , & None , & self . path , shape) . ok ( ) ?;
293
+ let path = rewrite_path ( context, PathContext :: Type , & None , & self . path , shape) ?;
284
294
let has_trailing_comma = crate :: expr:: span_ends_with_comma ( context, self . span ) ;
285
295
overflow:: rewrite_with_parens (
286
296
context,
287
297
& path,
288
298
list. iter ( ) ,
289
299
// 1 = "]"
290
- shape. sub_width ( 1 ) ?,
300
+ shape. sub_width ( 1 ) . max_width_error ( shape . width , self . span ) ?,
291
301
self . span ,
292
302
context. config . attr_fn_like_width ( ) ,
293
303
Some ( if has_trailing_comma {
294
304
SeparatorTactic :: Always
295
305
} else {
296
306
SeparatorTactic :: Never
297
307
} ) ,
298
- ) ?
308
+ )
309
+ . unknown_error ( ) ? // TODO rebase?
299
310
}
300
311
ast:: MetaItemKind :: NameValue ( ref lit) => {
301
- let path =
302
- rewrite_path ( context, PathContext :: Type , & None , & self . path , shape) . ok ( ) ?;
312
+ let path = rewrite_path ( context, PathContext :: Type , & None , & self . path , shape) ?;
303
313
// 3 = ` = `
304
- let lit_shape = shape. shrink_left ( path. len ( ) + 3 ) ?;
314
+ let lit_shape = shape
315
+ . shrink_left ( path. len ( ) + 3 )
316
+ . max_width_error ( shape. width , self . span ) ?;
305
317
// `rewrite_literal` returns `None` when `lit` exceeds max
306
318
// width. Since a literal is basically unformattable unless it
307
319
// is a string literal (and only if `format_strings` is set),
308
320
// we might be better off ignoring the fact that the attribute
309
321
// is longer than the max width and continue on formatting.
310
322
// See #2479 for example.
311
323
let value = rewrite_literal ( context, lit. as_token_lit ( ) , lit. span , lit_shape)
312
- . unwrap_or_else ( || context. snippet ( lit. span ) . to_owned ( ) ) ;
324
+ . unwrap_or_else ( |_ | context. snippet ( lit. span ) . to_owned ( ) ) ;
313
325
format ! ( "{path} = {value}" )
314
326
}
315
327
} )
0 commit comments