Skip to content

Commit 8b8378d

Browse files
committed
Replace ast::TokenKind::BinOp{,Eq} and remove BinOpToken.
`BinOpToken` is badly named, because it only covers the assignable binary ops and excludes comparisons and `&&`/`||`. Its use in `ast::TokenKind` does allow a small amount of code sharing, but it's a clumsy factoring. This commit removes `ast::TokenKind::BinOp{,Eq}`, replacing each one with 10 individual variants. This makes `ast::TokenKind` more similar to `rustc_lexer::TokenKind`, which has individual variants for all operators. Although the number of lines of code increases, the number of chars decreases due to the frequent use of shorter names like `token::Plus` instead of `token::BinOp(BinOpToken::Plus)`.
1 parent 5ef8e96 commit 8b8378d

File tree

19 files changed

+349
-308
lines changed

19 files changed

+349
-308
lines changed

compiler/rustc_ast/src/token.rs

Lines changed: 166 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::borrow::Cow;
22
use std::fmt;
33

4-
pub use BinOpToken::*;
54
pub use LitKind::*;
65
pub use Nonterminal::*;
76
pub use NtExprKind::*;
@@ -26,21 +25,6 @@ pub enum CommentKind {
2625
Block,
2726
}
2827

29-
#[derive(Clone, PartialEq, Encodable, Decodable, Hash, Debug, Copy)]
30-
#[derive(HashStable_Generic)]
31-
pub enum BinOpToken {
32-
Plus,
33-
Minus,
34-
Star,
35-
Slash,
36-
Percent,
37-
Caret,
38-
And,
39-
Or,
40-
Shl,
41-
Shr,
42-
}
43-
4428
// This type must not implement `Hash` due to the unusual `PartialEq` impl below.
4529
#[derive(Copy, Clone, Debug, Encodable, Decodable, HashStable_Generic)]
4630
pub enum InvisibleOrigin {
@@ -374,8 +358,46 @@ pub enum TokenKind {
374358
Not,
375359
/// `~`
376360
Tilde,
377-
BinOp(BinOpToken),
378-
BinOpEq(BinOpToken),
361+
// `+`
362+
Plus,
363+
// `-`
364+
Minus,
365+
// `*`
366+
Star,
367+
// `/`
368+
Slash,
369+
// `%`
370+
Percent,
371+
// `^`
372+
Caret,
373+
// `&`
374+
And,
375+
// `|`
376+
Or,
377+
// `<<`
378+
Shl,
379+
// `>>`
380+
Shr,
381+
// `+=`
382+
PlusEq,
383+
// `-=`
384+
MinusEq,
385+
// `*=`
386+
StarEq,
387+
// `/=`
388+
SlashEq,
389+
// `%=`
390+
PercentEq,
391+
// `^=`
392+
CaretEq,
393+
// `&=`
394+
AndEq,
395+
// `|=`
396+
OrEq,
397+
// `<<=`
398+
ShlEq,
399+
// `>>=`
400+
ShrEq,
379401

380402
/* Structural symbols */
381403
/// `@`
@@ -497,29 +519,29 @@ impl TokenKind {
497519
(EqEq, 1) => (Eq, Eq),
498520
(Ne, 1) => (Not, Eq),
499521
(Ge, 1) => (Gt, Eq),
500-
(AndAnd, 1) => (BinOp(And), BinOp(And)),
501-
(OrOr, 1) => (BinOp(Or), BinOp(Or)),
502-
(BinOp(Shl), 1) => (Lt, Lt),
503-
(BinOp(Shr), 1) => (Gt, Gt),
504-
(BinOpEq(Plus), 1) => (BinOp(Plus), Eq),
505-
(BinOpEq(Minus), 1) => (BinOp(Minus), Eq),
506-
(BinOpEq(Star), 1) => (BinOp(Star), Eq),
507-
(BinOpEq(Slash), 1) => (BinOp(Slash), Eq),
508-
(BinOpEq(Percent), 1) => (BinOp(Percent), Eq),
509-
(BinOpEq(Caret), 1) => (BinOp(Caret), Eq),
510-
(BinOpEq(And), 1) => (BinOp(And), Eq),
511-
(BinOpEq(Or), 1) => (BinOp(Or), Eq),
512-
(BinOpEq(Shl), 1) => (Lt, Le), // `<` + `<=`
513-
(BinOpEq(Shl), 2) => (BinOp(Shl), Eq), // `<<` + `=`
514-
(BinOpEq(Shr), 1) => (Gt, Ge), // `>` + `>=`
515-
(BinOpEq(Shr), 2) => (BinOp(Shr), Eq), // `>>` + `=`
522+
(AndAnd, 1) => (And, And),
523+
(OrOr, 1) => (Or, Or),
524+
(Shl, 1) => (Lt, Lt),
525+
(Shr, 1) => (Gt, Gt),
526+
(PlusEq, 1) => (Plus, Eq),
527+
(MinusEq, 1) => (Minus, Eq),
528+
(StarEq, 1) => (Star, Eq),
529+
(SlashEq, 1) => (Slash, Eq),
530+
(PercentEq, 1) => (Percent, Eq),
531+
(CaretEq, 1) => (Caret, Eq),
532+
(AndEq, 1) => (And, Eq),
533+
(OrEq, 1) => (Or, Eq),
534+
(ShlEq, 1) => (Lt, Le), // `<` + `<=`
535+
(ShlEq, 2) => (Shl, Eq), // `<<` + `=`
536+
(ShrEq, 1) => (Gt, Ge), // `>` + `>=`
537+
(ShrEq, 2) => (Shr, Eq), // `>>` + `=`
516538
(DotDot, 1) => (Dot, Dot),
517539
(DotDotDot, 1) => (Dot, DotDot), // `.` + `..`
518540
(DotDotDot, 2) => (DotDot, Dot), // `..` + `.`
519541
(DotDotEq, 2) => (DotDot, Eq),
520542
(PathSep, 1) => (Colon, Colon),
521-
(RArrow, 1) => (BinOp(Minus), Gt),
522-
(LArrow, 1) => (Lt, BinOp(Minus)),
543+
(RArrow, 1) => (Minus, Gt),
544+
(LArrow, 1) => (Lt, Minus),
523545
(FatArrow, 1) => (Eq, Gt),
524546
_ => return None,
525547
})
@@ -538,7 +560,7 @@ impl TokenKind {
538560
}
539561

540562
pub fn should_end_const_arg(&self) -> bool {
541-
matches!(self, Gt | Ge | BinOp(Shr) | BinOpEq(Shr))
563+
matches!(self, Gt | Ge | Shr | ShrEq)
542564
}
543565
}
544566

