@@ -447,6 +447,9 @@ impl Token {
447447 }
448448
449449 /// Returns `true` if the token can appear at the start of an expression.
450+ ///
451+ /// **NB**: Take care when modifying this function, since it will change
452+ /// the stable set of tokens that are allowed to match an expr nonterminal.
450453 pub fn can_begin_expr ( & self ) -> bool {
451454 match self . uninterpolate ( ) . kind {
452455 Ident ( name, is_raw) =>
@@ -475,24 +478,34 @@ impl Token {
475478
476479 /// Returns `true` if the token can appear at the start of a pattern.
477480 ///
478- /// Shamelessly borrowed from `can_begin_expr`, only used for diagnostics right now.
479- pub fn can_begin_pattern ( & self ) -> bool {
480- match self . uninterpolate ( ) . kind {
481- Ident ( name, is_raw) =>
482- ident_can_begin_expr ( name, self . span , is_raw) , // value name or keyword
483- | OpenDelim ( Delimiter :: Bracket | Delimiter :: Parenthesis ) // tuple or array
484- | Literal ( ..) // literal
485- | BinOp ( Minus ) // unary minus
486- | BinOp ( And ) // reference
487- | AndAnd // double reference
488- // DotDotDot is no longer supported
489- | DotDot | DotDotDot | DotDotEq // ranges
490- | Lt | BinOp ( Shl ) // associated path
491- | ModSep => true , // global path
492- Interpolated ( ref nt) => matches ! ( & nt. 0 , NtLiteral ( ..) |
493- NtPat ( ..) |
494- NtBlock ( ..) |
495- NtPath ( ..) ) ,
481+ /// **NB**: Take care when modifying this function, since it will change
482+ /// the stable set of tokens that are allowed to match a pat nonterminal.
483+ pub fn can_begin_pattern ( & self , allow_leading_or : bool ) -> bool {
484+ match & self . uninterpolate ( ) . kind {
485+ Ident ( ..) | // box, ref, mut, and other identifiers (can stricten)
486+ OpenDelim ( Delimiter :: Parenthesis ) | // tuple pattern
487+ OpenDelim ( Delimiter :: Bracket ) | // slice pattern
488+ BinOp ( And ) | // reference
489+ BinOp ( Minus ) | // negative literal
490+ AndAnd | // double reference
491+ Literal ( _) | // literal
492+ DotDot | // range pattern (future compat)
493+ DotDotDot | // range pattern (future compat)
494+ ModSep | // path
495+ Lt | // path (UFCS constant)
496+ BinOp ( Shl ) => true , // path (double UFCS)
497+ // leading vert `|` or-pattern
498+ BinOp ( Or ) => allow_leading_or,
499+ Interpolated ( nt) =>
500+ matches ! ( & nt. 0 ,
501+ | NtBlock ( ..)
502+ | NtExpr ( ..)
503+ | NtLiteral ( ..)
504+ | NtMeta ( ..)
505+ | NtPat ( ..)
506+ | NtPath ( ..)
507+ | NtTy ( ..)
508+ ) ,
496509 _ => false ,
497510 }
498511 }
0 commit comments