Skip to content

Commit 42d104a

Browse files
authored
Unrolled build for rust-lang#140312
Rollup merge of rust-lang#140312 - nnethercote:DelimArgs-spacing, r=petrochenkov Improve pretty-printing of braces r? ````@petrochenkov````
2 parents d2eadb7 + 99f6b63 commit 42d104a

File tree

9 files changed

+63
-30
lines changed

9 files changed

+63
-30
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+24-9
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
634634
false,
635635
None,
636636
*delim,
637+
None,
637638
tokens,
638639
true,
639640
span,
@@ -679,6 +680,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
679680
false,
680681
None,
681682
*delim,
683+
Some(spacing.open),
682684
tts,
683685
convert_dollar_crate,
684686
dspan.entire(),
@@ -735,6 +737,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
735737
has_bang: bool,
736738
ident: Option<Ident>,
737739
delim: Delimiter,
740+
open_spacing: Option<Spacing>,
738741
tts: &TokenStream,
739742
convert_dollar_crate: bool,
740743
span: Span,
@@ -758,16 +761,26 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
758761
self.nbsp();
759762
}
760763
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 {
762769
self.space();
763770
}
764771
let ib = self.ibox(0);
765772
self.print_tts(tts, convert_dollar_crate);
766773
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());
769780
}
770781
delim => {
782+
// `open_spacing` is ignored. We never print spaces after
783+
// non-brace opening delims or before non-brace closing delims.
771784
let token_str = self.token_kind_to_string(&delim.as_open_token_kind());
772785
self.word(token_str);
773786
let ib = self.ibox(0);
@@ -797,6 +810,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
797810
has_bang,
798811
Some(*ident),
799812
macro_def.body.delim,
813+
None,
800814
&macro_def.body.tokens,
801815
true,
802816
sp,
@@ -844,9 +858,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
844858
self.end(ib);
845859
}
846860

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>) {
848862
let has_comment = self.maybe_print_comment(span.hi());
849-
if !empty || has_comment {
863+
if !no_space || has_comment {
850864
self.break_offset_if_not_bol(1, -INDENT_UNIT);
851865
}
852866
self.word("}");
@@ -855,9 +869,9 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
855869
}
856870
}
857871

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) {
859873
let cb = Some(cb);
860-
self.bclose_maybe_open(span, empty, cb)
874+
self.bclose_maybe_open(span, no_space, cb)
861875
}
862876

863877
fn break_offset_if_not_bol(&mut self, n: usize, off: isize) {
@@ -1434,8 +1448,8 @@ impl<'a> State<'a> {
14341448
}
14351449
}
14361450

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);
14391453
self.ann.post(self, AnnNode::Block(blk))
14401454
}
14411455

