Skip to content

Commit ff5762b

Browse files
committed
extract recover_field_access_by_float_lit
1 parent a15d0cd commit ff5762b

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

src/librustc_parse/parser/expr.rs

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -711,33 +711,9 @@ impl<'a> Parser<'a> {
711711
e = self.parse_tuple_field_access_expr(lo, e, symbol, suffix);
712712
}
713713
token::Literal(token::Lit { kind: token::Float, symbol, .. }) => {
714-
self.bump();
715-
let fstr = symbol.as_str();
716-
let msg = format!("unexpected token: `{}`", symbol);
717-
let mut err = self.diagnostic().struct_span_err(self.prev_span, &msg);
718-
err.span_label(self.prev_span, "unexpected token");
719-
if fstr.chars().all(|x| "0123456789.".contains(x)) {
720-
let float = match fstr.parse::<f64>().ok() {
721-
Some(f) => f,
722-
None => continue,
723-
};
724-
let sugg = pprust::to_string(|s| {
725-
s.popen();
726-
s.print_expr(&e);
727-
s.s.word(".");
728-
s.print_usize(float.trunc() as usize);
729-
s.pclose();
730-
s.s.word(".");
731-
s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
732-
});
733-
err.span_suggestion(
734-
lo.to(self.prev_span),
735-
"try parenthesizing the first index",
736-
sugg,
737-
Applicability::MachineApplicable,
738-
);
714+
if let Some(err) = self.recover_field_access_by_float_lit(lo, &e, symbol) {
715+
err?
739716
}
740-
return Err(err);
741717
}
742718
_ => {
743719
// FIXME Could factor this out into non_fatal_unexpected or something.
@@ -759,6 +735,41 @@ impl<'a> Parser<'a> {
759735
return Ok(e);
760736
}
761737

738+
fn recover_field_access_by_float_lit(
739+
&mut self,
740+
lo: Span,
741+
base: &P<Expr>,
742+
sym: Symbol,
743+
) -> Option<PResult<'a, ()>> {
744+
self.bump();
745+
746+
let fstr = sym.as_str();
747+
let msg = format!("unexpected token: `{}`", sym);
748+
749+
let mut err = self.struct_span_err(self.prev_span, &msg);
750+
err.span_label(self.prev_span, "unexpected token");
751+
752+
if fstr.chars().all(|x| "0123456789.".contains(x)) {
753+
let float = fstr.parse::<f64>().ok()?;
754+
let sugg = pprust::to_string(|s| {
755+
s.popen();
756+
s.print_expr(&base);
757+
s.s.word(".");
758+
s.print_usize(float.trunc() as usize);
759+
s.pclose();
760+
s.s.word(".");
761+
s.s.word(fstr.splitn(2, ".").last().unwrap().to_string())
762+
});
763+
err.span_suggestion(
764+
lo.to(self.prev_span),
765+
"try parenthesizing the first index",
766+
sugg,
767+
Applicability::MachineApplicable,
768+
);
769+
}
770+
Some(Err(err))
771+
}
772+
762773
fn parse_tuple_field_access_expr(
763774
&mut self,
764775
lo: Span,

0 commit comments

Comments
 (0)