Skip to content

Commit 23898d0

Browse files
authored
Rollup merge of rust-lang#91568 - dtolnay:breakspace, r=nagisa
Pretty print break and continue without redundant space **Repro:** ```rust macro_rules! m { ($e:expr) => { stringify!($e) }; } fn main() { println!("{:?}", m!(loop { break; })); println!("{:?}", m!(loop { break 'a; })); println!("{:?}", m!(loop { break false; })); } ``` **Before:** - `"loop { break ; }"` - `"loop { break 'a ; }"` - `"loop { break false ; }"` **After:** - `"loop { break; }"` - `"loop { break 'a; }"` - `"loop { break false; }"` <br> Notice that `return` and `yield` already follow the same approach as this PR of printing the space *before* each additional piece following the keyword, rather than *after* each thing. https://github.com/rust-lang/rust/blob/772d51f887fa407216860bf8ecf3f1a32fb795b4/compiler/rustc_ast_pretty/src/pprust/state.rs#L2148-L2154 https://github.com/rust-lang/rust/blob/772d51f887fa407216860bf8ecf3f1a32fb795b4/compiler/rustc_ast_pretty/src/pprust/state.rs#L2221-L2228
2 parents 1b4d7b7 + abb8893 commit 23898d0

File tree

5 files changed

+11
-16
lines changed

5 files changed

+11
-16
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -2135,22 +2135,20 @@ impl<'a> State<'a> {
21352135
ast::ExprKind::Path(Some(ref qself), ref path) => self.print_qpath(path, qself, true),
21362136
ast::ExprKind::Break(opt_label, ref opt_expr) => {
21372137
self.s.word("break");
2138-
self.s.space();
21392138
if let Some(label) = opt_label {
2140-
self.print_ident(label.ident);
21412139
self.s.space();
2140+
self.print_ident(label.ident);
21422141
}
21432142
if let Some(ref expr) = *opt_expr {
2144-
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
21452143
self.s.space();
2144+
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
21462145
}
21472146
}
21482147
ast::ExprKind::Continue(opt_label) => {
21492148
self.s.word("continue");
2150-
self.s.space();
21512149
if let Some(label) = opt_label {
2150+
self.s.space();
21522151
self.print_ident(label.ident);
2153-
self.s.space()
21542152
}
21552153
}
21562154
ast::ExprKind::Ret(ref result) => {

compiler/rustc_hir_pretty/src/lib.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -1543,22 +1543,20 @@ impl<'a> State<'a> {
15431543
hir::ExprKind::Path(ref qpath) => self.print_qpath(qpath, true),
15441544
hir::ExprKind::Break(destination, ref opt_expr) => {
15451545
self.s.word("break");
1546-
self.s.space();
15471546
if let Some(label) = destination.label {
1548-
self.print_ident(label.ident);
15491547
self.s.space();
1548+
self.print_ident(label.ident);
15501549
}
15511550
if let Some(ref expr) = *opt_expr {
1552-
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
15531551
self.s.space();
1552+
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
15541553
}
15551554
}
15561555
hir::ExprKind::Continue(destination) => {
15571556
self.s.word("continue");
1558-
self.s.space();
15591557
if let Some(label) = destination.label {
1558+
self.s.space();
15601559
self.print_ident(label.ident);
1561-
self.s.space()
15621560
}
15631561
}
15641562
hir::ExprKind::Ret(ref result) => {

src/test/pretty/ast-stmt-expr-attr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ fn syntax() {
110110
let _ = #[attr] &mut 0;
111111
let _ = #[attr] &#[attr] 0;
112112
let _ = #[attr] &mut #[attr] 0;
113-
let _ = #[attr] break ;
114-
let _ = #[attr] continue ;
113+
let _ = #[attr] break;
114+
let _ = #[attr] continue;
115115
let _ = #[attr] return;
116116
let _ = #[attr] foo!();
117117
let _ = #[attr] foo!(#! [attr]);

src/test/pretty/hir-pretty-loop.pp

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
// pretty-mode:hir
77
// pp-exact:hir-pretty-loop.pp
88

9-
pub fn foo() { loop { break ; } }
9+
pub fn foo() { loop { break; } }

src/test/pretty/stmt_expr_attributes.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,8 @@ fn _11() {
229229
let _ = #[rustc_dummy] &mut 0;
230230
let _ = #[rustc_dummy] &#[rustc_dummy] 0;
231231
let _ = #[rustc_dummy] &mut #[rustc_dummy] 0;
232-
// FIXME: pp bug, extra space after keyword?
233-
while false { let _ = #[rustc_dummy] continue ; }
234-
while true { let _ = #[rustc_dummy] break ; }
232+
while false { let _ = #[rustc_dummy] continue; }
233+
while true { let _ = #[rustc_dummy] break; }
235234
|| #[rustc_dummy] return;
236235
let _ = #[rustc_dummy] expr_mac!();
237236
let _ = #[rustc_dummy] expr_mac![];

0 commit comments

Comments
 (0)