@@ -1482,6 +1496,7 @@ impl<'a> State<'a> {
14821496
true,
14831497
None,
14841498
m.args.delim,
1499+
None,
14851500
&m.args.tokens,
14861501
true,
14871502
m.span(),

compiler/rustc_builtin_macros/src/autodiff.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -323,9 +323,9 @@ mod llvm_enzyme {
323323
Spacing::Joint,
324324
)];
325325
let never_arg = ast::DelimArgs {
326-
dspan: ast::tokenstream::DelimSpan::from_single(span),
326+
dspan: DelimSpan::from_single(span),
327327
delim: ast::token::Delimiter::Parenthesis,
328-
tokens: ast::tokenstream::TokenStream::from_iter(ts2),
328+
tokens: TokenStream::from_iter(ts2),
329329
};
330330
let inline_item = ast::AttrItem {
331331
unsafety: ast::Safety::Default,

compiler/rustc_expand/src/build.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
use rustc_ast::ptr::P;
2+
use rustc_ast::token::Delimiter;
3+
use rustc_ast::tokenstream::TokenStream;
24
use rustc_ast::util::literal;
35
use rustc_ast::{
46
self as ast, AnonConst, AttrVec, BlockCheckMode, Expr, LocalKind, MatchKind, PatKind, UnOp,
5-
attr, token,
7+
attr, token, tokenstream,
68
};
79
use rustc_span::source_map::Spanned;
810
use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
@@ -55,13 +57,13 @@ impl<'a> ExtCtxt<'a> {
5557
&self,
5658
span: Span,
5759
path: ast::Path,
58-
delim: ast::token::Delimiter,
59-
tokens: ast::tokenstream::TokenStream,
60+
delim: Delimiter,
61+
tokens: TokenStream,
6062
) -> P<ast::MacCall> {
6163
P(ast::MacCall {
6264
path,
6365
args: P(ast::DelimArgs {
64-
dspan: ast::tokenstream::DelimSpan { open: span, close: span },
66+
dspan: tokenstream::DelimSpan { open: span, close: span },
6567
delim,
6668
tokens,
6769
}),
@@ -480,8 +482,8 @@ impl<'a> ExtCtxt<'a> {
480482
span,
481483
[sym::std, sym::unreachable].map(|s| Ident::new(s, span)).to_vec(),
482484
),
483-
ast::token::Delimiter::Parenthesis,
484-
ast::tokenstream::TokenStream::default(),
485+
Delimiter::Parenthesis,
486+
TokenStream::default(),
485487
),
486488
)
487489
}

compiler/rustc_hir_pretty/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ impl<'a> State<'a> {
146146
false,
147147
None,
148148
*delim,
149+
None,
149150
&tokens,
150151
true,
151152
span,

tests/ui/macros/stringify.rs

+19-3
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,9 @@ fn test_expr() {
288288
// ExprKind::OffsetOf: untestable because this test works pre-expansion.
289289

290290
// ExprKind::MacCall
291+
c1!(expr, [ mac!() ], "mac!()");
292+
c1!(expr, [ mac![] ], "mac![]");
293+
c1!(expr, [ mac! {} ], "mac! {}");
291294
c1!(expr, [ mac!(...) ], "mac!(...)");
292295
c1!(expr, [ mac![...] ], "mac![...]");
293296
c1!(expr, [ mac! { ... } ], "mac! { ... }");
@@ -353,7 +356,8 @@ fn test_item() {
353356
c1!(item, [ pub extern crate self as std; ], "pub extern crate self as std;");
354357

355358
// ItemKind::Use
356-
c1!(item, [ pub use crate::{a, b::c}; ], "pub use crate::{ a, b::c };"); // FIXME
359+
c1!(item, [ pub use crate::{a, b::c}; ], "pub use crate::{a, b::c};");
360+
c1!(item, [ pub use crate::{ e, ff }; ], "pub use crate::{ e, ff };");
357361
c1!(item, [ pub use A::*; ], "pub use A::*;");
358362

359363
// ItemKind::Static
@@ -482,9 +486,12 @@ fn test_item() {
482486
c1!(item, [ impl ~const Struct {} ], "impl ~const Struct {}");
483487

484488
// ItemKind::MacCall
489+
c1!(item, [ mac!(); ], "mac!();");
490+
c1!(item, [ mac![]; ], "mac![];");
491+
c1!(item, [ mac! {} ], "mac! {}");
485492
c1!(item, [ mac!(...); ], "mac!(...);");
486493
c1!(item, [ mac![...]; ], "mac![...];");
487-
c1!(item, [ mac! { ... } ], "mac! { ... }");
494+
c1!(item, [ mac! {...} ], "mac! {...}");
488495

489496
// ItemKind::MacroDef
490497
c1!(item,
@@ -598,8 +605,11 @@ fn test_pat() {
598605
c1!(pat, [ (pat) ], "(pat)");
599606

600607
// PatKind::MacCall
608+
c1!(pat, [ mac!() ], "mac!()");
609+
c1!(pat, [ mac![] ], "mac![]");
610+
c1!(pat, [ mac! {} ], "mac! {}");
601611
c1!(pat, [ mac!(...) ], "mac!(...)");
602-
c1!(pat, [ mac![...] ], "mac![...]");
612+
c1!(pat, [ mac! [ ... ] ], "mac! [...]");
603613
c1!(pat, [ mac! { ... } ], "mac! { ... }");
604614

605615
// Attributes are not allowed on patterns.
@@ -644,6 +654,9 @@ fn test_stmt() {
644654
c1!(stmt, [ ; ], ";");
645655

646656
// StmtKind::MacCall
657+
c1!(stmt, [ mac! ( ) ], "mac! ()");
658+
c1!(stmt, [ mac![] ], "mac![]");
659+
c1!(stmt, [ mac!{} ], "mac!{}");
647660
c1!(stmt, [ mac!(...) ], "mac!(...)");
648661
c1!(stmt, [ mac![...] ], "mac![...]");
649662
c1!(stmt, [ mac! { ... } ], "mac! { ... }");
@@ -739,6 +752,9 @@ fn test_ty() {
739752
// TyKind::ImplicitSelf: there is no syntax for this.
740753

741754
// TyKind::MacCall
755+
c1!(ty, [ mac!() ], "mac!()");
756+
c1!(ty, [ mac![] ], "mac![]");
757+
c1!(ty, [ mac! { } ], "mac! {}");
742758
c1!(ty, [ mac!(...) ], "mac!(...)");
743759
c1!(ty, [ mac![...] ], "mac![...]");
744760
c1!(ty, [ mac! { ... } ], "mac! { ... }");

tests/ui/macros/trace_faulty_macros.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ LL | let a = pat_macro!();
8787
| ^^^^^^^^^^^^
8888
|
8989
= note: expanding `pat_macro! { }`
90-
= note: to `pat_macro! (A { a : a, b : 0, c : _, .. });`
91-
= note: expanding `pat_macro! { A { a : a, b : 0, c : _, .. } }`
92-
= note: to `A { a : a, b : 0, c : _, .. }`
90+
= note: to `pat_macro! (A {a : a, b : 0, c : _, ..});`
91+
= note: expanding `pat_macro! { A {a : a, b : 0, c : _, ..} }`
92+
= note: to `A {a : a, b : 0, c : _, ..}`
9393

9494
note: trace_macro
9595
--> $DIR/trace_faulty_macros.rs:53:5

tests/ui/proc-macro/attr-complex-fn.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ PRINT-ATTR INPUT (DEBUG): TokenStream [
7777
span: $DIR/attr-complex-fn.rs:19:42: 19:44 (#0),
7878
},
7979
]
80-
PRINT-ATTR INPUT (DISPLAY): impl<T> MyTrait<T> for MyStruct<{ true }> { #![rustc_dummy] }
80+
PRINT-ATTR INPUT (DISPLAY): impl<T> MyTrait<T> for MyStruct<{true}> { #![rustc_dummy] }
8181
PRINT-ATTR RE-COLLECTED (DISPLAY): impl < T > MyTrait < T > for MyStruct < { true } > { #![rustc_dummy] }
8282
PRINT-ATTR DEEP-RE-COLLECTED (DISPLAY): impl < T > MyTrait < T > for MyStruct < { true } > { #! [rustc_dummy] }
8383
PRINT-ATTR INPUT (DEBUG): TokenStream [

tests/ui/proc-macro/weird-braces.stdout

+4-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ PRINT-ATTR_ARGS INPUT (DEBUG): TokenStream [
55
span: $DIR/weird-braces.rs:16:25: 16:36 (#0),
66
},
77
]
8-
PRINT-ATTR INPUT (DISPLAY): #[print_target_and_args(second_outer)] impl Bar<{ 1 > 0 }> for Foo<{ true }>
8+
PRINT-ATTR INPUT (DISPLAY): #[print_target_and_args(second_outer)] impl Bar<{1 > 0}> for Foo<{true}>
99
{
1010
#![print_target_and_args(first_inner)]
1111
#![print_target_and_args(second_inner)]
@@ -191,7 +191,7 @@ PRINT-ATTR_ARGS INPUT (DEBUG): TokenStream [
191191
span: $DIR/weird-braces.rs:17:25: 17:37 (#0),
192192
},
193193
]
194-
PRINT-ATTR INPUT (DISPLAY): impl Bar<{ 1 > 0 }> for Foo<{ true }>
194+
PRINT-ATTR INPUT (DISPLAY): impl Bar<{1 > 0}> for Foo<{true}>
195195
{
196196
#![print_target_and_args(first_inner)]
197197
#![print_target_and_args(second_inner)]
@@ -350,8 +350,7 @@ PRINT-ATTR_ARGS INPUT (DEBUG): TokenStream [
350350
span: $DIR/weird-braces.rs:19:30: 19:41 (#0),
351351
},
352352
]
353-
PRINT-ATTR INPUT (DISPLAY): impl Bar<{ 1 > 0 }> for Foo<{ true }>
354-
{ #![print_target_and_args(second_inner)] }
353+
PRINT-ATTR INPUT (DISPLAY): impl Bar<{1 > 0}> for Foo<{true}> { #![print_target_and_args(second_inner)] }
355354
PRINT-ATTR RE-COLLECTED (DISPLAY): impl Bar < { 1 > 0 } > for Foo < { true } >
356355
{ #![print_target_and_args(second_inner)] }
357356
PRINT-ATTR DEEP-RE-COLLECTED (DISPLAY): impl Bar < { 1 > 0 } > for Foo < { true } >
@@ -470,7 +469,7 @@ PRINT-ATTR_ARGS INPUT (DEBUG): TokenStream [
470469
span: $DIR/weird-braces.rs:20:30: 20:42 (#0),
471470
},
472471
]
473-
PRINT-ATTR INPUT (DISPLAY): impl Bar<{ 1 > 0 }> for Foo<{ true }> {}
472+
PRINT-ATTR INPUT (DISPLAY): impl Bar<{1 > 0}> for Foo<{true}> {}
474473
PRINT-ATTR RE-COLLECTED (DISPLAY): impl Bar < { 1 > 0 } > for Foo < { true } > {}
475474
PRINT-ATTR INPUT (DEBUG): TokenStream [
476475
Ident {

tests/ui/unpretty/expanded-exhaustive.stdout

+1-1
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ mod items {
505505
mod item_mac_call { }
506506
/// ItemKind::MacroDef
507507
mod item_macro_def {
508-
macro_rules! mac { () => { ... }; }
508+
macro_rules! mac { () => {...}; }
509509
pub macro stringify { () => {} }
510510
}
511511
/// ItemKind::Delegation

0 commit comments

Comments
 (0)