@@ -132,7 +132,7 @@ impl<'a> Parser<'a> {
132132 Ok ( expr) => Ok ( expr) ,
133133 Err ( mut err) => match self . token . ident ( ) {
134134 Some ( ( Ident { name : kw:: Underscore , .. } , false ) )
135- if self . look_ahead ( 1 , |t| t == & token:: Comma ) =>
135+ if self . may_recover ( ) && self . look_ahead ( 1 , |t| t == & token:: Comma ) =>
136136 {
137137 // Special-case handling of `foo(_, _, _)`
138138 err. emit ( ) ;
@@ -456,15 +456,15 @@ impl<'a> Parser<'a> {
456456 return None ;
457457 }
458458 ( Some ( op) , _) => ( op, self . token . span ) ,
459- ( None , Some ( ( Ident { name : sym:: and, span } , false ) ) ) => {
459+ ( None , Some ( ( Ident { name : sym:: and, span } , false ) ) ) if self . may_recover ( ) => {
460460 self . sess . emit_err ( InvalidLogicalOperator {
461461 span : self . token . span ,
462462 incorrect : "and" . into ( ) ,
463463 sub : InvalidLogicalOperatorSub :: Conjunction ( self . token . span ) ,
464464 } ) ;
465465 ( AssocOp :: LAnd , span)
466466 }
467- ( None , Some ( ( Ident { name : sym:: or, span } , false ) ) ) => {
467+ ( None , Some ( ( Ident { name : sym:: or, span } , false ) ) ) if self . may_recover ( ) => {
468468 self . sess . emit_err ( InvalidLogicalOperator {
469469 span : self . token . span ,
470470 incorrect : "or" . into ( ) ,
@@ -615,7 +615,7 @@ impl<'a> Parser<'a> {
615615 token:: Ident ( ..) if this. token . is_keyword ( kw:: Box ) => {
616616 make_it ! ( this, attrs, |this, _| this. parse_box_expr( lo) )
617617 }
618- token:: Ident ( ..) if this. is_mistaken_not_ident_negation ( ) => {
618+ token:: Ident ( ..) if this. may_recover ( ) && this . is_mistaken_not_ident_negation ( ) => {
619619 make_it ! ( this, attrs, |this, _| this. recover_not_expr( lo) )
620620 }
621621 _ => return this. parse_dot_or_call_expr ( Some ( attrs) ) ,
@@ -718,6 +718,10 @@ impl<'a> Parser<'a> {
718718 let cast_expr = match self . parse_as_cast_ty ( ) {
719719 Ok ( rhs) => mk_expr ( self , lhs, rhs) ,
720720 Err ( type_err) => {
721+ if !self . may_recover ( ) {
722+ return Err ( type_err) ;
723+ }
724+
721725 // Rewind to before attempting to parse the type with generics, to recover
722726 // from situations like `x as usize < y` in which we first tried to parse
723727 // `usize < y` as a type with generic arguments.
@@ -1197,6 +1201,10 @@ impl<'a> Parser<'a> {
11971201 seq : & mut PResult < ' a , P < Expr > > ,
11981202 snapshot : Option < ( SnapshotParser < ' a > , ExprKind ) > ,
11991203 ) -> Option < P < Expr > > {
1204+ if !self . may_recover ( ) {
1205+ return None ;
1206+ }
1207+
12001208 match ( seq. as_mut ( ) , snapshot) {
12011209 ( Err ( err) , Some ( ( mut snapshot, ExprKind :: Path ( None , path) ) ) ) => {
12021210 snapshot. bump ( ) ; // `(`
@@ -1360,7 +1368,7 @@ impl<'a> Parser<'a> {
13601368 )
13611369 } else if self . check_inline_const ( 0 ) {
13621370 self . parse_const_block ( lo. to ( self . token . span ) , false )
1363- } else if self . is_do_catch_block ( ) {
1371+ } else if self . may_recover ( ) && self . is_do_catch_block ( ) {
13641372 self . recover_do_catch ( )
13651373 } else if self . is_try_block ( ) {
13661374 self . expect_keyword ( kw:: Try ) ?;
@@ -1532,6 +1540,7 @@ impl<'a> Parser<'a> {
15321540 {
15331541 self . parse_block_expr ( label, lo, BlockCheckMode :: Default )
15341542 } else if !ate_colon
1543+ && self . may_recover ( )
15351544 && ( matches ! ( self . token. kind, token:: CloseDelim ( _) | token:: Comma )
15361545 || self . token . is_op ( ) )
15371546 {
@@ -1999,6 +2008,10 @@ impl<'a> Parser<'a> {
19992008 prev_span : Span ,
20002009 open_delim_span : Span ,
20012010 ) -> PResult < ' a , ( ) > {
2011+ if !self . may_recover ( ) {
2012+ return Ok ( ( ) ) ;
2013+ }
2014+
20022015 if self . token . kind == token:: Comma {
20032016 if !self . sess . source_map ( ) . is_multiline ( prev_span. until ( self . token . span ) ) {
20042017 return Ok ( ( ) ) ;
@@ -2039,7 +2052,7 @@ impl<'a> Parser<'a> {
20392052 lo : Span ,
20402053 blk_mode : BlockCheckMode ,
20412054 ) -> PResult < ' a , P < Expr > > {
2042- if self . is_array_like_block ( ) {
2055+ if self . may_recover ( ) && self . is_array_like_block ( ) {
20432056 if let Some ( arr) = self . maybe_suggest_brackets_instead_of_braces ( lo) {
20442057 return Ok ( arr) ;
20452058 }
0 commit comments