@@ -577,19 +599,19 @@ impl Token {
577599

578600
pub fn is_punct(&self) -> bool {
579601
match self.kind {
580-
Eq | Lt | Le | EqEq | Ne | Ge | Gt | AndAnd | OrOr | Not | Tilde | BinOp(_)
581-
| BinOpEq(_) | At | Dot | DotDot | DotDotDot | DotDotEq | Comma | Semi | Colon
582-
| PathSep | RArrow | LArrow | FatArrow | Pound | Dollar | Question | SingleQuote => {
583-
true
584-
}
602+
Eq | Lt | Le | EqEq | Ne | Ge | Gt | AndAnd | OrOr | Not | Tilde | Plus | Minus
603+
| Star | Slash | Percent | Caret | And | Or | Shl | Shr | PlusEq | MinusEq | StarEq
604+
| SlashEq | PercentEq | CaretEq | AndEq | OrEq | ShlEq | ShrEq | At | Dot | DotDot
605+
| DotDotDot | DotDotEq | Comma | Semi | Colon | PathSep | RArrow | LArrow
606+
| FatArrow | Pound | Dollar | Question | SingleQuote => true,
585607

586608
OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) | Ident(..)
587609
| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Interpolated(..) | Eof => false,
588610
}
589611
}
590612

591613
pub fn is_like_plus(&self) -> bool {
592-
matches!(self.kind, BinOp(Plus) | BinOpEq(Plus))
614+
matches!(self.kind, Plus | PlusEq)
593615
}
594616

