From bc95228f1b01619e4faecf4694c1cd1f0938815d Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 7 Dec 2019 01:52:53 +0100 Subject: [PATCH] extract parse_dot_suffix_expr --- src/librustc_parse/parser/expr.rs | 45 +++++++++++-------- .../ui/parser/attr-stmt-expr-attr-bad.stderr | 6 +-- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/src/librustc_parse/parser/expr.rs b/src/librustc_parse/parser/expr.rs index 585ddad5a9e7..f58b885522f6 100644 --- a/src/librustc_parse/parser/expr.rs +++ b/src/librustc_parse/parser/expr.rs @@ -703,20 +703,7 @@ impl<'a> Parser<'a> { // expr.f if self.eat(&token::Dot) { - match self.token.kind { - token::Ident(..) => { - e = self.parse_dot_suffix(e, lo)?; - } - token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) => { - e = self.parse_tuple_field_access_expr(lo, e, symbol, suffix); - } - token::Literal(token::Lit { kind: token::Float, symbol, .. }) => { - if let Some(err) = self.recover_field_access_by_float_lit(lo, &e, symbol) { - err? - } - } - _ => self.error_unexpected_after_dot(), - } + e = self.parse_dot_suffix_expr(lo, e)?; continue; } if self.expr_is_complete(&e) { @@ -731,6 +718,22 @@ impl<'a> Parser<'a> { return Ok(e); } + fn parse_dot_suffix_expr(&mut self, lo: Span, base: P) -> PResult<'a, P> { + match self.token.kind { + token::Ident(..) => self.parse_dot_suffix(base, lo), + token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) => { + Ok(self.parse_tuple_field_access_expr(lo, base, symbol, suffix)) + } + token::Literal(token::Lit { kind: token::Float, symbol, .. }) => { + self.recover_field_access_by_float_lit(lo, base, symbol) + } + _ => { + self.error_unexpected_after_dot(); + Ok(base) + } + } + } + fn error_unexpected_after_dot(&self) { // FIXME Could factor this out into non_fatal_unexpected or something. let actual = self.this_token_to_string(); @@ -740,9 +743,9 @@ impl<'a> Parser<'a> { fn recover_field_access_by_float_lit( &mut self, lo: Span, - base: &P, + base: P, sym: Symbol, - ) -> Option> { + ) -> PResult<'a, P> { self.bump(); let fstr = sym.as_str(); @@ -752,7 +755,13 @@ impl<'a> Parser<'a> { err.span_label(self.prev_span, "unexpected token"); if fstr.chars().all(|x| "0123456789.".contains(x)) { - let float = fstr.parse::().ok()?; + let float = match fstr.parse::() { + Ok(f) => f, + Err(_) => { + err.emit(); + return Ok(base); + } + }; let sugg = pprust::to_string(|s| { s.popen(); s.print_expr(&base); @@ -769,7 +778,7 @@ impl<'a> Parser<'a> { Applicability::MachineApplicable, ); } - Some(Err(err)) + Err(err) } fn parse_tuple_field_access_expr( diff --git a/src/test/ui/parser/attr-stmt-expr-attr-bad.stderr b/src/test/ui/parser/attr-stmt-expr-attr-bad.stderr index 992b491917ee..371d3f575a41 100644 --- a/src/test/ui/parser/attr-stmt-expr-attr-bad.stderr +++ b/src/test/ui/parser/attr-stmt-expr-attr-bad.stderr @@ -393,19 +393,19 @@ LL | #[cfg(FALSE)] fn e() { let _ = x.#![attr]foo(); } | ^ expected one of `.`, `;`, `?`, or an operator error: unexpected token: `#` - --> $DIR/attr-stmt-expr-attr-bad.rs:106:34 + --> $DIR/attr-stmt-expr-attr-bad.rs:107:34 | LL | #[cfg(FALSE)] fn e() { let _ = x.#[attr]foo(); } | ^ error: expected one of `.`, `;`, `?`, or an operator, found `#` - --> $DIR/attr-stmt-expr-attr-bad.rs:106:34 + --> $DIR/attr-stmt-expr-attr-bad.rs:107:34 | LL | #[cfg(FALSE)] fn e() { let _ = x.#[attr]foo(); } | ^ expected one of `.`, `;`, `?`, or an operator error: expected statement after outer attribute - --> $DIR/attr-stmt-expr-attr-bad.rs:110:44 + --> $DIR/attr-stmt-expr-attr-bad.rs:112:44 | LL | #[cfg(FALSE)] fn e() { { fn foo() { #[attr]; } } } | ^