@@ -3804,6 +3804,12 @@ impl<'a> Parser<'a> {
3804
3804
3805
3805
/// Parse a pattern.
3806
3806
pub fn parse_pat ( & mut self ) -> PResult < ' a , P < Pat > > {
3807
+ self . parse_pat_with_range_pat ( true )
3808
+ }
3809
+
3810
+ /// Parse a pattern, with a setting whether modern range patterns e.g. `a..=b`, `a..b` are
3811
+ /// allowed.
3812
+ fn parse_pat_with_range_pat ( & mut self , allow_range_pat : bool ) -> PResult < ' a , P < Pat > > {
3807
3813
maybe_whole ! ( self , NtPat , |x| x) ;
3808
3814
3809
3815
let lo = self . span ;
@@ -3824,7 +3830,7 @@ impl<'a> Parser<'a> {
3824
3830
err. span_label ( self . span , "unexpected lifetime" ) ;
3825
3831
return Err ( err) ;
3826
3832
}
3827
- let subpat = self . parse_pat ( ) ?;
3833
+ let subpat = self . parse_pat_with_range_pat ( false ) ?;
3828
3834
pat = PatKind :: Ref ( subpat, mutbl) ;
3829
3835
}
3830
3836
token:: OpenDelim ( token:: Paren ) => {
@@ -3863,7 +3869,7 @@ impl<'a> Parser<'a> {
3863
3869
pat = self . parse_pat_ident ( BindingMode :: ByRef ( mutbl) ) ?;
3864
3870
} else if self . eat_keyword ( keywords:: Box ) {
3865
3871
// Parse box pat
3866
- let subpat = self . parse_pat ( ) ?;
3872
+ let subpat = self . parse_pat_with_range_pat ( false ) ?;
3867
3873
pat = PatKind :: Box ( subpat) ;
3868
3874
} else if self . token . is_ident ( ) && !self . token . is_reserved_ident ( ) &&
3869
3875
self . parse_as_ident ( ) {
@@ -3968,6 +3974,25 @@ impl<'a> Parser<'a> {
3968
3974
let pat = Pat { node : pat, span : lo. to ( self . prev_span ) , id : ast:: DUMMY_NODE_ID } ;
3969
3975
let pat = self . maybe_recover_from_bad_qpath ( pat, true ) ?;
3970
3976
3977
+ if !allow_range_pat {
3978
+ match pat. node {
3979
+ PatKind :: Range ( _, _, RangeEnd :: Included ( RangeSyntax :: DotDotDot ) ) => { }
3980
+ PatKind :: Range ( ..) => {
3981
+ let mut err = self . struct_span_err (
3982
+ pat. span ,
3983
+ "the range pattern here has ambiguous interpretation" ,
3984
+ ) ;
3985
+ err. span_suggestion (
3986
+ pat. span ,
3987
+ "add parentheses to clarify the precedence" ,
3988
+ format ! ( "({})" , pprust:: pat_to_string( & pat) ) ,
3989
+ ) ;
3990
+ return Err ( err) ;
3991
+ }
3992
+ _ => { }
3993
+ }
3994
+ }
3995
+
3971
3996
Ok ( P ( pat) )
3972
3997
}
3973
3998
0 commit comments