595617
/// Returns `true` if the token can appear at the start of an expression.
@@ -604,14 +626,14 @@ impl Token {
604626
OpenDelim(Parenthesis | Brace | Bracket) | // tuple, array or block
605627
Literal(..) | // literal
606628
Not | // operator not
607-
BinOp(Minus) | // unary minus
608-
BinOp(Star) | // dereference
609-
BinOp(Or) | OrOr | // closure
610-
BinOp(And) | // reference
629+
Minus | // unary minus
630+
Star | // dereference
631+
Or | OrOr | // closure
632+
And | // reference
611633
AndAnd | // double reference
612634
// DotDotDot is no longer supported, but we need some way to display the error
613635
DotDot | DotDotDot | DotDotEq | // range notation
614-
Lt | BinOp(Shl) | // associated path
636+
Lt | Shl | // associated path
615637
PathSep | // global path
616638
Lifetime(..) | // labeled loop
617639
Pound => true, // expression attributes
@@ -641,17 +663,16 @@ impl Token {
641663
Ident(..) | NtIdent(..) |
642664
OpenDelim(Delimiter::Parenthesis) | // tuple pattern
643665
OpenDelim(Delimiter::Bracket) | // slice pattern
644-
BinOp(And) | // reference
645-
BinOp(Minus) | // negative literal
646-
AndAnd | // double reference
647-
Literal(_) | // literal
648-
DotDot | // range pattern (future compat)
649-
DotDotDot | // range pattern (future compat)
650-
PathSep | // path
651-
Lt | // path (UFCS constant)
652-
BinOp(Shl) => true, // path (double UFCS)
653-
// leading vert `|` or-pattern
654-
BinOp(Or) => matches!(pat_kind, PatWithOr),
666+
And | // reference
667+
Minus | // negative literal
668+
AndAnd | // double reference
669+
Literal(_) | // literal
670+
DotDot | // range pattern (future compat)
671+
DotDotDot | // range pattern (future compat)
672+
PathSep | // path
673+
Lt | // path (UFCS constant)
674+
Shl => true, // path (double UFCS)
675+
Or => matches!(pat_kind, PatWithOr), // leading vert `|` or-pattern
655676
Interpolated(nt) =>
656677
matches!(&**nt,
657678
| NtExpr(..)
@@ -680,14 +701,14 @@ impl Token {
680701
ident_can_begin_type(name, self.span, is_raw), // type name or keyword
681702
OpenDelim(Delimiter::Parenthesis) | // tuple
682703
OpenDelim(Delimiter::Bracket) | // array
683-
Not | // never
684-
BinOp(Star) | // raw pointer
685-
BinOp(And) | // reference
686-
AndAnd | // double reference
687-
Question | // maybe bound in trait object
688-
Lifetime(..) | // lifetime bound in trait object
689-
Lt | BinOp(Shl) | // associated path
690-
PathSep => true, // global path
704+
Not | // never
705+
Star | // raw pointer
706+
And | // reference
707+
AndAnd | // double reference
708+
Question | // maybe bound in trait object
709+
Lifetime(..) | // lifetime bound in trait object
710+
Lt | Shl | // associated path
711+
PathSep => true, // global path
691712
Interpolated(ref nt) => matches!(&**nt, NtTy(..) | NtPath(..)),
692713
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
693714
MetaVarKind::Ty |
@@ -702,7 +723,7 @@ impl Token {
702723
/// Returns `true` if the token can appear at the start of a const param.
703724
pub fn can_begin_const_arg(&self) -> bool {
704725
match self.kind {
705-
OpenDelim(Delimiter::Brace) | Literal(..) | BinOp(Minus) => true,
726+
OpenDelim(Delimiter::Brace) | Literal(..) | Minus => true,
706727
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
707728
Interpolated(ref nt) => matches!(&**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
708729
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
@@ -751,7 +772,7 @@ impl Token {
751772
/// Keep this in sync with and `Lit::from_token`, excluding unary negation.
752773
pub fn can_begin_literal_maybe_minus(&self) -> bool {
753774
match self.uninterpolate().kind {
754-
Literal(..) | BinOp(Minus) => true,
775+
Literal(..) | Minus => true,
755776
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
756777
Interpolated(ref nt) => match &**nt {
757778
NtLiteral(_) => true,
@@ -887,7 +908,7 @@ impl Token {
887908
}
888909

889910
pub fn is_qpath_start(&self) -> bool {
890-
self == &Lt || self == &BinOp(Shl)
911+
self == &Lt || self == &Shl
891912
}
892913

893914
pub fn is_path_start(&self) -> bool {
@@ -970,59 +991,82 @@ impl Token {
970991
}
971992

972993
pub fn glue(&self, joint: &Token) -> Option<Token> {
973-
let kind = match self.kind {
974-
Eq => match joint.kind {
975-
Eq => EqEq,
976-
Gt => FatArrow,
977-
_ => return None,
978-
},
979-
Lt => match joint.kind {
980-
Eq => Le,
981-
Lt => BinOp(Shl),
982-
Le => BinOpEq(Shl),
983-
BinOp(Minus) => LArrow,
984-
_ => return None,
985-
},
986-
Gt => match joint.kind {
987-
Eq => Ge,
988-
Gt => BinOp(Shr),
989-
Ge => BinOpEq(Shr),
990-
_ => return None,
991-
},
992-
Not => match joint.kind {
993-
Eq => Ne,
994-
_ => return None,
995-
},
996-
BinOp(op) => match joint.kind {
997-
Eq => BinOpEq(op),
998-
BinOp(And) if op == And => AndAnd,
999-
BinOp(Or) if op == Or => OrOr,
1000-
Gt if op == Minus => RArrow,
1001-
_ => return None,
1002-
},
1003-
Dot => match joint.kind {
1004-
Dot => DotDot,
1005-
DotDot => DotDotDot,
1006-
_ => return None,
1007-
},
1008-
DotDot => match joint.kind {
1009-
Dot => DotDotDot,
1010-
Eq => DotDotEq,
1011-
_ => return None,
1012-
},
1013-
Colon => match joint.kind {
1014-
Colon => PathSep,
1015-
_ => return None,
1016-
},
1017-
SingleQuote => match joint.kind {
1018-
Ident(name, is_raw) => Lifetime(Symbol::intern(&format!("'{name}")), is_raw),
1019-
_ => return None,
1020-
},
994+
let kind = match (&self.kind, &joint.kind) {
995+
(Eq, Eq) => EqEq,
996+
(Eq, Gt) => FatArrow,
997+
(Eq, _) => return None,
998+
999+
(Lt, Eq) => Le,
1000+
(Lt, Lt) => Shl,
1001+
(Lt, Le) => ShlEq,
1002+
(Lt, Minus) => LArrow,
1003+
(Lt, _) => return None,
1004+
1005+
(Gt, Eq) => Ge,
1006+
(Gt, Gt) => Shr,
1007+
(Gt, Ge) => ShrEq,
1008+
(Gt, _) => return None,
1009+
1010+
(Not, Eq) => Ne,
1011+
(Not, _) => return None,
1012+
1013+
(Plus, Eq) => PlusEq,
1014+
(Plus, _) => return None,
1015+
1016+
(Minus, Eq) => MinusEq,
1017+
(Minus, Gt) => RArrow,
1018+
(Minus, _) => return None,
10211019

1022-
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | BinOpEq(..) | At | DotDotDot
1023-
| DotDotEq | Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar
1024-
| Question | OpenDelim(..) | CloseDelim(..) | Literal(..) | Ident(..) | NtIdent(..)
1025-
| Lifetime(..) | NtLifetime(..) | Interpolated(..) | DocComment(..) | Eof => {
1020+
(Star, Eq) => StarEq,
1021+
(Star, _) => return None,
1022+
1023+
(Slash, Eq) => SlashEq,
1024+
(Slash, _) => return None,
1025+
1026+
(Percent, Eq) => PercentEq,
1027+
(Percent, _) => return None,
1028+
1029+
(Caret, Eq) => CaretEq,
1030+
(Caret, _) => return None,
1031+
1032+
(And, Eq) => AndEq,
1033+
(And, And) => AndAnd,
1034+
(And, _) => return None,
1035+
1036+
(Or, Eq) => OrEq,
1037+
(Or, Or) => OrOr,
1038+
(Or, _) => return None,
1039+
1040+
(Shl, Eq) => ShlEq,
1041+
(Shl, _) => return None,
1042+
1043+
(Shr, Eq) => ShrEq,
1044+
(Shr, _) => return None,
1045+
1046+
(Dot, Dot) => DotDot,
1047+
(Dot, DotDot) => DotDotDot,
1048+
(Dot, _) => return None,
1049+
1050+
(DotDot, Dot) => DotDotDot,
1051+
(DotDot, Eq) => DotDotEq,
1052+
(DotDot, _) => return None,
1053+
1054+
(Colon, Colon) => PathSep,
1055+
(Colon, _) => return None,
1056+
1057+
(SingleQuote, Ident(name, is_raw)) => {
1058+
Lifetime(Symbol::intern(&format!("'{name}")), *is_raw)
1059+
}
1060+
(SingleQuote, _) => return None,
1061+
1062+
(
1063+
Le | EqEq | Ne | Ge | AndAnd | OrOr | Tilde | PlusEq | MinusEq | StarEq | SlashEq
1064+
| PercentEq | CaretEq | AndEq | OrEq | ShlEq | ShrEq | At | DotDotDot | DotDotEq
1065+
| Comma | Semi | PathSep | RArrow | LArrow | FatArrow | Pound | Dollar | Question
1066+
| OpenDelim(..) | CloseDelim(..) | Literal(..) | Ident(..) | NtIdent(..)
1067+
| Lifetime(..) | NtLifetime(..) | Interpolated(..) | DocComment(..) | Eof,
1068+
_,
1069+
) => {
10261070
return None;
10271071
}
10281072
};

0 commit comments

Comments
 (0)