Skip to content

Commit 5583010

Browse files
authored
Rollup merge of rust-lang#92371 - dtolnay:attrblock, r=oli-obk
Remove pretty printer space inside block with only outer attrs Follow-up to rust-lang#92238 fixing one of the FIXMEs. ```rust macro_rules! repro { ($expr:expr) => { stringify!($expr) }; } fn main() { println!("{}", repro!(#[attr] {})); } ``` Before: `#[attr] { }` After: `#[attr] {}`
2 parents c82b2bc + cbccc4a commit 5583010

File tree

7 files changed

+24
-23
lines changed

7 files changed

+24
-23
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -387,23 +387,23 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
387387
self.print_string(sym.as_str(), style);
388388
}
389389

390-
fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) {
390+
fn print_inner_attributes(&mut self, attrs: &[ast::Attribute]) -> bool {
391391
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, true)
392392
}
393393

394-
fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) {
394+
fn print_inner_attributes_no_trailing_hardbreak(&mut self, attrs: &[ast::Attribute]) -> bool {
395395
self.print_either_attributes(attrs, ast::AttrStyle::Inner, false, false)
396396
}
397397

398-
fn print_outer_attributes(&mut self, attrs: &[ast::Attribute]) {
398+
fn print_outer_attributes(&mut self, attrs: &[ast::Attribute]) -> bool {
399399
self.print_either_attributes(attrs, ast::AttrStyle::Outer, false, true)
400400
}
401401

402-
fn print_inner_attributes_inline(&mut self, attrs: &[ast::Attribute]) {
402+
fn print_inner_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool {
403403
self.print_either_attributes(attrs, ast::AttrStyle::Inner, true, true)
404404
}
405405

406-
fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) {
406+
fn print_outer_attributes_inline(&mut self, attrs: &[ast::Attribute]) -> bool {
407407
self.print_either_attributes(attrs, ast::AttrStyle::Outer, true, true)
408408
}
409409

@@ -413,20 +413,21 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
413413
kind: ast::AttrStyle,
414414
is_inline: bool,
415415
trailing_hardbreak: bool,
416-
) {
417-
let mut count = 0;
416+
) -> bool {
417+
let mut printed = false;
418418
for attr in attrs {
419419
if attr.style == kind {
420420
self.print_attribute_inline(attr, is_inline);
421421
if is_inline {
422422
self.nbsp();
423423
}
424-
count += 1;
424+
printed = true;
425425
}
426426
}
427-
if count > 0 && trailing_hardbreak && !is_inline {
427+
if printed && trailing_hardbreak && !is_inline {
428428
self.hardbreak_if_not_bol();
429429
}
430+
printed
430431
}
431432

432433
fn print_attribute(&mut self, attr: &ast::Attribute) {
@@ -1646,7 +1647,7 @@ impl<'a> State<'a> {
16461647
self.ann.pre(self, AnnNode::Block(blk));
16471648
self.bopen();
16481649

1649-
self.print_inner_attributes(attrs);
1650+
let has_attrs = self.print_inner_attributes(attrs);
16501651

16511652
for (i, st) in blk.stmts.iter().enumerate() {
16521653
match st.kind {
@@ -1660,7 +1661,7 @@ impl<'a> State<'a> {
16601661
}
16611662
}
16621663

1663-
let empty = attrs.is_empty() && blk.stmts.is_empty();
1664+
let empty = !has_attrs && blk.stmts.is_empty();
16641665
self.bclose_maybe_open(blk.span, empty, close_box);
16651666
self.ann.post(self, AnnNode::Block(blk))
16661667
}

src/test/pretty/attr-fn-inner.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
fn main() {
1010
#![rustc_dummy]
1111
#[rustc_dummy]
12-
fn f() { }
12+
fn f() {}
1313

1414
#[rustc_dummy]
15-
fn g() { }
15+
fn g() {}
1616
}

src/test/pretty/attr-literals.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
fn main() {
88
#![rustc_dummy("hi", 1, 2, 1.012, pi = 3.14, bye, name("John"))]
99
#[rustc_dummy = 8]
10-
fn f() { }
10+
fn f() {}
1111

1212
#[rustc_dummy(1, 2, 3)]
13-
fn g() { }
13+
fn g() {}
1414
}

src/test/pretty/attr-tokens-raw-ident.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
// pp-exact
55

66
#[rustfmt::r#final(final)]
7-
fn main() { }
7+
fn main() {}

src/test/pretty/delimited-token-groups.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ mac! {
4545
}]
4646
#[rustc_dummy =
4747
"aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa aaaaaaaa"]
48-
fn main() { }
48+
fn main() {}

src/test/pretty/doc-comments.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// some single-line non-doc comment
66

77
/// some single line outer-docs
8-
fn a() { }
8+
fn a() {}
99

1010
fn b() {
1111
//! some single line inner-docs
@@ -17,7 +17,7 @@ fn b() {
1717
//////////////////////////////////
1818
/// some single-line outer-docs preceded by a separator
1919
/// (and trailing whitespaces)
20-
fn c() { }
20+
fn c() {}
2121

2222
/*
2323
* some multi-line non-doc comment
@@ -26,7 +26,7 @@ fn c() { }
2626
/**
2727
* some multi-line outer-docs
2828
*/
29-
fn d() { }
29+
fn d() {}
3030

3131
fn e() {
3232
/*!
@@ -43,10 +43,10 @@ fn e() {
4343
/**
4444
* some multi-line outer-docs preceded by a separator
4545
*/
46-
fn f() { }
46+
fn f() {}
4747

4848
#[doc = "unsugared outer doc-comments work also"]
49-
fn g() { }
49+
fn g() {}
5050

5151
fn h() {
5252
#![doc = "as do inner ones"]

src/test/ui/macros/stringify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ fn test_expr() {
235235
#[attr]
236236
{}
237237
),
238-
"#[attr] { }", // FIXME
238+
"#[attr] {}",
239239
);
240240
assert_eq!(
241241
stringify_expr!(

0 commit comments

Comments
 (0)