Skip to content

Commit ffc623a

Browse files
committed
review comment: move recovery code to its own function
1 parent b82ec36 commit ffc623a

File tree

1 file changed

+19
-2
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+19
-2
lines changed

compiler/rustc_parse/src/parser/expr.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,23 @@ impl<'a> Parser<'a> {
10491049
let mut seq = self.parse_paren_expr_seq().map(|args| {
10501050
self.mk_expr(lo.to(self.prev_token.span), self.mk_call(fun, args), AttrVec::new())
10511051
});
1052+
if let Some(expr) =
1053+
self.maybe_recover_struct_lit_bad_delims(lo, open_paren, &mut seq, snapshot)
1054+
{
1055+
return expr;
1056+
}
1057+
self.recover_seq_parse_error(token::Paren, lo, seq)
1058+
}
1059+
1060+
/// If we encounter a parser state that looks like the user has written a `struct` literal with
1061+
/// parentheses instead of braces, recover the parser state and provide suggestions.
1062+
fn maybe_recover_struct_lit_bad_delims(
1063+
&mut self,
1064+
lo: Span,
1065+
open_paren: Span,
1066+
seq: &mut PResult<'a, P<Expr>>,
1067+
snapshot: Option<(Self, ExprKind)>,
1068+
) -> Option<P<Expr>> {
10521069
match (seq.as_mut(), snapshot) {
10531070
(Err(ref mut err), Some((mut snapshot, ExprKind::Path(None, path)))) => {
10541071
let name = pprust::path_to_string(&path);
@@ -1079,15 +1096,15 @@ impl<'a> Parser<'a> {
10791096
Applicability::MaybeIncorrect,
10801097
)
10811098
.emit();
1082-
return self.mk_expr_err(span);
1099+
return Some(self.mk_expr_err(span));
10831100
}
10841101
Ok(_) => {}
10851102
Err(mut err) => err.emit(),
10861103
}
10871104
}
10881105
_ => {}
10891106
}
1090-
self.recover_seq_parse_error(token::Paren, lo, seq)
1107+
None
10911108
}
10921109

10931110
/// Parse an indexing expression `expr[...]`.

0 commit comments

Comments
 (0)