From ffc623ab93340f0c13fea3d518a00f6e0e49a7ec Mon Sep 17 00:00:00 2001 From: Esteban Kuber Date: Tue, 14 Sep 2021 18:16:33 +0000 Subject: [PATCH] review comment: move recovery code to its own function --- compiler/rustc_parse/src/parser/expr.rs | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index c802d40fb8595..cfd3fe93d32eb 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -1049,6 +1049,23 @@ impl<'a> Parser<'a> { let mut seq = self.parse_paren_expr_seq().map(|args| { self.mk_expr(lo.to(self.prev_token.span), self.mk_call(fun, args), AttrVec::new()) }); + if let Some(expr) = + self.maybe_recover_struct_lit_bad_delims(lo, open_paren, &mut seq, snapshot) + { + return expr; + } + self.recover_seq_parse_error(token::Paren, lo, seq) + } + + /// If we encounter a parser state that looks like the user has written a `struct` literal with + /// parentheses instead of braces, recover the parser state and provide suggestions. + fn maybe_recover_struct_lit_bad_delims( + &mut self, + lo: Span, + open_paren: Span, + seq: &mut PResult<'a, P>, + snapshot: Option<(Self, ExprKind)>, + ) -> Option> { match (seq.as_mut(), snapshot) { (Err(ref mut err), Some((mut snapshot, ExprKind::Path(None, path)))) => { let name = pprust::path_to_string(&path); @@ -1079,7 +1096,7 @@ impl<'a> Parser<'a> { Applicability::MaybeIncorrect, ) .emit(); - return self.mk_expr_err(span); + return Some(self.mk_expr_err(span)); } Ok(_) => {} Err(mut err) => err.emit(), @@ -1087,7 +1104,7 @@ impl<'a> Parser<'a> { } _ => {} } - self.recover_seq_parse_error(token::Paren, lo, seq) + None } /// Parse an indexing expression `expr[...]`.