Skip to content

Commit cf14962

Browse files
committed
Fix a pretty-printing case.
There are a few cases where there should be a space between an identifier and a `(`, when the identifier is a keyword like `let` or `if` or `while`.
1 parent d7a4d14 commit cf14962

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,17 @@ fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
175175
// ANYTHING + `.`: `x.y`, `tup.0`
176176
(_, Tok(Token { kind: Comma | Semi | Dot, .. }, _)) => false,
177177

178-
// IDENT + `(`: `f(3)`
179-
//
180-
// FIXME: Incorrect cases:
181-
// - Let: `let(a, b) = (1, 2)`
182-
(Tok(Token { kind: Ident(..), .. }, _), Del(_, Parenthesis, _)) => false,
178+
// IDENT|`fn`|`Self`|`pub` + `(`: `f(3)`, `fn(x: u8)`, `Self()`, `pub(crate)`,
179+
// but `let (a, b) = (1, 2)` needs a space after the `let`
180+
(Tok(Token { kind: Ident(sym, is_raw), span }, _), Del(_, Parenthesis, _))
181+
if !Ident::new(*sym, *span).is_reserved()
182+
|| *sym == kw::Fn
183+
|| *sym == kw::SelfUpper
184+
|| *sym == kw::Pub
185+
|| *is_raw =>
186+
{
187+
false
188+
}
183189

184190
// `#` + `[`: `#[attr]`
185191
(Tok(Token { kind: Pound, .. }, _), Del(_, Bracket, _)) => false,

tests/ui/macros/stringify.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -655,11 +655,11 @@ fn test_stmt() {
655655
c2!(stmt, [ let _ ], "let _;", "let _");
656656
c2!(stmt, [ let x = true ], "let x = true;", "let x = true");
657657
c2!(stmt, [ let x: bool = true ], "let x: bool = true;", "let x : bool = true");
658-
c2!(stmt, [ let (a, b) = (1, 2) ], "let (a, b) = (1, 2);", "let(a, b) = (1, 2)"); // FIXME
658+
c2!(stmt, [ let (a, b) = (1, 2) ], "let (a, b) = (1, 2);", "let (a, b) = (1, 2)");
659659
c2!(stmt,
660660
[ let (a, b): (u32, u32) = (1, 2) ],
661661
"let (a, b): (u32, u32) = (1, 2);",
662-
"let(a, b) : (u32, u32) = (1, 2)"
662+
"let (a, b) : (u32, u32) = (1, 2)"
663663
);
664664

665665
// StmtKind::Item

tests/ui/proc-macro/issue-76182-leading-vert-pat.stdout

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
PRINT-ATTR INPUT (DISPLAY): fn main() { match() { | () => () } }
1+
PRINT-ATTR INPUT (DISPLAY): fn main() { match () { | () => () } }
22
PRINT-ATTR INPUT (DEBUG): TokenStream [
33
Ident {
44
ident: "fn",

0 commit comments

Comments
 (0)