Skip to content

Commit 2a1e2e9

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 7c4a55c commit 2a1e2e9

File tree

19 files changed

+352
-309
lines changed

19 files changed

+352
-309
lines changed

compiler/rustc_ast/src/token.rs

+167-123
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use std::borrow::Cow;
22
use std::fmt;
33
use std::sync::Arc;
44

5-
pub use BinOpToken::*;
65
pub use LitKind::*;
76
pub use Nonterminal::*;
87
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 {
@@ -379,8 +363,46 @@ pub enum TokenKind {
379363
Not,
380364
/// `~`
381365
Tilde,
382-
BinOp(BinOpToken),
383-
BinOpEq(BinOpToken),
366+
// `+`
367+
Plus,
368+
// `-`
369+
Minus,
370+
// `*`
371+
Star,
372+
// `/`
373+
Slash,
374+
// `%`
375+
Percent,
376+
// `^`
377+
Caret,
378+
// `&`
379+
And,
380+
// `|`
381+
Or,
382+
// `<<`
383+
Shl,
384+
// `>>`
385+
Shr,
386+
// `+=`
387+
PlusEq,
388+
// `-=`
389+
MinusEq,
390+
// `*=`
391+
StarEq,
392+
// `/=`
393+
SlashEq,
394+
// `%=`
395+
PercentEq,
396+
// `^=`
397+
CaretEq,
398+
// `&=`
399+
AndEq,
400+
// `|=`
401+
OrEq,
402+
// `<<=`
403+
ShlEq,
404+
// `>>=`
405+
ShrEq,
384406

385407
/* Structural symbols */
386408
/// `@`
@@ -502,29 +524,29 @@ impl TokenKind {
502524
(EqEq, 1) => (Eq, Eq),
503525
(Ne, 1) => (Not, Eq),
504526
(Ge, 1) => (Gt, Eq),
505-
(AndAnd, 1) => (BinOp(And), BinOp(And)),
506-
(OrOr, 1) => (BinOp(Or), BinOp(Or)),
507-
(BinOp(Shl), 1) => (Lt, Lt),
508-
(BinOp(Shr), 1) => (Gt, Gt),
509-
(BinOpEq(Plus), 1) => (BinOp(Plus), Eq),
510-
(BinOpEq(Minus), 1) => (BinOp(Minus), Eq),
511-
(BinOpEq(Star), 1) => (BinOp(Star), Eq),
512-
(BinOpEq(Slash), 1) => (BinOp(Slash), Eq),
513-
(BinOpEq(Percent), 1) => (BinOp(Percent), Eq),
514-
(BinOpEq(Caret), 1) => (BinOp(Caret), Eq),
515-
(BinOpEq(And), 1) => (BinOp(And), Eq),
516-
(BinOpEq(Or), 1) => (BinOp(Or), Eq),
517-
(BinOpEq(Shl), 1) => (Lt, Le), // `<` + `<=`
518-
(BinOpEq(Shl), 2) => (BinOp(Shl), Eq), // `<<` + `=`
519-
(BinOpEq(Shr), 1) => (Gt, Ge), // `>` + `>=`
520-
(BinOpEq(Shr), 2) => (BinOp(Shr), Eq), // `>>` + `=`
527+
(AndAnd, 1) => (And, And),
528+
(OrOr, 1) => (Or, Or),
529+
(Shl, 1) => (Lt, Lt),
530+
(Shr, 1) => (Gt, Gt),
531+
(PlusEq, 1) => (Plus, Eq),
532+
(MinusEq, 1) => (Minus, Eq),
533+
(StarEq, 1) => (Star, Eq),
534+
(SlashEq, 1) => (Slash, Eq),
535+
(PercentEq, 1) => (Percent, Eq),
536+
(CaretEq, 1) => (Caret, Eq),
537+
(AndEq, 1) => (And, Eq),
538+
(OrEq, 1) => (Or, Eq),
539+
(ShlEq, 1) => (Lt, Le), // `<` + `<=`
540+
(ShlEq, 2) => (Shl, Eq), // `<<` + `=`
541+
(ShrEq, 1) => (Gt, Ge), // `>` + `>=`
542+
(ShrEq, 2) => (Shr, Eq), // `>>` + `=`
521543
(DotDot, 1) => (Dot, Dot),
522544
(DotDotDot, 1) => (Dot, DotDot), // `.` + `..`
523545
(DotDotDot, 2) => (DotDot, Dot), // `..` + `.`
524546
(DotDotEq, 2) => (DotDot, Eq),
525547
(PathSep, 1) => (Colon, Colon),
526-
(RArrow, 1) => (BinOp(Minus), Gt),
527-
(LArrow, 1) => (Lt, BinOp(Minus)),
548+
(RArrow, 1) => (Minus, Gt),
549+
(LArrow, 1) => (Lt, Minus),
528550
(FatArrow, 1) => (Eq, Gt),
529551
_ => return None,
530552
})
@@ -543,7 +565,7 @@ impl TokenKind {
543565
}
544566

545567
pub fn should_end_const_arg(&self) -> bool {
546-
matches!(self, Gt | Ge | BinOp(Shr) | BinOpEq(Shr))
568+
matches!(self, Gt | Ge | Shr | ShrEq)
547569
}
548570
}
549571

