Skip to content

Commit 3c4b961

Browse files
committed
Add helpful comments to tt_prepend_space.
In particular, I found some places where it treats `!` inappropriately, which are worth documenting.
1 parent 8e7fd55 commit 3c4b961

File tree

1 file changed

+11
-0
lines changed
  • compiler/rustc_ast_pretty/src/pprust

1 file changed

+11
-0
lines changed

compiler/rustc_ast_pretty/src/pprust/state.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,8 @@ pub fn print_crate<'a>(
150150
/// and also addresses some specific regressions described in #63896 and #73345.
151151
fn tt_prepend_space(tt: &TokenTree, prev: &TokenTree) -> bool {
152152
if let TokenTree::Token(token, _) = prev {
153+
// No space after these tokens, e.g. `x.y`, `$e`
154+
// (The carets point to `prev`.) ^ ^
153155
if matches!(token.kind, token::Dot | token::Dollar) {
154156
return false;
155157
}
@@ -158,10 +160,19 @@ fn tt_prepend_space(tt: &TokenTree, prev: &TokenTree) -> bool {
158160
}
159161
}
160162
match tt {
163+
// No space before these tokens, e.g. `foo,`, `println!`, `x.y`
164+
// (The carets point to `token`.) ^ ^ ^
165+
//
166+
// FIXME: having `Not` here works well for macro invocations like
167+
// `println!`, but is bad when `!` means "logical not" or "the never
168+
// type", where the lack of space causes ugliness like this:
169+
// `Fn() ->!`, `x =! y`, `if! x { f(); }`.
161170
TokenTree::Token(token, _) => !matches!(token.kind, token::Comma | token::Not | token::Dot),
171+
// No space before parentheses if preceded by these tokens, e.g. `foo(...)`
162172
TokenTree::Delimited(_, Delimiter::Parenthesis, _) => {
163173
!matches!(prev, TokenTree::Token(Token { kind: token::Ident(..), .. }, _))
164174
}
175+
// No space before brackets if preceded by these tokens, e.g. `#[...]`
165176
TokenTree::Delimited(_, Delimiter::Bracket, _) => {
166177
!matches!(prev, TokenTree::Token(Token { kind: token::Pound, .. }, _))
167178
}

0 commit comments

Comments
 (0)