@@ -170,7 +170,7 @@ fn return_macro_parse_failure_fallback(
170
170
) -> Option < String > {
171
171
// Mark this as a failure however we format it
172
172
context. macro_rewrite_failure . replace ( true ) ;
173
-
173
+ context . macro_original_code_was_used . replace ( true ) ;
174
174
// Heuristically determine whether the last line of the macro uses "Block" style
175
175
// rather than using "Visual" style, or another indentation style.
176
176
let is_like_block_indent_style = context
@@ -229,6 +229,7 @@ pub(crate) fn rewrite_macro(
229
229
guard. is_nested ( ) ,
230
230
)
231
231
} ) ) ;
232
+
232
233
match result {
233
234
Err ( ..) | Ok ( None ) => {
234
235
context. macro_rewrite_failure . replace ( true ) ;
@@ -510,13 +511,17 @@ pub(crate) fn rewrite_macro_def(
510
511
) -> Option < String > {
511
512
let snippet = Some ( remove_trailing_white_spaces ( context. snippet ( span) ) ) ;
512
513
if snippet. as_ref ( ) . map_or ( true , |s| s. ends_with ( ';' ) ) {
514
+ context. macro_original_code_was_used . replace ( true ) ;
513
515
return snippet;
514
516
}
515
517
let ts = def. body . inner_tokens ( ) ;
516
518
let mut parser = MacroParser :: new ( ts. into_trees ( ) ) ;
517
519
let parsed_def = match parser. parse ( def. macro_rules ) {
518
520
Some ( def) => def,
519
- None => return snippet,
521
+ None => {
522
+ context. macro_original_code_was_used . replace ( true ) ;
523
+ return snippet;
524
+ }
520
525
} ;
521
526
522
527
let mut result = if def. macro_rules {
@@ -527,7 +532,6 @@ pub(crate) fn rewrite_macro_def(
527
532
528
533
result += " " ;
529
534
result += rewrite_ident ( context, ident) ;
530
-
531
535
let multi_branch_style = def. macro_rules || parsed_def. branches . len ( ) != 1 ;
532
536
533
537
let arm_shape = if multi_branch_style {
@@ -550,6 +554,7 @@ pub(crate) fn rewrite_macro_def(
550
554
// if the rewrite returned None because a macro could not be rewritten, then return the
551
555
// original body
552
556
None if context. macro_rewrite_failure . get ( ) => {
557
+ context. macro_original_code_was_used . replace ( true ) ;
553
558
Some ( context. snippet ( branch. body ) . trim ( ) . to_string ( ) )
554
559
}
555
560
None => None ,
@@ -576,14 +581,16 @@ pub(crate) fn rewrite_macro_def(
576
581
577
582
match write_list ( & branch_items, & fmt) {
578
583
Some ( ref s) => result += s,
579
- None => return snippet,
584
+ None => {
585
+ context. macro_original_code_was_used . replace ( true ) ;
586
+ return snippet;
587
+ }
580
588
}
581
589
582
590
if multi_branch_style {
583
591
result += & indent. to_string_with_newline ( context. config ) ;
584
592
result += "}" ;
585
593
}
586
-
587
594
Some ( result)
588
595
}
589
596
@@ -1274,7 +1281,8 @@ impl MacroParser {
1274
1281
branches. push ( self . parse_branch ( is_macro_rules) ?) ;
1275
1282
}
1276
1283
1277
- Some ( Macro { branches } )
1284
+ let ret = Some ( Macro { branches } ) ;
1285
+ ret
1278
1286
}
1279
1287
1280
1288
// `(` ... `)` `=>` `{` ... `}`
@@ -1356,6 +1364,10 @@ impl MacroBranch {
1356
1364
result += " =>" ;
1357
1365
}
1358
1366
1367
+ // Prepare for restoring original macro body if original code was use during formatting
1368
+ // (since in this case indentation done by this function doesn't work properly).
1369
+ let result_without_body = result. clone ( ) ;
1370
+
1359
1371
if !context. config . format_macro_bodies ( ) {
1360
1372
result += " " ;
1361
1373
result += context. snippet ( self . whole_body ) ;
@@ -1386,23 +1398,44 @@ impl MacroBranch {
1386
1398
config. set ( ) . max_width ( new_width) ;
1387
1399
1388
1400
// First try to format as items, then as statements.
1389
- let new_body_snippet = match format_snippet ( & body_str, & config, true ) {
1390
- Some ( new_body) => new_body,
1391
- None => {
1392
- let new_width = new_width + config. tab_spaces ( ) ;
1393
- config. set ( ) . max_width ( new_width) ;
1394
- match format_code_block ( & body_str, & config, true ) {
1395
- Some ( new_body) => new_body,
1396
- None => return None ,
1401
+
1402
+ let mut macro_original_code_was_used = false ;
1403
+
1404
+ let new_body_snippet =
1405
+ match format_snippet ( & body_str, & config, true , & mut macro_original_code_was_used) {
1406
+ Some ( new_body) => new_body,
1407
+ None => {
1408
+ let new_width = new_width + config. tab_spaces ( ) ;
1409
+ config. set ( ) . max_width ( new_width) ;
1410
+ match format_code_block (
1411
+ & body_str,
1412
+ & config,
1413
+ true ,
1414
+ & mut macro_original_code_was_used,
1415
+ ) {
1416
+ Some ( new_body) => new_body,
1417
+ None => {
1418
+ return None ;
1419
+ }
1420
+ }
1397
1421
}
1398
- }
1399
- } ;
1422
+ } ;
1423
+
1400
1424
let new_body = wrap_str (
1401
1425
new_body_snippet. as_ref ( ) . to_owned ( ) ,
1402
1426
config. max_width ( ) ,
1403
1427
shape,
1404
1428
) ?;
1405
1429
1430
+ // If original code was used during the macro body formatting -
1431
+ // use whole original body without formatting
1432
+ if macro_original_code_was_used {
1433
+ result = result_without_body;
1434
+ result += " " ;
1435
+ result += context. snippet ( self . whole_body ) ;
1436
+ return Some ( result) ;
1437
+ }
1438
+
1406
1439
// Indent the body since it is in a block.
1407
1440
let indent_str = body_indent. to_string ( & config) ;
1408
1441
let mut new_body = LineClasses :: new ( new_body. trim_end ( ) )
@@ -1425,7 +1458,6 @@ impl MacroBranch {
1425
1458
// FIXME: this could be *much* more efficient.
1426
1459
for ( old, new) in & substs {
1427
1460
if old_body. contains ( new) {
1428
- debug ! ( "rewrite_macro_def: bailing matching variable: `{}`" , new) ;
1429
1461
return None ;
1430
1462
}
1431
1463
new_body = new_body. replace ( new, old) ;
@@ -1576,6 +1608,11 @@ fn rewrite_macro_with_items(
1576
1608
result. push_str ( & shape. indent . to_string_with_newline ( context. config ) ) ;
1577
1609
result. push_str ( closer) ;
1578
1610
result. push_str ( trailing_semicolon) ;
1611
+
1612
+ if visitor. macro_original_code_was_used . get ( ) {
1613
+ context. macro_original_code_was_used . replace ( true ) ;
1614
+ }
1615
+
1579
1616
Some ( result)
1580
1617
}
1581
1618
0 commit comments