Recover with suggestion from writing .42 instead of 0.42

This commit is contained in:
Esteban Küber 2019-01-20 01:49:04 -08:00
parent b1f169fe7a
commit acbda76f23
3 changed files with 47 additions and 12 deletions

View file

@ -1989,6 +1989,32 @@ impl<'a> Parser<'a> {
result.unwrap()
}
token::Dot if self.look_ahead(1, |t| match t {
token::Literal(parse::token::Lit::Integer(_) , None) => true,
_ => false,
}) => { // recover from `let x = .4;`
let lo = self.span;
self.bump();
if let token::Literal(
parse::token::Lit::Integer(val),
None
) = self.token {
self.bump();
let sp = lo.to(self.prev_span);
let mut err = self.diagnostic()
.struct_span_err(sp, "numeric float literals must have a significant");
err.span_suggestion_with_applicability(
sp,
"numeric float literals must have a significant",
format!("0.{}", val),
Applicability::MachineApplicable,
);
err.emit();
return Ok(ast::LitKind::Float(val, ast::FloatTy::F32));
} else {
unreachable!();
};
}
_ => { return self.unexpected_last(&self.token); }
};

View file

@ -2,8 +2,9 @@ struct Foo { bar: f64, baz: i64, bat: i64 }
fn main() {
let _ = Foo { bar: .5, baz: 42 };
//~^ ERROR expected expression
//~^ ERROR numeric float literals must have a significant
//~| ERROR missing field `bat` in initializer of `Foo`
//~| ERROR mismatched types
let bar = 1.5f32;
let _ = Foo { bar.into(), bat: -1, . };
//~^ ERROR expected one of

View file

@ -1,13 +1,11 @@
error: expected expression, found `.`
error: numeric float literals must have a significant
--> $DIR/issue-52496.rs:4:24
|
LL | let _ = Foo { bar: .5, baz: 42 };
| --- ^ expected expression
| |
| while parsing this struct
| ^^ help: numeric float literals must have a significant: `0.5`
error: expected one of `,` or `}`, found `.`
--> $DIR/issue-52496.rs:8:22
--> $DIR/issue-52496.rs:9:22
|
LL | let _ = Foo { bar.into(), bat: -1, . };
| --- ^ expected one of `,` or `}` here
@ -15,13 +13,23 @@ LL | let _ = Foo { bar.into(), bat: -1, . };
| while parsing this struct
error: expected identifier, found `.`
--> $DIR/issue-52496.rs:8:40
--> $DIR/issue-52496.rs:9:40
|
LL | let _ = Foo { bar.into(), bat: -1, . };
| --- ^ expected identifier
| |
| while parsing this struct
error[E0308]: mismatched types
--> $DIR/issue-52496.rs:4:24
|
LL | let _ = Foo { bar: .5, baz: 42 };
| ^^ expected f64, found f32
help: change the type of the numeric literal from `f32` to `f64`
|
LL | let _ = Foo { bar: .5f64, baz: 42 };
| ^^^^^
error[E0063]: missing field `bat` in initializer of `Foo`
--> $DIR/issue-52496.rs:4:13
|
@ -29,22 +37,22 @@ LL | let _ = Foo { bar: .5, baz: 42 };
| ^^^ missing `bat`
error[E0308]: mismatched types
--> $DIR/issue-52496.rs:8:19
--> $DIR/issue-52496.rs:9:19
|
LL | let _ = Foo { bar.into(), bat: -1, . };
| ^^^ expected f64, found f32
help: you can cast an `f32` to `f64` in a lossless way
|
LL | let _ = Foo { bar: bar.into().into(), bat: -1, . };
| ^^^^^^^^^^^^^^^
LL | let _ = Foo { bar.into().into(), bat: -1, . };
| ^^^^^^^^^^
error[E0063]: missing field `baz` in initializer of `Foo`
--> $DIR/issue-52496.rs:8:13
--> $DIR/issue-52496.rs:9:13
|
LL | let _ = Foo { bar.into(), bat: -1, . };
| ^^^ missing `baz`
error: aborting due to 6 previous errors
error: aborting due to 7 previous errors
Some errors occurred: E0063, E0308.
For more information about an error, try `rustc --explain E0063`.