about summary refs log tree commit diff
path: root/src/tools
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2025-04-16 16:13:50 +1000
committerNicholas Nethercote <n.nethercote@gmail.com>2025-04-21 07:35:56 +1000
commitbf8ce32558a4657d077a6761eaa293d0645c2e16 (patch)
treec9ceaabace92d090d8613bcda12ad4fc4fd33a24 /src/tools
parenta15cce2690e8fab72422515c9dc02c6fbc506733 (diff)
downloadrust-bf8ce32558a4657d077a6761eaa293d0645c2e16.tar.gz
rust-bf8ce32558a4657d077a6761eaa293d0645c2e16.zip
Remove `token::{Open,Close}Delim`.
By replacing them with `{Open,Close}{Param,Brace,Bracket,Invisible}`.

PR #137902 made `ast::TokenKind` more like `lexer::TokenKind` by
replacing the compound `BinOp{,Eq}(BinOpToken)` variants with fieldless
variants `Plus`, `Minus`, `Star`, etc. This commit does a similar thing
with delimiters. It also makes `ast::TokenKind` more similar to
`parser::TokenType`.

This requires a few new methods:
- `TokenKind::is_{,open_,close_}delim()` replace various kinds of
  pattern matches.
- `Delimiter::as_{open,close}_token_kind` are used to convert
  `Delimiter` values to `TokenKind`.

Despite these additions, it's a net reduction in lines of code. This is
because e.g. `token::OpenParen` is so much shorter than
`token::OpenDelim(Delimiter::Parenthesis)` that many multi-line forms
reduce to single line forms. And many places where the number of lines
doesn't change are still easier to read, just because the names are
shorter, e.g.:
```
-   } else if self.token != token::CloseDelim(Delimiter::Brace) {
+   } else if self.token != token::CloseBrace {
```
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/rustfmt/src/macros.rs12
-rw-r--r--src/tools/rustfmt/src/parse/macros/cfg_if.rs6
2 files changed, 11 insertions, 7 deletions
diff --git a/src/tools/rustfmt/src/macros.rs b/src/tools/rustfmt/src/macros.rs
index 1e16aace304..0ff0aad7a2d 100644
--- a/src/tools/rustfmt/src/macros.rs
+++ b/src/tools/rustfmt/src/macros.rs
@@ -722,7 +722,7 @@ fn last_tok(tt: &TokenTree) -> Token {
     match *tt {
         TokenTree::Token(ref t, _) => t.clone(),
         TokenTree::Delimited(delim_span, _, delim, _) => Token {
-            kind: TokenKind::CloseDelim(delim),
+            kind: delim.as_open_token_kind(),
             span: delim_span.close,
         },
     }
@@ -1124,8 +1124,14 @@ fn next_space(tok: &TokenKind) -> SpaceState {
         TokenKind::PathSep
         | TokenKind::Pound
         | TokenKind::Dollar
-        | TokenKind::OpenDelim(_)
-        | TokenKind::CloseDelim(_) => SpaceState::Never,
+        | TokenKind::OpenParen
+        | TokenKind::CloseParen
+        | TokenKind::OpenBrace
+        | TokenKind::CloseBrace
+        | TokenKind::OpenBracket
+        | TokenKind::CloseBracket
+        | TokenKind::OpenInvisible(_)
+        | TokenKind::CloseInvisible(_) => SpaceState::Never,
 
         TokenKind::Literal(..) | TokenKind::Ident(..) | TokenKind::Lifetime(..) => {
             SpaceState::Ident
diff --git a/src/tools/rustfmt/src/parse/macros/cfg_if.rs b/src/tools/rustfmt/src/parse/macros/cfg_if.rs
index 0b7b6c4d361..30b83373c17 100644
--- a/src/tools/rustfmt/src/parse/macros/cfg_if.rs
+++ b/src/tools/rustfmt/src/parse/macros/cfg_if.rs
@@ -1,7 +1,7 @@
 use std::panic::{AssertUnwindSafe, catch_unwind};
 
 use rustc_ast::ast;
-use rustc_ast::token::{Delimiter, TokenKind};
+use rustc_ast::token::TokenKind;
 use rustc_parse::exp;
 use rustc_parse::parser::ForceCollect;
 use rustc_span::symbol::kw;
@@ -60,9 +60,7 @@ fn parse_cfg_if_inner<'a>(
             return Err("Expected an opening brace");
         }
 
-        while parser.token != TokenKind::CloseDelim(Delimiter::Brace)
-            && parser.token.kind != TokenKind::Eof
-        {
+        while parser.token != TokenKind::CloseBrace && parser.token.kind != TokenKind::Eof {
             let item = match parser.parse_item(ForceCollect::No) {
                 Ok(Some(item_ptr)) => item_ptr.into_inner(),
                 Ok(None) => continue,