@@ -582,19 +604,19 @@ impl Token {
582604

583605
pub fn is_punct(&self) -> bool {
584606
match self.kind {
585-
Eq | Lt | Le | EqEq | Ne | Ge | Gt | AndAnd | OrOr | Not | Tilde | BinOp(_)
586-
| BinOpEq(_) | At | Dot | DotDot | DotDotDot | DotDotEq | Comma | Semi | Colon
587-
| PathSep | RArrow | LArrow | FatArrow | Pound | Dollar | Question | SingleQuote => {
588-
true
589-
}
607+
Eq | Lt | Le | EqEq | Ne | Ge | Gt | AndAnd | OrOr | Not | Tilde | Plus | Minus
608+
| Star | Slash | Percent | Caret | And | Or | Shl | Shr | PlusEq | MinusEq | StarEq
609+
| SlashEq | PercentEq | CaretEq | AndEq | OrEq | ShlEq | ShrEq | At | Dot | DotDot
610+
| DotDotDot | DotDotEq | Comma | Semi | Colon | PathSep | RArrow | LArrow
611+
| FatArrow | Pound | Dollar | Question | SingleQuote => true,
590612

591613
OpenDelim(..) | CloseDelim(..) | Literal(..) | DocComment(..) | Ident(..)
592614
| NtIdent(..) | Lifetime(..) | NtLifetime(..) | Interpolated(..) | Eof => false,
593615
}
594616
}
595617

596618
pub fn is_like_plus(&self) -> bool {
597-
matches!(self.kind, BinOp(Plus) | BinOpEq(Plus))
619+
matches!(self.kind, Plus | PlusEq)
598620
}
599621

600622
/// Returns `true` if the token can appear at the start of an expression.
@@ -609,14 +631,14 @@ impl Token {
609631
OpenDelim(Parenthesis | Brace | Bracket) | // tuple, array or block
610632
Literal(..) | // literal
611633
Not | // operator not
612-
BinOp(Minus) | // unary minus
613-
BinOp(Star) | // dereference
614-
BinOp(Or) | OrOr | // closure
615-
BinOp(And) | // reference
634+
Minus | // unary minus
635+
Star | // dereference
636+
Or | OrOr | // closure
637+
And | // reference
616638
AndAnd | // double reference
617639
// DotDotDot is no longer supported, but we need some way to display the error
618640
DotDot | DotDotDot | DotDotEq | // range notation
619-
Lt | BinOp(Shl) | // associated path
641+
Lt | Shl | // associated path
620642
PathSep | // global path
621643
Lifetime(..) | // labeled loop
622644
Pound => true, // expression attributes
@@ -645,17 +667,16 @@ impl Token {
645667
Ident(..) | NtIdent(..) |
646668
OpenDelim(Delimiter::Parenthesis) | // tuple pattern
647669
OpenDelim(Delimiter::Bracket) | // slice pattern
648-
BinOp(And) | // reference
649-
BinOp(Minus) | // negative literal
650-
AndAnd | // double reference
651-
Literal(_) | // literal
652-
DotDot | // range pattern (future compat)
653-
DotDotDot | // range pattern (future compat)
654-
PathSep | // path
655-
Lt | // path (UFCS constant)
656-
BinOp(Shl) => true, // path (double UFCS)
657-
// leading vert `|` or-pattern
658-
BinOp(Or) => matches!(pat_kind, PatWithOr),
670+
And | // reference
671+
Minus | // negative literal
672+
AndAnd | // double reference
673+
Literal(_) | // literal
674+
DotDot | // range pattern (future compat)
675+
DotDotDot | // range pattern (future compat)
676+
PathSep | // path
677+
Lt | // path (UFCS constant)
678+
Shl => true, // path (double UFCS)
679+
Or => matches!(pat_kind, PatWithOr), // leading vert `|` or-pattern
659680
Interpolated(nt) =>
660681
matches!(&**nt,
661682
| NtExpr(..)
@@ -676,18 +697,18 @@ impl Token {
676697
/// Returns `true` if the token can appear at the start of a type.
677698
pub fn can_begin_type(&self) -> bool {
678699
match self.uninterpolate().kind {
679-
Ident(name, is_raw) =>
700+
Ident(name, is_raw) =>
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
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
692713
MetaVarKind::Ty { .. } |
693714
MetaVarKind::Path
@@ -701,7 +722,7 @@ impl Token {
701722
/// Returns `true` if the token can appear at the start of a const param.
702723
pub fn can_begin_const_arg(&self) -> bool {
703724
match self.kind {
704-
OpenDelim(Delimiter::Brace) | Literal(..) | BinOp(Minus) => true,
725+
OpenDelim(Delimiter::Brace) | Literal(..) | Minus => true,
705726
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
706727
Interpolated(ref nt) => matches!(&**nt, NtExpr(..) | NtBlock(..) | NtLiteral(..)),
707728
OpenDelim(Delimiter::Invisible(InvisibleOrigin::MetaVar(
@@ -750,7 +771,7 @@ impl Token {
750771
/// Keep this in sync with and `Lit::from_token`, excluding unary negation.
751772
pub fn can_begin_literal_maybe_minus(&self) -> bool {
752773
match self.uninterpolate().kind {
753-
Literal(..) | BinOp(Minus) => true,
774+
Literal(..) | Minus => true,
754775
Ident(name, IdentIsRaw::No) if name.is_bool_lit() => true,
755776
Interpolated(ref nt) => match &**nt {
756777
NtLiteral(_) => true,
@@ -875,7 +896,7 @@ impl Token {
875896
}
876897

877898
pub fn is_qpath_start(&self) -> bool {
878-
self == &Lt || self == &BinOp(Shl)
899+
self == &Lt || self == &Shl
879900
}
880901

881902
pub fn is_path_start(&self) -> bool {
@@ -967,59 +988,82 @@ impl Token {
967988
}
968989

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

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

0 commit comments

Comments
 (0)