@@ -486,6 +486,9 @@ impl Token {
486486 }
487487
488488 /// Returns `true` if the token can appear at the start of an expression.
489+ ///
490+ /// **NB**: Take care when modifying this function, since it will change
491+ /// the stable set of tokens that are allowed to match an expr nonterminal.
489492 pub fn can_begin_expr ( & self ) -> bool {
490493 match self . uninterpolate ( ) . kind {
491494 Ident ( name, is_raw) =>
@@ -504,34 +507,46 @@ impl Token {
504507 PathSep | // global path
505508 Lifetime ( ..) | // labeled loop
506509 Pound => true , // expression attributes
507- Interpolated ( ref nt) => matches ! ( & * * nt, NtLiteral ( ..) |
508- NtExpr ( ..) |
509- NtBlock ( ..) |
510- NtPath ( ..) ) ,
510+ Interpolated ( ref nt) =>
511+ matches ! ( & * * nt,
512+ NtBlock ( ..) |
513+ NtExpr ( ..) |
514+ NtLiteral ( ..) |
515+ NtPath ( ..)
516+ ) ,
511517 _ => false ,
512518 }
513519 }
514520
515521 /// Returns `true` if the token can appear at the start of a pattern.
516522 ///
517523 /// Shamelessly borrowed from `can_begin_expr`, only used for diagnostics right now.
518- pub fn can_begin_pattern ( & self ) -> bool {
519- match self . uninterpolate ( ) . kind {
520- Ident ( name, is_raw) =>
521- ident_can_begin_expr ( name, self . span , is_raw) , // value name or keyword
522- | OpenDelim ( Delimiter :: Bracket | Delimiter :: Parenthesis ) // tuple or array
523- | Literal ( ..) // literal
524- | BinOp ( Minus ) // unary minus
525- | BinOp ( And ) // reference
526- | AndAnd // double reference
527- // DotDotDot is no longer supported
528- | DotDot | DotDotDot | DotDotEq // ranges
529- | Lt | BinOp ( Shl ) // associated path
530- | PathSep => true , // global path
531- Interpolated ( ref nt) => matches ! ( & * * nt, NtLiteral ( ..) |
532- NtPat ( ..) |
533- NtBlock ( ..) |
534- NtPath ( ..) ) ,
524+ pub fn can_begin_pattern ( & self , pat_kind : NtPatKind ) -> bool {
525+ match & self . uninterpolate ( ) . kind {
526+ // box, ref, mut, and other identifiers (can stricten)
527+ Ident ( ..) | NtIdent ( ..) |
528+ OpenDelim ( Delimiter :: Parenthesis ) | // tuple pattern
529+ OpenDelim ( Delimiter :: Bracket ) | // slice pattern
530+ BinOp ( And ) | // reference
531+ BinOp ( Minus ) | // negative literal
532+ AndAnd | // double reference
533+ Literal ( _) | // literal
534+ DotDot | // range pattern (future compat)
535+ DotDotDot | // range pattern (future compat)
536+ PathSep | // path
537+ Lt | // path (UFCS constant)
538+ BinOp ( Shl ) => true , // path (double UFCS)
539+ // leading vert `|` or-pattern
540+ BinOp ( Or ) => matches ! ( pat_kind, PatWithOr ) ,
541+ Interpolated ( nt) =>
542+ matches ! ( & * * nt,
543+ | NtExpr ( ..)
544+ | NtLiteral ( ..)
545+ | NtMeta ( ..)
546+ | NtPat ( ..)
547+ | NtPath ( ..)
548+ | NtTy ( ..)
549+ ) ,
535550 _ => false ,
536551 }
537552 }
0 commit comments