about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2019-01-20 01:49:04 -0800
committerEsteban Küber <esteban@kuber.com.ar>2019-01-20 01:49:04 -0800
commitacbda76f23b8945fd8f45332352269044ecbf2ca (patch)
tree03dff9f95bbe01a8be8c6768deba719e04568902 /src
parentb1f169fe7a19cf10f70ee2aa2513276185c70e9b (diff)
downloadrust-acbda76f23b8945fd8f45332352269044ecbf2ca.tar.gz
rust-acbda76f23b8945fd8f45332352269044ecbf2ca.zip
Recover with suggestion from writing `.42` instead of `0.42`
Diffstat (limited to 'src')
-rw-r--r--src/libsyntax/parse/parser.rs26
-rw-r--r--src/test/ui/issues/issue-52496.rs3
-rw-r--r--src/test/ui/issues/issue-52496.stderr30
3 files changed, 47 insertions, 12 deletions
diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs
index 9b20937cf93..43a263b8a6b 100644
--- a/src/libsyntax/parse/parser.rs
+++ b/src/libsyntax/parse/parser.rs
@@ -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); }
         };
 
diff --git a/src/test/ui/issues/issue-52496.rs b/src/test/ui/issues/issue-52496.rs
index d2636b7ecb3..2e790792675 100644
--- a/src/test/ui/issues/issue-52496.rs
+++ b/src/test/ui/issues/issue-52496.rs
@@ -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
diff --git a/src/test/ui/issues/issue-52496.stderr b/src/test/ui/issues/issue-52496.stderr
index c98de6ffbed..3c805631660 100644
--- a/src/test/ui/issues/issue-52496.stderr
+++ b/src/test/ui/issues/issue-52496.stderr
@@ -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`.