@@ -486,6 +486,9 @@ impl Token {
486
486
}
487
487
488
488
/// 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.
489
492
pub fn can_begin_expr ( & self ) -> bool {
490
493
match self . uninterpolate ( ) . kind {
491
494
Ident ( name, is_raw) =>
@@ -504,34 +507,46 @@ impl Token {
504
507
PathSep | // global path
505
508
Lifetime ( ..) | // labeled loop
506
509
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
+ ) ,
511
517
_ => false ,
512
518
}
513
519
}
514
520
515
521
/// Returns `true` if the token can appear at the start of a pattern.
516
522
///
517
523
/// 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
+ ) ,
535
550
_ => false ,
536
551
}
537
552
}
0 commit comments