diff options
| author | Esteban Kuber <esteban@kuber.com.ar> | 2021-09-14 18:16:33 +0000 |
|---|---|---|
| committer | Esteban Kuber <esteban@kuber.com.ar> | 2021-09-14 18:16:33 +0000 |
| commit | ffc623ab93340f0c13fea3d518a00f6e0e49a7ec (patch) | |
| tree | 95bf9a8ff03613a4a8c479b3d7c5c2b8124d294e /compiler/rustc_parse/src/parser | |
| parent | b82ec362ca14a6c369b1999bdd40150b60567c96 (diff) | |
| download | rust-ffc623ab93340f0c13fea3d518a00f6e0e49a7ec.tar.gz rust-ffc623ab93340f0c13fea3d518a00f6e0e49a7ec.zip | |
review comment: move recovery code to its own function
Diffstat (limited to 'compiler/rustc_parse/src/parser')
| -rw-r--r-- | compiler/rustc_parse/src/parser/expr.rs | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index c802d40fb85..cfd3fe93d32 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<Expr>>, + snapshot: Option<(Self, ExprKind)>, + ) -> Option<P<Expr>> { 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[...]`. |
