Skip to content

Commit acbda76

Browse files
committed
Recover with suggestion from writing .42 instead of 0.42
1 parent b1f169f commit acbda76

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,32 @@ impl<'a> Parser<'a> {
19891989

19901990
result.unwrap()
19911991
}
1992+
token::Dot if self.look_ahead(1, |t| match t {
1993+
token::Literal(parse::token::Lit::Integer(_) , None) => true,
1994+
_ => false,
1995+
}) => { // recover from `let x = .4;`
1996+
let lo = self.span;
1997+
self.bump();
1998+
if let token::Literal(
1999+
parse::token::Lit::Integer(val),
2000+
None
2001+
) = self.token {
2002+
self.bump();
2003+
let sp = lo.to(self.prev_span);
2004+
let mut err = self.diagnostic()
2005+
.struct_span_err(sp, "numeric float literals must have a significant");
2006+
err.span_suggestion_with_applicability(
2007+
sp,
2008+
"numeric float literals must have a significant",
2009+
format!("0.{}", val),
2010+
Applicability::MachineApplicable,
2011+
);
2012+
err.emit();
2013+
return Ok(ast::LitKind::Float(val, ast::FloatTy::F32));
2014+
} else {
2015+
unreachable!();
2016+
};
2017+
}
19922018
_ => { return self.unexpected_last(&self.token); }
19932019
};
19942020

src/test/ui/issues/issue-52496.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ struct Foo { bar: f64, baz: i64, bat: i64 }
22

33
fn main() {
44
let _ = Foo { bar: .5, baz: 42 };
5-
//~^ ERROR expected expression
5+
//~^ ERROR numeric float literals must have a significant
66
//~| ERROR missing field `bat` in initializer of `Foo`
7+
//~| ERROR mismatched types
78
let bar = 1.5f32;
89
let _ = Foo { bar.into(), bat: -1, . };
910
//~^ ERROR expected one of

src/test/ui/issues/issue-52496.stderr

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,58 @@
1-
error: expected expression, found `.`
1+
error: numeric float literals must have a significant
22
--> $DIR/issue-52496.rs:4:24
33
|
44
LL | let _ = Foo { bar: .5, baz: 42 };
5-
| --- ^ expected expression
6-
| |
7-
| while parsing this struct
5+
| ^^ help: numeric float literals must have a significant: `0.5`
86

97
error: expected one of `,` or `}`, found `.`
10-
--> $DIR/issue-52496.rs:8:22
8+
--> $DIR/issue-52496.rs:9:22
119
|
1210
LL | let _ = Foo { bar.into(), bat: -1, . };
1311
| --- ^ expected one of `,` or `}` here
1412
| |
1513
| while parsing this struct
1614

1715
error: expected identifier, found `.`
18-
--> $DIR/issue-52496.rs:8:40
16+
--> $DIR/issue-52496.rs:9:40
1917
|
2018
LL | let _ = Foo { bar.into(), bat: -1, . };
2119
| --- ^ expected identifier
2220
| |
2321
| while parsing this struct
2422

23+
error[E0308]: mismatched types
24+
--> $DIR/issue-52496.rs:4:24
25+
|
26+
LL | let _ = Foo { bar: .5, baz: 42 };
27+
| ^^ expected f64, found f32
28+
help: change the type of the numeric literal from `f32` to `f64`
29+
|
30+
LL | let _ = Foo { bar: .5f64, baz: 42 };
31+
| ^^^^^
32+
2533
error[E0063]: missing field `bat` in initializer of `Foo`
2634
--> $DIR/issue-52496.rs:4:13
2735
|
2836
LL | let _ = Foo { bar: .5, baz: 42 };
2937
| ^^^ missing `bat`
3038

3139
error[E0308]: mismatched types
32-
--> $DIR/issue-52496.rs:8:19
40+
--> $DIR/issue-52496.rs:9:19
3341
|
3442
LL | let _ = Foo { bar.into(), bat: -1, . };
3543
| ^^^ expected f64, found f32
3644
help: you can cast an `f32` to `f64` in a lossless way
3745
|
38-
LL | let _ = Foo { bar: bar.into().into(), bat: -1, . };
39-
| ^^^^^^^^^^^^^^^
46+
LL | let _ = Foo { bar.into().into(), bat: -1, . };
47+
| ^^^^^^^^^^
4048

4149
error[E0063]: missing field `baz` in initializer of `Foo`
42-
--> $DIR/issue-52496.rs:8:13
50+
--> $DIR/issue-52496.rs:9:13
4351
|
4452
LL | let _ = Foo { bar.into(), bat: -1, . };
4553
| ^^^ missing `baz`
4654

47-
error: aborting due to 6 previous errors
55+
error: aborting due to 7 previous errors
4856

4957
Some errors occurred: E0063, E0308.
5058
For more information about an error, try `rustc --explain E0063`.

0 commit comments

Comments
 (0)