Recover with suggestion from writing .42
instead of 0.42
This commit is contained in:
parent
b1f169fe7a
commit
acbda76f23
3 changed files with 47 additions and 12 deletions
|
@ -1989,6 +1989,32 @@ impl<'a> Parser<'a> {
|
||||||
|
|
||||||
result.unwrap()
|
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); }
|
_ => { return self.unexpected_last(&self.token); }
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -2,8 +2,9 @@ struct Foo { bar: f64, baz: i64, bat: i64 }
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let _ = Foo { bar: .5, baz: 42 };
|
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 missing field `bat` in initializer of `Foo`
|
||||||
|
//~| ERROR mismatched types
|
||||||
let bar = 1.5f32;
|
let bar = 1.5f32;
|
||||||
let _ = Foo { bar.into(), bat: -1, . };
|
let _ = Foo { bar.into(), bat: -1, . };
|
||||||
//~^ ERROR expected one of
|
//~^ ERROR expected one of
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
error: expected expression, found `.`
|
error: numeric float literals must have a significant
|
||||||
--> $DIR/issue-52496.rs:4:24
|
--> $DIR/issue-52496.rs:4:24
|
||||||
|
|
|
|
||||||
LL | let _ = Foo { bar: .5, baz: 42 };
|
LL | let _ = Foo { bar: .5, baz: 42 };
|
||||||
| --- ^ expected expression
|
| ^^ help: numeric float literals must have a significant: `0.5`
|
||||||
| |
|
|
||||||
| while parsing this struct
|
|
||||||
|
|
||||||
error: expected one of `,` or `}`, found `.`
|
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, . };
|
LL | let _ = Foo { bar.into(), bat: -1, . };
|
||||||
| --- ^ expected one of `,` or `}` here
|
| --- ^ expected one of `,` or `}` here
|
||||||
|
@ -15,13 +13,23 @@ LL | let _ = Foo { bar.into(), bat: -1, . };
|
||||||
| while parsing this struct
|
| while parsing this struct
|
||||||
|
|
||||||
error: expected identifier, found `.`
|
error: expected identifier, found `.`
|
||||||
--> $DIR/issue-52496.rs:8:40
|
--> $DIR/issue-52496.rs:9:40
|
||||||
|
|
|
|
||||||
LL | let _ = Foo { bar.into(), bat: -1, . };
|
LL | let _ = Foo { bar.into(), bat: -1, . };
|
||||||
| --- ^ expected identifier
|
| --- ^ expected identifier
|
||||||
| |
|
| |
|
||||||
| while parsing this struct
|
| 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`
|
error[E0063]: missing field `bat` in initializer of `Foo`
|
||||||
--> $DIR/issue-52496.rs:4:13
|
--> $DIR/issue-52496.rs:4:13
|
||||||
|
|
|
|
||||||
|
@ -29,22 +37,22 @@ LL | let _ = Foo { bar: .5, baz: 42 };
|
||||||
| ^^^ missing `bat`
|
| ^^^ missing `bat`
|
||||||
|
|
||||||
error[E0308]: mismatched types
|
error[E0308]: mismatched types
|
||||||
--> $DIR/issue-52496.rs:8:19
|
--> $DIR/issue-52496.rs:9:19
|
||||||
|
|
|
|
||||||
LL | let _ = Foo { bar.into(), bat: -1, . };
|
LL | let _ = Foo { bar.into(), bat: -1, . };
|
||||||
| ^^^ expected f64, found f32
|
| ^^^ expected f64, found f32
|
||||||
help: you can cast an `f32` to `f64` in a lossless way
|
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`
|
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, . };
|
LL | let _ = Foo { bar.into(), bat: -1, . };
|
||||||
| ^^^ missing `baz`
|
| ^^^ missing `baz`
|
||||||
|
|
||||||
error: aborting due to 6 previous errors
|
error: aborting due to 7 previous errors
|
||||||
|
|
||||||
Some errors occurred: E0063, E0308.
|
Some errors occurred: E0063, E0308.
|
||||||
For more information about an error, try `rustc --explain E0063`.
|
For more information about an error, try `rustc --explain E0063`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue