Skip to content

Commit bc95228

Browse files
committed
extract parse_dot_suffix_expr
1 parent 9c6bbf1 commit bc95228

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

src/librustc_parse/parser/expr.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -703,20 +703,7 @@ impl<'a> Parser<'a> {
703703

704704
// expr.f
705705
if self.eat(&token::Dot) {
706-
match self.token.kind {
707-
token::Ident(..) => {
708-
e = self.parse_dot_suffix(e, lo)?;
709-
}
710-
token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) => {
711-
e = self.parse_tuple_field_access_expr(lo, e, symbol, suffix);
712-
}
713-
token::Literal(token::Lit { kind: token::Float, symbol, .. }) => {
714-
if let Some(err) = self.recover_field_access_by_float_lit(lo, &e, symbol) {
715-
err?
716-
}
717-
}
718-
_ => self.error_unexpected_after_dot(),
719-
}
706+
e = self.parse_dot_suffix_expr(lo, e)?;
720707
continue;
721708
}
722709
if self.expr_is_complete(&e) {
@@ -731,6 +718,22 @@ impl<'a> Parser<'a> {
731718
return Ok(e);
732719
}
733720

721+
fn parse_dot_suffix_expr(&mut self, lo: Span, base: P<Expr>) -> PResult<'a, P<Expr>> {
722+
match self.token.kind {
723+
token::Ident(..) => self.parse_dot_suffix(base, lo),
724+
token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) => {
725+
Ok(self.parse_tuple_field_access_expr(lo, base, symbol, suffix))
726+
}
727+
token::Literal(token::Lit { kind: token::Float, symbol, .. }) => {
728+
self.recover_field_access_by_float_lit(lo, base, symbol)
729+
}
730+
_ => {
731+
self.error_unexpected_after_dot();
732+
Ok(base)
733+
}
734+
}
735+
}
736+
734737
fn error_unexpected_after_dot(&self) {
735738
// FIXME Could factor this out into non_fatal_unexpected or something.
736739
let actual = self.this_token_to_string();
@@ -740,9 +743,9 @@ impl<'a> Parser<'a> {
740743
fn recover_field_access_by_float_lit(
741744
&mut self,
742745
lo: Span,
743-
base: &P<Expr>,
746+
base: P<Expr>,
744747
sym: Symbol,
745-
) -> Option<PResult<'a, ()>> {
748+
) -> PResult<'a, P<Expr>> {
746749
self.bump();
747750

748751
let fstr = sym.as_str();
@@ -752,7 +755,13 @@ impl<'a> Parser<'a> {
752755
err.span_label(self.prev_span, "unexpected token");
753756

754757
if fstr.chars().all(|x| "0123456789.".contains(x)) {
755-
let float = fstr.parse::<f64>().ok()?;
758+
let float = match fstr.parse::<f64>() {
759+
Ok(f) => f,
760+
Err(_) => {
761+
err.emit();
762+
return Ok(base);
763+
}
764+
};
756765
let sugg = pprust::to_string(|s| {
757766
s.popen();
758767
s.print_expr(&base);
@@ -769,7 +778,7 @@ impl<'a> Parser<'a> {
769778
Applicability::MachineApplicable,
770779
);
771780
}
772-
Some(Err(err))
781+
Err(err)
773782
}
774783

775784
fn parse_tuple_field_access_expr(

src/test/ui/parser/attr-stmt-expr-attr-bad.stderr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -393,19 +393,19 @@ LL | #[cfg(FALSE)] fn e() { let _ = x.#![attr]foo(); }
393393
| ^ expected one of `.`, `;`, `?`, or an operator
394394

395395
error: unexpected token: `#`
396-
--> $DIR/attr-stmt-expr-attr-bad.rs:106:34
396+
--> $DIR/attr-stmt-expr-attr-bad.rs:107:34
397397
|
398398
LL | #[cfg(FALSE)] fn e() { let _ = x.#[attr]foo(); }
399399
| ^
400400

401401
error: expected one of `.`, `;`, `?`, or an operator, found `#`
402-
--> $DIR/attr-stmt-expr-attr-bad.rs:106:34
402+
--> $DIR/attr-stmt-expr-attr-bad.rs:107:34
403403
|
404404
LL | #[cfg(FALSE)] fn e() { let _ = x.#[attr]foo(); }
405405
| ^ expected one of `.`, `;`, `?`, or an operator
406406

407407
error: expected statement after outer attribute
408-
--> $DIR/attr-stmt-expr-attr-bad.rs:110:44
408+
--> $DIR/attr-stmt-expr-attr-bad.rs:112:44
409409
|
410410
LL | #[cfg(FALSE)] fn e() { { fn foo() { #[attr]; } } }
411411
| ^

0 commit comments

Comments
 (0)