Skip to content

Commit 29e50e8

Browse files
committedOct 28, 2022
Gate some recovery behind a flag
Mainly in `expr.rs`
1 parent 5237c4d commit 29e50e8

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed
 

‎compiler/rustc_parse/src/parser/diagnostics.rs

+12
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,10 @@ impl<'a> Parser<'a> {
769769
segment: &PathSegment,
770770
end: &[&TokenKind],
771771
) -> bool {
772+
if !self.may_recover() {
773+
return false;
774+
}
775+
772776
// This function is intended to be invoked after parsing a path segment where there are two
773777
// cases:
774778
//
@@ -863,6 +867,10 @@ impl<'a> Parser<'a> {
863867
/// Check if a method call with an intended turbofish has been written without surrounding
864868
/// angle brackets.
865869
pub(super) fn check_turbofish_missing_angle_brackets(&mut self, segment: &mut PathSegment) {
870+
if !self.may_recover() {
871+
return;
872+
}
873+
866874
if token::ModSep == self.token.kind && segment.args.is_none() {
867875
let snapshot = self.create_snapshot_for_diagnostic();
868876
self.bump();
@@ -1396,6 +1404,10 @@ impl<'a> Parser<'a> {
13961404
&mut self,
13971405
base: P<T>,
13981406
) -> PResult<'a, P<T>> {
1407+
if !self.may_recover() {
1408+
return Ok(base);
1409+
}
1410+
13991411
// Do not add `::` to expected tokens.
14001412
if self.token == token::ModSep {
14011413
if let Some(ty) = base.to_ty() {

‎compiler/rustc_parse/src/parser/expr.rs

+19-6
Original file line numberDiff line numberDiff line change
@@ -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
}

‎compiler/rustc_parse/src/parser/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ macro_rules! maybe_whole {
104104
macro_rules! maybe_recover_from_interpolated_ty_qpath {
105105
($self: expr, $allow_qpath_recovery: expr) => {
106106
if $allow_qpath_recovery
107+
&& $self.may_recover()
107108
&& $self.look_ahead(1, |t| t == &token::ModSep)
108109
&& let token::Interpolated(nt) = &$self.token.kind
109110
&& let token::NtTy(ty) = &**nt

0 commit comments

Comments
 (0)