@@ -132,7 +132,7 @@ impl<'a> Parser<'a> {
132
132
Ok ( expr) => Ok ( expr) ,
133
133
Err ( mut err) => match self . token . ident ( ) {
134
134
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 ) =>
136
136
{
137
137
// Special-case handling of `foo(_, _, _)`
138
138
err. emit ( ) ;
@@ -456,15 +456,15 @@ impl<'a> Parser<'a> {
456
456
return None ;
457
457
}
458
458
( 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 ( ) => {
460
460
self . sess . emit_err ( InvalidLogicalOperator {
461
461
span : self . token . span ,
462
462
incorrect : "and" . into ( ) ,
463
463
sub : InvalidLogicalOperatorSub :: Conjunction ( self . token . span ) ,
464
464
} ) ;
465
465
( AssocOp :: LAnd , span)
466
466
}
467
- ( None , Some ( ( Ident { name : sym:: or, span } , false ) ) ) => {
467
+ ( None , Some ( ( Ident { name : sym:: or, span } , false ) ) ) if self . may_recover ( ) => {
468
468
self . sess . emit_err ( InvalidLogicalOperator {
469
469
span : self . token . span ,
470
470
incorrect : "or" . into ( ) ,
@@ -615,7 +615,7 @@ impl<'a> Parser<'a> {
615
615
token:: Ident ( ..) if this. token . is_keyword ( kw:: Box ) => {
616
616
make_it ! ( this, attrs, |this, _| this. parse_box_expr( lo) )
617
617
}
618
- token:: Ident ( ..) if this. is_mistaken_not_ident_negation ( ) => {
618
+ token:: Ident ( ..) if this. may_recover ( ) && this . is_mistaken_not_ident_negation ( ) => {
619
619
make_it ! ( this, attrs, |this, _| this. recover_not_expr( lo) )
620
620
}
621
621
_ => return this. parse_dot_or_call_expr ( Some ( attrs) ) ,
@@ -718,6 +718,10 @@ impl<'a> Parser<'a> {
718
718
let cast_expr = match self . parse_as_cast_ty ( ) {
719
719
Ok ( rhs) => mk_expr ( self , lhs, rhs) ,
720
720
Err ( type_err) => {
721
+ if !self . may_recover ( ) {
722
+ return Err ( type_err) ;
723
+ }
724
+
721
725
// Rewind to before attempting to parse the type with generics, to recover
722
726
// from situations like `x as usize < y` in which we first tried to parse
723
727
// `usize < y` as a type with generic arguments.
@@ -1197,6 +1201,10 @@ impl<'a> Parser<'a> {
1197
1201
seq : & mut PResult < ' a , P < Expr > > ,
1198
1202
snapshot : Option < ( SnapshotParser < ' a > , ExprKind ) > ,
1199
1203
) -> Option < P < Expr > > {
1204
+ if !self . may_recover ( ) {
1205
+ return None ;
1206
+ }
1207
+
1200
1208
match ( seq. as_mut ( ) , snapshot) {
1201
1209
( Err ( err) , Some ( ( mut snapshot, ExprKind :: Path ( None , path) ) ) ) => {
1202
1210
snapshot. bump ( ) ; // `(`
@@ -1360,7 +1368,7 @@ impl<'a> Parser<'a> {
1360
1368
)
1361
1369
} else if self . check_inline_const ( 0 ) {
1362
1370
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 ( ) {
1364
1372
self . recover_do_catch ( )
1365
1373
} else if self . is_try_block ( ) {
1366
1374
self . expect_keyword ( kw:: Try ) ?;
@@ -1532,6 +1540,7 @@ impl<'a> Parser<'a> {
1532
1540
{
1533
1541
self . parse_block_expr ( label, lo, BlockCheckMode :: Default )
1534
1542
} else if !ate_colon
1543
+ && self . may_recover ( )
1535
1544
&& ( matches ! ( self . token. kind, token:: CloseDelim ( _) | token:: Comma )
1536
1545
|| self . token . is_op ( ) )
1537
1546
{
@@ -1999,6 +2008,10 @@ impl<'a> Parser<'a> {
1999
2008
prev_span : Span ,
2000
2009
open_delim_span : Span ,
2001
2010
) -> PResult < ' a , ( ) > {
2011
+ if !self . may_recover ( ) {
2012
+ return Ok ( ( ) ) ;
2013
+ }
2014
+
2002
2015
if self . token . kind == token:: Comma {
2003
2016
if !self . sess . source_map ( ) . is_multiline ( prev_span. until ( self . token . span ) ) {
2004
2017
return Ok ( ( ) ) ;
@@ -2039,7 +2052,7 @@ impl<'a> Parser<'a> {
2039
2052
lo : Span ,
2040
2053
blk_mode : BlockCheckMode ,
2041
2054
) -> PResult < ' a , P < Expr > > {
2042
- if self . is_array_like_block ( ) {
2055
+ if self . may_recover ( ) && self . is_array_like_block ( ) {
2043
2056
if let Some ( arr) = self . maybe_suggest_brackets_instead_of_braces ( lo) {
2044
2057
return Ok ( arr) ;
2045
2058
}
0 commit comments