Extend incorrect float literal recovery to account for suffixes

This commit is contained in:
Esteban Küber 2019-01-20 14:25:53 -08:00
parent e387597a8f
commit 15bad8bbfd
5 changed files with 74 additions and 20 deletions

View file

@ -1990,15 +1990,23 @@ impl<'a> Parser<'a> {
result.unwrap()
}
token::Dot if self.look_ahead(1, |t| match t {
token::Literal(parse::token::Lit::Integer(_) , None) => true,
token::Literal(parse::token::Lit::Integer(_) , _) => true,
_ => false,
}) => { // recover from `let x = .4;`
let lo = self.span;
self.bump();
if let token::Literal(
parse::token::Lit::Integer(val),
None
suffix,
) = self.token {
let suffix = suffix.and_then(|s| {
let s = s.as_str().get();
if ["f32", "f64"].contains(&s) {
Some(s)
} else {
None
}
}).unwrap_or("");
self.bump();
let sp = lo.to(self.prev_span);
let mut err = self.diagnostic()
@ -2006,11 +2014,15 @@ impl<'a> Parser<'a> {
err.span_suggestion_with_applicability(
sp,
"must have an integer part",
format!("0.{}", val),
format!("0.{}{}", val, suffix),
Applicability::MachineApplicable,
);
err.emit();
return Ok(ast::LitKind::Float(val, ast::FloatTy::F32));
return Ok(match suffix {
"f32" => ast::LitKind::Float(val, ast::FloatTy::F32),
"f64" => ast::LitKind::Float(val, ast::FloatTy::F64),
_ => ast::LitKind::FloatUnsuffixed(val),
});
} else {
unreachable!();
};