@@ -634,6 +634,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
634
634
false ,
635
635
None ,
636
636
* delim,
637
+ None ,
637
638
tokens,
638
639
true ,
639
640
span,
@@ -679,6 +680,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
679
680
false ,
680
681
None ,
681
682
* delim,
683
+ Some ( spacing. open ) ,
682
684
tts,
683
685
convert_dollar_crate,
684
686
dspan. entire ( ) ,
@@ -735,6 +737,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
735
737
has_bang : bool ,
736
738
ident : Option < Ident > ,
737
739
delim : Delimiter ,
740
+ open_spacing : Option < Spacing > ,
738
741
tts : & TokenStream ,
739
742
convert_dollar_crate : bool ,
740
743
span : Span ,
@@ -758,16 +761,26 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
758
761
self . nbsp ( ) ;
759
762
}
760
763
self . word ( "{" ) ;
761
- if !tts. is_empty ( ) {
764
+
765
+ // Respect `Alone`, if provided, and print a space. Unless the list is empty.
766
+ let open_space = ( open_spacing == None || open_spacing == Some ( Spacing :: Alone ) )
767
+ && !tts. is_empty ( ) ;
768
+ if open_space {
762
769
self . space ( ) ;
763
770
}
764
771
let ib = self . ibox ( 0 ) ;
765
772
self . print_tts ( tts, convert_dollar_crate) ;
766
773
self . end ( ib) ;
767
- let empty = tts. is_empty ( ) ;
768
- self . bclose ( span, empty, cb. unwrap ( ) ) ;
774
+
775
+ // Use `open_space` for the spacing *before* the closing delim.
776
+ // Because spacing on delimiters is lost when going through
777
+ // proc macros, and otherwise we can end up with ugly cases
778
+ // like `{ x}`. Symmetry is better.
779
+ self . bclose ( span, !open_space, cb. unwrap ( ) ) ;
769
780
}
770
781
delim => {
782
+ // `open_spacing` is ignored. We never print spaces after
783
+ // non-brace opening delims or before non-brace closing delims.
771
784
let token_str = self . token_kind_to_string ( & delim. as_open_token_kind ( ) ) ;
772
785
self . word ( token_str) ;
773
786
let ib = self . ibox ( 0 ) ;
@@ -797,6 +810,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
797
810
has_bang,
798
811
Some ( * ident) ,
799
812
macro_def. body . delim ,
813
+ None ,
800
814
& macro_def. body . tokens ,
801
815
true ,
802
816
sp,
@@ -844,9 +858,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
844
858
self . end ( ib) ;
845
859
}
846
860
847
- fn bclose_maybe_open ( & mut self , span : rustc_span:: Span , empty : bool , cb : Option < BoxMarker > ) {
861
+ fn bclose_maybe_open ( & mut self , span : rustc_span:: Span , no_space : bool , cb : Option < BoxMarker > ) {
848
862
let has_comment = self . maybe_print_comment ( span. hi ( ) ) ;
849
- if !empty || has_comment {
863
+ if !no_space || has_comment {
850
864
self . break_offset_if_not_bol ( 1 , -INDENT_UNIT ) ;
851
865
}
852
866
self . word ( "}" ) ;
@@ -855,9 +869,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
855
869
}
856
870
}
857
871
858
- fn bclose ( & mut self , span : rustc_span:: Span , empty : bool , cb : BoxMarker ) {
872
+ fn bclose ( & mut self , span : rustc_span:: Span , no_space : bool , cb : BoxMarker ) {
859
873
let cb = Some ( cb) ;
860
- self . bclose_maybe_open ( span, empty , cb)
874
+ self . bclose_maybe_open ( span, no_space , cb)
861
875
}
862
876
863
877
fn break_offset_if_not_bol ( & mut self , n : usize , off : isize ) {
@@ -1434,8 +1448,8 @@ impl<'a> State<'a> {
1434
1448
}
1435
1449
}
1436
1450
1437
- let empty = !has_attrs && blk. stmts . is_empty ( ) ;
1438
- self . bclose_maybe_open ( blk. span , empty , cb) ;
1451
+ let no_space = !has_attrs && blk. stmts . is_empty ( ) ;
1452
+ self . bclose_maybe_open ( blk. span , no_space , cb) ;
1439
1453
self . ann . post ( self , AnnNode :: Block ( blk) )
1440
1454
}
1441
1455
@@ -1482,6 +1496,7 @@ impl<'a> State<'a> {
1482
1496
true ,
1483
1497
None ,
1484
1498
m. args . delim ,
1499
+ None ,
1485
1500
& m. args . tokens ,
1486
1501
true ,
1487
1502
m. span ( ) ,
0 commit comments