Skip to content

Commit fae7785

Browse files
committed
Auto merge of rust-lang#139897 - nnethercote:rm-OpenDelim-CloseDelim, r=petrochenkov
Remove `token::{Open,Close}Delim` By replacing them with `{Open,Close}{Param,Brace,Bracket,Invisible}`. PR rust-lang#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 { ``` r? `@petrochenkov`
2 parents d6c1e45 + bf8ce32 commit fae7785

File tree

30 files changed

+457
-499
lines changed

30 files changed

+457
-499
lines changed

compiler/rustc_ast/src/attr/mod.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -416,10 +416,7 @@ impl MetaItem {
416416
// This path is currently unreachable in the test suite.
417417
unreachable!()
418418
}
419-
Some(TokenTree::Token(
420-
Token { kind: token::OpenDelim(_) | token::CloseDelim(_), .. },
421-
_,
422-
)) => {
419+
Some(TokenTree::Token(Token { kind, .. }, _)) if kind.is_delim() => {
423420
panic!("Should be `AttrTokenTree::Delimited`, not delim tokens: {:?}", tt);
424421
}
425422
_ => return None,

compiler/rustc_ast/src/token.rs

+103-28
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ pub enum InvisibleOrigin {
3232
ProcMacro,
3333
}
3434

35+
impl InvisibleOrigin {
36+
// Should the parser skip these invisible delimiters? Ideally this function
37+
// will eventually disappear and no invisible delimiters will be skipped.
38+
#[inline]
39+
pub fn skip(&self) -> bool {
40+
match self {
41+
InvisibleOrigin::MetaVar(_) => false,
42+
InvisibleOrigin::ProcMacro => true,
43+
}
44+
}
45+
}
46+
3547
impl PartialEq for InvisibleOrigin {
3648
#[inline]
3749
fn eq(&self, _other: &InvisibleOrigin) -> bool {
@@ -125,8 +137,7 @@ impl Delimiter {
125137
pub fn skip(&self) -> bool {
126138
match self {
127139
Delimiter::Parenthesis | Delimiter::Bracket | Delimiter::Brace => false,
128-
Delimiter::Invisible(InvisibleOrigin::MetaVar(_)) => false,
129-
Delimiter::Invisible(InvisibleOrigin::ProcMacro) => true,
140+
Delimiter::Invisible(origin) => origin.skip(),
130141
}
131142
}
132143

@@ -140,6 +151,24 @@ impl Delimiter {
140151
_ => false,
141152
}
142153
}
154+
155+
pub fn as_open_token_kind(&self) -> TokenKind {
156+
match *self {
157+
Delimiter::Parenthesis => OpenParen,
158+
Delimiter::Brace => OpenBrace,
159+
Delimiter::Bracket => OpenBracket,
160+
Delimiter::Invisible(origin) => OpenInvisible(origin),
161+
}
162+
}
163+
164+
pub fn as_close_token_kind(&self) -> TokenKind {
165+
match *self {
166+
Delimiter::Parenthesis => CloseParen,
167+
Delimiter::Brace => CloseBrace,
168+
Delimiter::Bracket => CloseBracket,
169+
Delimiter::Invisible(origin) => CloseInvisible(origin),
170+
}
171+
}
143172
}
144173

145174
// Note that the suffix is *not* considered when deciding the `LitKind` in this
@@ -194,9 +223,9 @@ impl Lit {
194223
match token.uninterpolate().kind {
195224
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => Some(Lit::new(Bool, name, None)),
196225
Literal(token_lit) => Some(token_lit),
197-
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
226+
OpenInvisible(InvisibleOrigin::MetaVar(
198227
MetaVarKind::Literal | MetaVarKind::Expr { .. },
199-
))) => {
228+
)) => {
200229
// Unreachable with the current test suite.
201230
panic!("from_token metavar");
202231
}
@@ -426,10 +455,22 @@ pub enum TokenKind {
426455
Question,
427456
/// Used by proc macros for representing lifetimes, not generated by lexer right now.
428457
SingleQuote,
429-
/// An opening delimiter (e.g., `{`).
430-
OpenDelim(Delimiter),
431-
/// A closing delimiter (e.g., `}`).
432-
CloseDelim(Delimiter),
458+
/// `(`
459+
OpenParen,
460+
/// `)`
461+
CloseParen,
462+
/// `{`
463+
OpenBrace,
464+
/// `}`
465+
CloseBrace,
466+
/// `[`
467+
OpenBracket,
468+
/// `]`
469+
CloseBracket,
470+
/// Invisible opening delimiter, produced by a macro.
471+
OpenInvisible(InvisibleOrigin),
472+
/// Invisible closing delimiter, produced by a macro.
473+
CloseInvisible(InvisibleOrigin),
433474

434475
/* Literals */
435476
Literal(Lit),
@@ -530,6 +571,37 @@ impl TokenKind {
530571
pub fn should_end_const_arg(&self) -> bool {
531572
matches!(self, Gt | Ge | Shr | ShrEq)
532573
}
574+
575+
pub fn is_delim(&self) -> bool {
576+
self.open_delim().is_some() || self.close_delim().is_some()
577+
}
578+
579+
pub fn open_delim(&self) -> Option<Delimiter> {
580+
match *self {
581+
OpenParen => Some(Delimiter::Parenthesis),
582+
OpenBrace => Some(Delimiter::Brace),
583+
OpenBracket => Some(Delimiter::Bracket),
584+
OpenInvisible(origin) => Some(Delimiter::Invisible(origin)),
585+
_ => None,
586+
}
587+
}
588+
589+
pub fn close_delim(&self) -> Option<Delimiter> {
590+
match *self {
591+
CloseParen => Some(Delimiter::Parenthesis),
592+
CloseBrace => Some(Delimiter::Brace),
593+
CloseBracket => Some(Delimiter::Bracket),
594+
CloseInvisible(origin) => Some(Delimiter::Invisible(origin)),
595+
_ => None,
596+
}
597+
}
598+
599+
pub fn is_close_delim_or_eof(&self) -> bool {
600+
match self {
601+
CloseParen | CloseBrace | CloseBracket | CloseInvisible(_) | Eof => true,
602+
_ => false,
603+
}
604+
}
533605
}
534606

535607
impl Token {
@@ -559,7 +631,8 @@ impl Token {
559631
| DotDotDot | DotDotEq | Comma | Semi | Colon | PathSep | RArrow | LArrow
560632
| FatArrow | Pound | Dollar | Question | SingleQuote => true,
561633

562-
OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) | Ident(..)
634+
OpenParen | CloseParen | OpenBrace | CloseBrace | OpenBracket | CloseBracket
635+
| OpenInvisible(_) | CloseInvisible(_) | Literal(..) | DocComment(..) | Ident(..)
563636
| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Eof => false,
564637
}
565638
}
@@ -573,11 +646,12 @@ impl Token {
573646
/// **NB**: Take care when modifying this function, since it will change
574647
/// the stable set of tokens that are allowed to match an expr nonterminal.
575648
pub fn can_begin_expr(&self) -> bool {
576-
use Delimiter::*;
577649
match self.uninterpolate().kind {
578650
Ident(name, is_raw) =>
579651
ident_can_begin_expr(name, self.span, is_raw), // value name or keyword
580-
OpenDelim(Parenthesis | Brace | Bracket) | // tuple, array or block
652+
OpenParen | // tuple
653+
OpenBrace | // block
654+
OpenBracket | // array
581655
Literal(..) | // literal
582656
Bang | // operator not
583657
Minus | // unary minus
@@ -591,12 +665,12 @@ impl Token {
591665
PathSep | // global path
592666
Lifetime(..) | // labeled loop
593667
Pound => true, // expression attributes
594-
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
668+
OpenInvisible(InvisibleOrigin::MetaVar(
595669
MetaVarKind::Block |
596670
MetaVarKind::Expr { .. } |
597671
MetaVarKind::Literal |
598672
MetaVarKind::Path
599-
))) => true,
673+
)) => true,
600674
_ => false,
601675
}
602676
}
@@ -608,8 +682,8 @@ impl Token {
608682
match &self.uninterpolate().kind {
609683
// box, ref, mut, and other identifiers (can stricten)
610684
Ident(..) | NtIdent(..) |
611-
OpenDelim(Delimiter::Parenthesis) | // tuple pattern
612-
OpenDelim(Delimiter::Bracket) | // slice pattern
685+
OpenParen | // tuple pattern
686+
OpenBracket | // slice pattern
613687
And | // reference
614688
Minus | // negative literal
615689
AndAnd | // double reference
@@ -620,14 +694,14 @@ impl Token {
620694
Lt | // path (UFCS constant)
621695
Shl => true, // path (double UFCS)
622696
Or => matches!(pat_kind, PatWithOr), // leading vert `|` or-pattern
623-
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
697+
OpenInvisible(InvisibleOrigin::MetaVar(
624698
MetaVarKind::Expr { .. } |
625699
MetaVarKind::Literal |
626700
MetaVarKind::Meta { .. } |
627701
MetaVarKind::Pat(_) |
628702
MetaVarKind::Path |
629703
MetaVarKind::Ty { .. }
630-
))) => true,
704+
)) => true,
631705
_ => false,
632706
}
633707
}
@@ -637,8 +711,8 @@ impl Token {
637711
match self.uninterpolate().kind {
638712
Ident(name, is_raw) =>
639713
ident_can_begin_type(name, self.span, is_raw), // type name or keyword
640-
OpenDelim(Delimiter::Parenthesis) | // tuple
641-
OpenDelim(Delimiter::Bracket) | // array
714+
OpenParen | // tuple
715+
OpenBracket | // array
642716
Bang | // never
643717
Star | // raw pointer
644718
And | // reference
@@ -647,10 +721,10 @@ impl Token {
647721
Lifetime(..) | // lifetime bound in trait object
648722
Lt | Shl | // associated path
649723
PathSep => true, // global path
650-
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
724+
OpenInvisible(InvisibleOrigin::MetaVar(
651725
MetaVarKind::Ty { .. } |
652726
MetaVarKind::Path
653-
))) => true,
727+
)) => true,
654728
// For anonymous structs or unions, which only appear in specific positions
655729
// (type of struct fields or union fields), we don't consider them as regular types
656730
_ => false,
@@ -660,11 +734,11 @@ impl Token {
660734
/// Returns `true` if the token can appear at the start of a const param.
661735
pub fn can_begin_const_arg(&self) -> bool {
662736
match self.kind {
663-
OpenDelim(Delimiter::Brace) | Literal(..) | Minus => true,
737+
OpenBrace | Literal(..) | Minus => true,
664738
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
665-
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
739+
OpenInvisible(InvisibleOrigin::MetaVar(
666740
MetaVarKind::Expr { .. } | MetaVarKind::Block | MetaVarKind::Literal,
667-
))) => true,
741+
)) => true,
668742
_ => false,
669743
}
670744
}
@@ -711,7 +785,7 @@ impl Token {
711785
match self.uninterpolate().kind {
712786
Literal(..) | Minus => true,
713787
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
714-
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
788+
OpenInvisible(InvisibleOrigin::MetaVar(mv_kind)) => match mv_kind {
715789
MetaVarKind::Literal => true,
716790
MetaVarKind::Expr { can_begin_literal_maybe_minus, .. } => {
717791
can_begin_literal_maybe_minus
@@ -725,7 +799,7 @@ impl Token {
725799
pub fn can_begin_string_literal(&self) -> bool {
726800
match self.uninterpolate().kind {
727801
Literal(..) => true,
728-
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(mv_kind))) => match mv_kind {
802+
OpenInvisible(InvisibleOrigin::MetaVar(mv_kind)) => match mv_kind {
729803
MetaVarKind::Literal => true,
730804
MetaVarKind::Expr { can_begin_string_literal, .. } => can_begin_string_literal,
731805
_ => false,
@@ -892,7 +966,7 @@ impl Token {
892966
/// from an expanded metavar?
893967
pub fn is_metavar_seq(&self) -> Option<MetaVarKind> {
894968
match self.kind {
895-
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(kind))) => Some(kind),
969+
OpenInvisible(InvisibleOrigin::MetaVar(kind)) => Some(kind),
896970
_ => None,
897971
}
898972
}
@@ -970,7 +1044,8 @@ impl Token {
9701044
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | PlusEq | MinusEq | StarEq | SlashEq
9711045
| PercentEq | CaretEq | AndEq | OrEq | ShlEq | ShrEq | At | DotDotDot | DotDotEq
9721046
| Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar | Question
973-
| OpenDelim(..) | CloseDelim(..) | Literal(..) | Ident(..) | NtIdent(..)
1047+
| OpenParen | CloseParen | OpenBrace | CloseBrace | OpenBracket | CloseBracket
1048+
| OpenInvisible(_) | CloseInvisible(_) | Literal(..) | Ident(..) | NtIdent(..)
9741049
| Lifetime(..) | NtLifetime(..) | DocComment(..) | Eof,
9751050
_,
9761051
) => {

compiler/rustc_ast_pretty/src/pprust/state.rs

+9-10
Original file line numberDiff line numberDiff line change
@@ -770,12 +770,12 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
770770
self.bclose(span, empty);
771771
}
772772
delim => {
773-
let token_str = self.token_kind_to_string(&token::OpenDelim(delim));
773+
let token_str = self.token_kind_to_string(&delim.as_open_token_kind());
774774
self.word(token_str);
775775
self.ibox(0);
776776
self.print_tts(tts, convert_dollar_crate);
777777
self.end();
778-
let token_str = self.token_kind_to_string(&token::CloseDelim(delim));
778+
let token_str = self.token_kind_to_string(&delim.as_close_token_kind());
779779
self.word(token_str);
780780
}
781781
}
@@ -932,14 +932,13 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
932932
token::RArrow => "->".into(),
933933
token::LArrow => "<-".into(),
934934
token::FatArrow => "=>".into(),
935-
token::OpenDelim(Delimiter::Parenthesis) => "(".into(),
936-
token::CloseDelim(Delimiter::Parenthesis) => ")".into(),
937-
token::OpenDelim(Delimiter::Bracket) => "[".into(),
938-
token::CloseDelim(Delimiter::Bracket) => "]".into(),
939-
token::OpenDelim(Delimiter::Brace) => "{".into(),
940-
token::CloseDelim(Delimiter::Brace) => "}".into(),
941-
token::OpenDelim(Delimiter::Invisible(_))
942-
| token::CloseDelim(Delimiter::Invisible(_)) => "".into(),
935+
token::OpenParen => "(".into(),
936+
token::CloseParen => ")".into(),
937+
token::OpenBracket => "[".into(),
938+
token::CloseBracket => "]".into(),
939+
token::OpenBrace => "{".into(),
940+
token::CloseBrace => "}".into(),
941+
token::OpenInvisible(_) | token::CloseInvisible(_) => "".into(),
943942
token::Pound => "#".into(),
944943
token::Dollar => "$".into(),
945944
token::Question => "?".into(),

compiler/rustc_attr_parsing/src/parser.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -430,9 +430,7 @@ impl<'a> MetaItemListParserContext<'a> {
430430
let span = span.with_hi(segments.last().unwrap().span.hi());
431431
Some(AttrPath { segments: segments.into_boxed_slice(), span })
432432
}
433-
TokenTree::Token(Token { kind: token::OpenDelim(_) | token::CloseDelim(_), .. }, _) => {
434-
None
435-
}
433+
TokenTree::Token(Token { kind, .. }, _) if kind.is_delim() => None,
436434
_ => {
437435
// malformed attributes can get here. We can't crash, but somewhere else should've
438436
// already warned for this.

compiler/rustc_expand/src/config.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,7 @@ impl<'a> StripUnconfigured<'a> {
237237
inner = self.configure_tokens(&inner);
238238
Some(AttrTokenTree::Delimited(sp, spacing, delim, inner))
239239
}
240-
AttrTokenTree::Token(
241-
Token { kind: TokenKind::OpenDelim(_) | TokenKind::CloseDelim(_), .. },
242-
_,
243-
) => {
240+
AttrTokenTree::Token(Token { kind, .. }, _) if kind.is_delim() => {
244241
panic!("Should be `AttrTokenTree::Delimited`, not delim tokens: {:?}", tree);
245242
}
246243
AttrTokenTree::Token(token, spacing) => Some(AttrTokenTree::Token(token, spacing)),

compiler/rustc_expand/src/expand.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ use std::{iter, mem};
77
use rustc_ast as ast;
88
use rustc_ast::mut_visit::*;
99
use rustc_ast::ptr::P;
10-
use rustc_ast::token::{self, Delimiter};
1110
use rustc_ast::tokenstream::TokenStream;
1211
use rustc_ast::visit::{self, AssocCtxt, Visitor, VisitorResult, try_visit, walk_list};
1312
use rustc_ast::{
1413
AssocItemKind, AstNodeWrapper, AttrArgs, AttrStyle, AttrVec, ExprKind, ForeignItemKind,
1514
HasAttrs, HasNodeId, Inline, ItemKind, MacStmtStyle, MetaItemInner, MetaItemKind, ModKind,
16-
NodeId, PatKind, StmtKind, TyKind,
15+
NodeId, PatKind, StmtKind, TyKind, token,
1716
};
1817
use rustc_ast_pretty::pprust;
1918
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
@@ -1004,7 +1003,7 @@ pub fn parse_ast_fragment<'a>(
10041003
AstFragmentKind::Stmts => {
10051004
let mut stmts = SmallVec::new();
10061005
// Won't make progress on a `}`.
1007-
while this.token != token::Eof && this.token != token::CloseDelim(Delimiter::Brace) {
1006+
while this.token != token::Eof && this.token != token::CloseBrace {
10081007
if let Some(stmt) = this.parse_full_stmt(AttemptLocalParseRecovery::Yes)? {
10091008
stmts.push(stmt);
10101009
}

compiler/rustc_expand/src/mbe/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Cow;
22

3-
use rustc_ast::token::{self, Delimiter, Token, TokenKind};
3+
use rustc_ast::token::{self, Token};
44
use rustc_ast::tokenstream::TokenStream;
55
use rustc_errors::{Applicability, Diag, DiagCtxtHandle, DiagMessage};
66
use rustc_macros::Subdiagnostic;
@@ -66,8 +66,8 @@ pub(super) fn failed_to_match_macro(
6666
}
6767

6868
if let MatcherLoc::Token { token: expected_token } = &remaining_matcher
69-
&& (matches!(expected_token.kind, TokenKind::OpenDelim(Delimiter::Invisible(_)))
70-
|| matches!(token.kind, TokenKind::OpenDelim(Delimiter::Invisible(_))))
69+
&& (matches!(expected_token.kind, token::OpenInvisible(_))
70+
|| matches!(token.kind, token::OpenInvisible(_)))
7171
{
7272
err.note("captured metavariables except for `:tt`, `:ident` and `:lifetime` cannot be compared to other tokens");
7373
err.note("see <https://doc.rust-lang.org/nightly/reference/macros-by-example.html#forwarding-a-matched-fragment> for more information");

0 commit comments

Comments
 (0)