Skip to content

Commit 1fbabee

Browse files
committed
Fix some cases in space_between.
There are a number of cases where we erroneously omit the space between two tokens, all involving an exception to a more general case. The affected tokens are `$`, `!`, `.`, `,`, and `let` followed by a parenthesis. This fixes a lot of FIXME comments.
1 parent 41e4a3e commit 1fbabee

File tree

3 files changed

+44
-37
lines changed

3 files changed

+44
-37
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

+27-19
Original file line numberDiff line numberDiff line change
@@ -171,27 +171,35 @@ fn space_between(tt1: &TokenTree, tt2: &TokenTree) -> bool {
171171
// No space after line doc comments.
172172
(Tok(Token { kind: DocComment(CommentKind::Line, ..), .. }, _), _) => false,
173173

174-
// `.` + ANYTHING: `x.y`, `tup.0`
175-
// `$` + ANYTHING: `$e`
176-
(Tok(Token { kind: Dot | Dollar, .. }, _), _) => false,
177-
178-
// ANYTHING + `,`: `foo,`
179-
// ANYTHING + `.`: `x.y`, `tup.0`
180-
// ANYTHING + `!`: `foo! { ... }`
181-
//
182-
// FIXME: Incorrect cases:
183-
// - Logical not: `x =! y`, `if! x { f(); }`
184-
// - Never type: `Fn() ->!`
185-
(_, Tok(Token { kind: Comma | Dot | Not, .. }, _)) => false,
174+
// `.` + NON-PUNCT: `x.y`, `tup.0`
175+
(Tok(Token { kind: Dot, .. }, _), tt2) if !is_punct(tt2) => false,
186176

187-
// NON-PUNCT + `;`: `x = 3;`, `[T; 3]`
188-
(tt1, Tok(Token { kind: Semi, .. }, _)) if !is_punct(tt1) => false,
177+
// `$` + IDENT: `$e`
178+
(Tok(Token { kind: Dollar, .. }, _), Tok(Token { kind: Ident(..), .. }, _)) => false,
189179

190-
// IDENT + `(`: `f(3)`
191-
//
192-
// FIXME: Incorrect cases:
193-
// - Let: `let(a, b) = (1, 2)`
194-
(Tok(Token { kind: Ident(..), .. }, _), Del(_, _, Parenthesis, _)) => false,
180+
// NON-PUNCT + `,`: `foo,`
181+
// NON-PUNCT + `;`: `x = 3;`, `[T; 3]`
182+
// NON-PUNCT + `.`: `x.y`, `tup.0`
183+
(tt1, Tok(Token { kind: Comma | Semi | Dot, .. }, _)) if !is_punct(tt1) => false,
184+
185+
// IDENT + `!`: `println!()`, but `if !x { ... }` needs a space after the `if`
186+
(Tok(Token { kind: Ident(sym, is_raw), span }, _), Tok(Token { kind: Not, .. }, _))
187+
if !Ident::new(*sym, *span).is_reserved() || *is_raw =>
188+
{
189+
false
190+
}
191+
192+
// IDENT|`fn`|`Self`|`pub` + `(`: `f(3)`, `fn(x: u8)`, `Self()`, `pub(crate)`,
193+
// but `let (a, b) = (1, 2)` needs a space after the `let`
194+
(Tok(Token { kind: Ident(sym, is_raw), span }, _), Del(_, _, Parenthesis, _))
195+
if !Ident::new(*sym, *span).is_reserved()
196+
|| *sym == kw::Fn
197+
|| *sym == kw::SelfUpper
198+
|| *sym == kw::Pub
199+
|| *is_raw =>
200+
{
201+
false
202+
}
195203

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

tests/ui/macros/stringify.rs

+16-17
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,9 @@ fn test_expr() {
107107
c1!(expr, [ true || false ], "true || false");
108108
c1!(expr, [ true || false && false ], "true || false && false");
109109
c1!(expr, [ a < 1 && 2 < b && c > 3 && 4 > d ], "a < 1 && 2 < b && c > 3 && 4 > d");
110-
c2!(expr, [ a & b & !c ], "a & b & !c", "a & b &!c"); // FIXME
110+
c1!(expr, [ a & b & !c ], "a & b & !c");
111111
c1!(expr, [ a + b * c - d + -1 * -2 - -3], "a + b * c - d + -1 * -2 - -3");
112-
c2!(expr, [ x = !y ], "x = !y", "x =!y"); // FIXME
112+
c1!(expr, [ x = !y ], "x = !y");
113113

114114
// ExprKind::Unary
115115
c1!(expr, [ *expr ], "*expr");
@@ -141,15 +141,14 @@ fn test_expr() {
141141
"if let _ = (true && false) {}",
142142
"if let _ = true && false {}",
143143
);
144-
c2!(expr,
144+
c1!(expr,
145145
[ match () { _ if let _ = Struct {} => {} } ],
146-
"match () { _ if let _ = Struct {} => {} }",
147-
"match() { _ if let _ = Struct {} => {} }",
146+
"match () { _ if let _ = Struct {} => {} }"
148147
);
149148

150149
// ExprKind::If
151150
c1!(expr, [ if true {} ], "if true {}");
152-
c2!(expr, [ if !true {} ], "if !true {}", "if!true {}"); // FIXME
151+
c1!(expr, [ if !true {} ], "if !true {}");
153152
c1!(expr, [ if ::std::blah() { } else { } ], "if ::std::blah() {} else {}");
154153
c1!(expr, [ if let true = true {} else {} ], "if let true = true {} else {}");
155154
c1!(expr,
@@ -212,7 +211,7 @@ fn test_expr() {
212211
c2_match_arm!(
213212
[ { 1 } - 1 ],
214213
"match () { _ => ({ 1 }) - 1, }",
215-
"match() { _ => { 1 } - 1 }",
214+
"match () { _ => { 1 } - 1 }",
216215
);
217216

218217
// ExprKind::Closure
@@ -655,11 +654,11 @@ fn test_stmt() {
655654
c2!(stmt, [ let _ ], "let _;", "let _");
656655
c2!(stmt, [ let x = true ], "let x = true;", "let x = true");
657656
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
657+
c2!(stmt, [ let (a, b) = (1, 2) ], "let (a, b) = (1, 2);", "let (a, b) = (1, 2)");
659658
c2!(stmt,
660659
[ let (a, b): (u32, u32) = (1, 2) ],
661660
"let (a, b): (u32, u32) = (1, 2);",
662-
"let(a, b): (u32, u32) = (1, 2)" // FIXME
661+
"let (a, b): (u32, u32) = (1, 2)"
663662
);
664663
macro_rules! c2_let_expr_minus_one {
665664
([ $expr:expr ], $stmt_expected:expr, $tokens_expected:expr $(,)?) => {
@@ -776,8 +775,8 @@ fn test_ty() {
776775
c1!(ty, [ Ref<'a> ], "Ref<'a>");
777776
c1!(ty, [ PhantomData<T> ], "PhantomData<T>");
778777
c2!(ty, [ PhantomData::<T> ], "PhantomData<T>", "PhantomData::<T>");
779-
c2!(ty, [ Fn() -> ! ], "Fn() -> !", "Fn() ->!");
780-
c2!(ty, [ Fn(u8) -> ! ], "Fn(u8) -> !", "Fn(u8) ->!"); // FIXME
778+
c1!(ty, [ Fn() -> ! ], "Fn() -> !");
779+
c1!(ty, [ Fn(u8) -> ! ], "Fn(u8) -> !");
781780
c1!(ty, [ <Struct as Trait>::Type ], "<Struct as Trait>::Type");
782781

783782
// TyKind::TraitObject
@@ -857,16 +856,16 @@ fn test_punct() {
857856
// Otherwise, any old proc macro that parses pretty-printed code might glue
858857
// together tokens that shouldn't be glued.
859858
p!([ = = < < <= <= == == != != >= >= > > ], "= = < < <= <= == == != != >= >= > >");
860-
p!([ && && & & || || | | ! ! ], "&& && & & || || | |!!"); // FIXME
859+
p!([ && && & & || || | | ! ! ], "&& && & & || || | | ! !");
861860
p!([ ~ ~ @ @ # # ], "~ ~ @ @ # #");
862-
p!([ . . .. .. ... ... ..= ..=], ".... .. ... ... ..= ..="); // FIXME
863-
p!([ , , ; ; : : :: :: ], ",, ; ; : : :: ::"); // FIXME
861+
p!([ . . .. .. ... ... ..= ..=], ". . .. .. ... ... ..= ..=");
862+
p!([ , , ; ; : : :: :: ], ", , ; ; : : :: ::");
864863
p!([ -> -> <- <- => =>], "-> -> <- <- => =>");
865-
p!([ $ $ ? ? ' ' ], "$$? ? ' '"); // FIXME
864+
p!([ $ $ ? ? ' ' ], "$ $ ? ? ' '");
866865
p!([ + + += += - - -= -= * * *= *= / / /= /= ], "+ + += += - - -= -= * * *= *= / / /= /=");
867866
p!([ % % %= %= ^ ^ ^= ^= << << <<= <<= >> >> >>= >>= ],
868867
"% % %= %= ^ ^ ^= ^= << << <<= <<= >> >> >>= >>=");
869-
p!([ +! ?= |> >>@ --> <-- $$ =====> ], "+! ?= |> >>@ --> <-- $$=====>");
870-
p!([ ,; ;, ** @@ $+$ >< <> ?? +== ], ",; ;, ** @@ $+$>< <> ?? +=="); // FIXME: `$ >` -> `$>`
868+
p!([ +! ?= |> >>@ --> <-- $$ =====> ], "+! ?= |> >>@ --> <-- $$ =====>");
869+
p!([ ,; ;, ** @@ $+$ >< <> ?? +== ], ",; ;, ** @@ $+$ >< <> ?? +==");
871870
p!([ :#!@|$=&*,+;*~? ], ":#!@|$=&*,+;*~?");
872871
}

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

+1-1
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)