@@ -3804,6 +3804,12 @@ impl<'a> Parser<'a> {
38043804
38053805 /// Parse a pattern.
38063806 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 > > {
38073813 maybe_whole ! ( self , NtPat , |x| x) ;
38083814
38093815 let lo = self . span ;
@@ -3824,7 +3830,7 @@ impl<'a> Parser<'a> {
38243830 err. span_label ( self . span , "unexpected lifetime" ) ;
38253831 return Err ( err) ;
38263832 }
3827- let subpat = self . parse_pat ( ) ?;
3833+ let subpat = self . parse_pat_with_range_pat ( false ) ?;
38283834 pat = PatKind :: Ref ( subpat, mutbl) ;
38293835 }
38303836 token:: OpenDelim ( token:: Paren ) => {
@@ -3863,7 +3869,7 @@ impl<'a> Parser<'a> {
38633869 pat = self . parse_pat_ident ( BindingMode :: ByRef ( mutbl) ) ?;
38643870 } else if self . eat_keyword ( keywords:: Box ) {
38653871 // Parse box pat
3866- let subpat = self . parse_pat ( ) ?;
3872+ let subpat = self . parse_pat_with_range_pat ( false ) ?;
38673873 pat = PatKind :: Box ( subpat) ;
38683874 } else if self . token . is_ident ( ) && !self . token . is_reserved_ident ( ) &&
38693875 self . parse_as_ident ( ) {
@@ -3968,6 +3974,25 @@ impl<'a> Parser<'a> {
39683974 let pat = Pat { node : pat, span : lo. to ( self . prev_span ) , id : ast:: DUMMY_NODE_ID } ;
39693975 let pat = self . maybe_recover_from_bad_qpath ( pat, true ) ?;
39703976
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+
39713996 Ok ( P ( pat) )
39723997 }
39733998
0 commit comments