@@ -629,15 +629,17 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
629
629
ast:: Safety :: Default | ast:: Safety :: Safe ( _) => { }
630
630
}
631
631
match & item. args {
632
- AttrArgs :: Delimited ( DelimArgs { dspan : _, delim, tokens } ) => self . print_mac_common (
633
- Some ( MacHeader :: Path ( & item. path ) ) ,
634
- false ,
635
- None ,
636
- * delim,
637
- tokens,
638
- true ,
639
- span,
640
- ) ,
632
+ AttrArgs :: Delimited ( DelimArgs { dspan : _, open_spacing, delim, tokens } ) => self
633
+ . print_mac_common (
634
+ Some ( MacHeader :: Path ( & item. path ) ) ,
635
+ false ,
636
+ None ,
637
+ * delim,
638
+ * open_spacing,
639
+ tokens,
640
+ true ,
641
+ span,
642
+ ) ,
641
643
AttrArgs :: Empty => {
642
644
self . print_path ( & item. path , false , 0 ) ;
643
645
}
@@ -679,6 +681,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
679
681
false ,
680
682
None ,
681
683
* delim,
684
+ spacing. open ,
682
685
tts,
683
686
convert_dollar_crate,
684
687
dspan. entire ( ) ,
@@ -735,6 +738,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
735
738
has_bang : bool ,
736
739
ident : Option < Ident > ,
737
740
delim : Delimiter ,
741
+ open_spacing : Spacing ,
738
742
tts : & TokenStream ,
739
743
convert_dollar_crate : bool ,
740
744
span : Span ,
@@ -760,16 +764,25 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
760
764
self . nbsp ( ) ;
761
765
}
762
766
self . word ( "{" ) ;
763
- if !tts. is_empty ( ) {
767
+
768
+ // Respect `Alone` and print a space, unless the list is empty.
769
+ let open_space = open_spacing == Spacing :: Alone && !tts. is_empty ( ) ;
770
+ if open_space {
764
771
self . space ( ) ;
765
772
}
766
773
self . ibox ( 0 ) ;
767
774
self . print_tts ( tts, convert_dollar_crate) ;
768
775
self . end ( ) ;
769
- let empty = tts. is_empty ( ) ;
770
- self . bclose ( span, empty) ;
776
+
777
+ // Use `spacing.open` for the spacing *before* the closing
778
+ // delim. Because spacing on delimiters is lost when going
779
+ // through proc macros, and otherwise we can end up with ugly
780
+ // cases like `{ x}`. Symmetry is better.
781
+ self . bclose ( span, !open_space) ;
771
782
}
772
783
delim => {
784
+ // `open_spacing` is ignored. We never print spaces after
785
+ // non-brace opening delims or before non-brace closing delims.
773
786
let token_str = self . token_kind_to_string ( & delim. as_open_token_kind ( ) ) ;
774
787
self . word ( token_str) ;
775
788
self . ibox ( 0 ) ;
@@ -799,6 +812,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
799
812
has_bang,
800
813
Some ( * ident) ,
801
814
macro_def. body . delim ,
815
+ macro_def. body . open_spacing ,
802
816
& macro_def. body . tokens ,
803
817
true ,
804
818
sp,
@@ -845,9 +859,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
845
859
self . end ( ) ; // Close the head-box.
846
860
}
847
861
848
- fn bclose_maybe_open ( & mut self , span : rustc_span:: Span , empty : bool , close_box : bool ) {
862
+ fn bclose_maybe_open ( & mut self , span : rustc_span:: Span , no_space : bool , close_box : bool ) {
849
863
let has_comment = self . maybe_print_comment ( span. hi ( ) ) ;
850
- if !empty || has_comment {
864
+ if !no_space || has_comment {
851
865
self . break_offset_if_not_bol ( 1 , -INDENT_UNIT ) ;
852
866
}
853
867
self . word ( "}" ) ;
@@ -856,9 +870,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
856
870
}
857
871
}
858
872
859
- fn bclose ( & mut self , span : rustc_span:: Span , empty : bool ) {
873
+ fn bclose ( & mut self , span : rustc_span:: Span , no_space : bool ) {
860
874
let close_box = true ;
861
- self . bclose_maybe_open ( span, empty , close_box)
875
+ self . bclose_maybe_open ( span, no_space , close_box)
862
876
}
863
877
864
878
fn break_offset_if_not_bol ( & mut self , n : usize , off : isize ) {
@@ -1420,8 +1434,8 @@ impl<'a> State<'a> {
1420
1434
}
1421
1435
}
1422
1436
1423
- let empty = !has_attrs && blk. stmts . is_empty ( ) ;
1424
- self . bclose_maybe_open ( blk. span , empty , close_box) ;
1437
+ let no_space = !has_attrs && blk. stmts . is_empty ( ) ;
1438
+ self . bclose_maybe_open ( blk. span , no_space , close_box) ;
1425
1439
self . ann . post ( self , AnnNode :: Block ( blk) )
1426
1440
}
1427
1441
@@ -1468,6 +1482,7 @@ impl<'a> State<'a> {
1468
1482
true ,
1469
1483
None ,
1470
1484
m. args . delim ,
1485
+ m. args . open_spacing ,
1471
1486
& m. args . tokens ,
1472
1487
true ,
1473
1488
m. span ( ) ,
0 commit comments