@@ -629,15 +629,17 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
629629 ast:: Safety :: Default | ast:: Safety :: Safe ( _) => { }
630630 }
631631 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+ ) ,
641643 AttrArgs :: Empty => {
642644 self . print_path ( & item. path , false , 0 ) ;
643645 }
@@ -679,6 +681,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
679681 false ,
680682 None ,
681683 * delim,
684+ spacing. open ,
682685 tts,
683686 convert_dollar_crate,
684687 dspan. entire ( ) ,
@@ -735,6 +738,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
735738 has_bang : bool ,
736739 ident : Option < Ident > ,
737740 delim : Delimiter ,
741+ open_spacing : Spacing ,
738742 tts : & TokenStream ,
739743 convert_dollar_crate : bool ,
740744 span : Span ,
@@ -760,16 +764,25 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
760764 self . nbsp ( ) ;
761765 }
762766 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 {
764771 self . space ( ) ;
765772 }
766773 self . ibox ( 0 ) ;
767774 self . print_tts ( tts, convert_dollar_crate) ;
768775 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) ;
771782 }
772783 delim => {
784+ // `open_spacing` is ignored. We never print spaces after
785+ // non-brace opening delims or before non-brace closing delims.
773786 let token_str = self . token_kind_to_string ( & delim. as_open_token_kind ( ) ) ;
774787 self . word ( token_str) ;
775788 self . ibox ( 0 ) ;
@@ -799,6 +812,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
799812 has_bang,
800813 Some ( * ident) ,
801814 macro_def. body . delim ,
815+ macro_def. body . open_spacing ,
802816 & macro_def. body . tokens ,
803817 true ,
804818 sp,
@@ -845,9 +859,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
845859 self . end ( ) ; // Close the head-box.
846860 }
847861
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 ) {
849863 let has_comment = self . maybe_print_comment ( span. hi ( ) ) ;
850- if !empty || has_comment {
864+ if !no_space || has_comment {
851865 self . break_offset_if_not_bol ( 1 , -INDENT_UNIT ) ;
852866 }
853867 self . word ( "}" ) ;
@@ -856,9 +870,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
856870 }
857871 }
858872
859- fn bclose ( & mut self , span : rustc_span:: Span , empty : bool ) {
873+ fn bclose ( & mut self , span : rustc_span:: Span , no_space : bool ) {
860874 let close_box = true ;
861- self . bclose_maybe_open ( span, empty , close_box)
875+ self . bclose_maybe_open ( span, no_space , close_box)
862876 }
863877
864878 fn break_offset_if_not_bol ( & mut self , n : usize , off : isize ) {
@@ -1420,8 +1434,8 @@ impl<'a> State<'a> {
14201434 }
14211435 }
14221436
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) ;
14251439 self . ann . post ( self , AnnNode :: Block ( blk) )
14261440 }
14271441
@@ -1468,6 +1482,7 @@ impl<'a> State<'a> {
14681482 true ,
14691483 None ,
14701484 m. args . delim ,
1485+ m. args . open_spacing ,
14711486 & m. args . tokens ,
14721487 true ,
14731488 m. span ( ) ,
0 commit comments