From b1f169fe7a19cf10f70ee2aa2513276185c70e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 20 Jan 2019 00:37:06 -0800 Subject: [PATCH 1/6] Recover from parse errors in struct literal fields Attempt to recover from parse errors while parsing a struct's literal fields by skipping tokens until a comma or the closing brace is found. This allows errors in other fields to be reported. --- src/libsyntax/parse/parser.rs | 47 ++++++++++++++--- src/test/ui/issues/issue-52496.rs | 13 +++++ src/test/ui/issues/issue-52496.stderr | 50 +++++++++++++++++++ .../ui/parser/removed-syntax-with-1.stderr | 4 +- .../ui/parser/removed-syntax-with-2.stderr | 4 +- .../parser/struct-field-numeric-shorthand.rs | 7 ++- .../struct-field-numeric-shorthand.stderr | 22 ++++++-- 7 files changed, 132 insertions(+), 15 deletions(-) create mode 100644 src/test/ui/issues/issue-52496.rs create mode 100644 src/test/ui/issues/issue-52496.stderr diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 7e15b23127655..9b20937cf933b 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -100,6 +100,7 @@ pub enum PathStyle { enum SemiColonMode { Break, Ignore, + Comma, } #[derive(Clone, Copy, PartialEq, Debug)] @@ -2656,18 +2657,37 @@ impl<'a> Parser<'a> { break; } + let mut recovery_field = None; + if let token::Ident(ident, _) = self.token { + if !self.token.is_reserved_ident() { + let mut ident = ident.clone(); + ident.span = self.span; + recovery_field = Some(ast::Field { + ident, + span: self.span, + expr: self.mk_expr(self.span, ExprKind::Err, ThinVec::new()), + is_shorthand: true, + attrs: ThinVec::new(), + }); + } + } match self.parse_field() { Ok(f) => fields.push(f), Err(mut e) => { e.span_label(struct_sp, "while parsing this struct"); e.emit(); + if let Some(f) = recovery_field { + fields.push(f); + } // If the next token is a comma, then try to parse // what comes next as additional fields, rather than // bailing out until next `}`. if self.token != token::Comma { - self.recover_stmt(); - break; + self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore); + if self.token != token::Comma { + break; + } } } } @@ -2676,9 +2696,10 @@ impl<'a> Parser<'a> { &[token::CloseDelim(token::Brace)]) { Ok(()) => {} Err(mut e) => { + e.span_label(struct_sp, "while parsing this struct"); e.emit(); - self.recover_stmt(); - break; + self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore); + self.eat(&token::Comma); } } } @@ -4538,13 +4559,13 @@ impl<'a> Parser<'a> { token::CloseDelim(token::DelimToken::Brace) => { if brace_depth == 0 { debug!("recover_stmt_ return - close delim {:?}", self.token); - return; + break; } brace_depth -= 1; self.bump(); if in_block && bracket_depth == 0 && brace_depth == 0 { debug!("recover_stmt_ return - block end {:?}", self.token); - return; + break; } } token::CloseDelim(token::DelimToken::Bracket) => { @@ -4556,7 +4577,7 @@ impl<'a> Parser<'a> { } token::Eof => { debug!("recover_stmt_ return - Eof"); - return; + break; } token::Semi => { self.bump(); @@ -4564,7 +4585,17 @@ impl<'a> Parser<'a> { brace_depth == 0 && bracket_depth == 0 { debug!("recover_stmt_ return - Semi"); - return; + break; + } + } + token::Comma => { + if break_on_semi == SemiColonMode::Comma && + brace_depth == 0 && + bracket_depth == 0 { + debug!("recover_stmt_ return - Semi"); + break; + } else { + self.bump(); } } _ => { diff --git a/src/test/ui/issues/issue-52496.rs b/src/test/ui/issues/issue-52496.rs new file mode 100644 index 0000000000000..d2636b7ecb3f8 --- /dev/null +++ b/src/test/ui/issues/issue-52496.rs @@ -0,0 +1,13 @@ +struct Foo { bar: f64, baz: i64, bat: i64 } + +fn main() { + let _ = Foo { bar: .5, baz: 42 }; + //~^ ERROR expected expression + //~| ERROR missing field `bat` in initializer of `Foo` + let bar = 1.5f32; + let _ = Foo { bar.into(), bat: -1, . }; + //~^ ERROR expected one of + //~| ERROR mismatched types + //~| ERROR missing field `baz` in initializer of `Foo` + //~| ERROR expected identifier, found `.` +} diff --git a/src/test/ui/issues/issue-52496.stderr b/src/test/ui/issues/issue-52496.stderr new file mode 100644 index 0000000000000..c98de6ffbed40 --- /dev/null +++ b/src/test/ui/issues/issue-52496.stderr @@ -0,0 +1,50 @@ +error: expected expression, found `.` + --> $DIR/issue-52496.rs:4:24 + | +LL | let _ = Foo { bar: .5, baz: 42 }; + | --- ^ expected expression + | | + | while parsing this struct + +error: expected one of `,` or `}`, found `.` + --> $DIR/issue-52496.rs:8:22 + | +LL | let _ = Foo { bar.into(), bat: -1, . }; + | --- ^ expected one of `,` or `}` here + | | + | while parsing this struct + +error: expected identifier, found `.` + --> $DIR/issue-52496.rs:8:40 + | +LL | let _ = Foo { bar.into(), bat: -1, . }; + | --- ^ expected identifier + | | + | while parsing this struct + +error[E0063]: missing field `bat` in initializer of `Foo` + --> $DIR/issue-52496.rs:4:13 + | +LL | let _ = Foo { bar: .5, baz: 42 }; + | ^^^ missing `bat` + +error[E0308]: mismatched types + --> $DIR/issue-52496.rs:8: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, . }; + | ^^^^^^^^^^^^^^^ + +error[E0063]: missing field `baz` in initializer of `Foo` + --> $DIR/issue-52496.rs:8:13 + | +LL | let _ = Foo { bar.into(), bat: -1, . }; + | ^^^ missing `baz` + +error: aborting due to 6 previous errors + +Some errors occurred: E0063, E0308. +For more information about an error, try `rustc --explain E0063`. diff --git a/src/test/ui/parser/removed-syntax-with-1.stderr b/src/test/ui/parser/removed-syntax-with-1.stderr index 77ed4fcea517c..b5956ad339db8 100644 --- a/src/test/ui/parser/removed-syntax-with-1.stderr +++ b/src/test/ui/parser/removed-syntax-with-1.stderr @@ -2,7 +2,9 @@ error: expected one of `,`, `.`, `?`, `}`, or an operator, found `with` --> $DIR/removed-syntax-with-1.rs:8:25 | LL | let b = S { foo: () with a }; - | ^^^^ expected one of `,`, `.`, `?`, `}`, or an operator here + | - ^^^^ expected one of `,`, `.`, `?`, `}`, or an operator here + | | + | while parsing this struct error[E0063]: missing field `bar` in initializer of `main::S` --> $DIR/removed-syntax-with-1.rs:8:13 diff --git a/src/test/ui/parser/removed-syntax-with-2.stderr b/src/test/ui/parser/removed-syntax-with-2.stderr index 5642d2f45ffce..ee7560017a675 100644 --- a/src/test/ui/parser/removed-syntax-with-2.stderr +++ b/src/test/ui/parser/removed-syntax-with-2.stderr @@ -2,7 +2,9 @@ error: expected one of `,` or `}`, found `a` --> $DIR/removed-syntax-with-2.rs:8:31 | LL | let b = S { foo: (), with a }; - | ^ expected one of `,` or `}` here + | - ^ expected one of `,` or `}` here + | | + | while parsing this struct error[E0425]: cannot find value `with` in this scope --> $DIR/removed-syntax-with-2.rs:8:26 diff --git a/src/test/ui/parser/struct-field-numeric-shorthand.rs b/src/test/ui/parser/struct-field-numeric-shorthand.rs index 914588f51e1e3..58c40b3d96a49 100644 --- a/src/test/ui/parser/struct-field-numeric-shorthand.rs +++ b/src/test/ui/parser/struct-field-numeric-shorthand.rs @@ -1,6 +1,9 @@ struct Rgb(u8, u8, u8); fn main() { - let _ = Rgb { 0, 1, 2 }; //~ ERROR expected identifier, found `0` - //~| ERROR missing fields `0`, `1`, `2` in initializer of `Rgb` + let _ = Rgb { 0, 1, 2 }; + //~^ ERROR expected identifier, found `0` + //~| ERROR expected identifier, found `1` + //~| ERROR expected identifier, found `2` + //~| ERROR missing fields `0`, `1`, `2` in initializer of `Rgb` } diff --git a/src/test/ui/parser/struct-field-numeric-shorthand.stderr b/src/test/ui/parser/struct-field-numeric-shorthand.stderr index f5dc226934ec6..cfb1f82014754 100644 --- a/src/test/ui/parser/struct-field-numeric-shorthand.stderr +++ b/src/test/ui/parser/struct-field-numeric-shorthand.stderr @@ -1,17 +1,33 @@ error: expected identifier, found `0` --> $DIR/struct-field-numeric-shorthand.rs:4:19 | -LL | let _ = Rgb { 0, 1, 2 }; //~ ERROR expected identifier, found `0` +LL | let _ = Rgb { 0, 1, 2 }; | --- ^ expected identifier | | | while parsing this struct +error: expected identifier, found `1` + --> $DIR/struct-field-numeric-shorthand.rs:4:22 + | +LL | let _ = Rgb { 0, 1, 2 }; + | --- ^ expected identifier + | | + | while parsing this struct + +error: expected identifier, found `2` + --> $DIR/struct-field-numeric-shorthand.rs:4:25 + | +LL | let _ = Rgb { 0, 1, 2 }; + | --- ^ expected identifier + | | + | while parsing this struct + error[E0063]: missing fields `0`, `1`, `2` in initializer of `Rgb` --> $DIR/struct-field-numeric-shorthand.rs:4:13 | -LL | let _ = Rgb { 0, 1, 2 }; //~ ERROR expected identifier, found `0` +LL | let _ = Rgb { 0, 1, 2 }; | ^^^ missing `0`, `1`, `2` -error: aborting due to 2 previous errors +error: aborting due to 4 previous errors For more information about this error, try `rustc --explain E0063`. From acbda76f23b8945fd8f45332352269044ecbf2ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 20 Jan 2019 01:49:04 -0800 Subject: [PATCH 2/6] Recover with suggestion from writing `.42` instead of `0.42` --- src/libsyntax/parse/parser.rs | 26 +++++++++++++++++++++++ src/test/ui/issues/issue-52496.rs | 3 ++- src/test/ui/issues/issue-52496.stderr | 30 +++++++++++++++++---------- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 9b20937cf933b..43a263b8a6b0e 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 d2636b7ecb3f8..2e79079267540 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 c98de6ffbed40..3c8056316603b 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`. From e387597a8f789ab6e37e6ce1bf67c8c45d4827c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 20 Jan 2019 13:59:35 -0800 Subject: [PATCH 3/6] Reword message for incorrect float literal --- src/libsyntax/parse/parser.rs | 4 ++-- src/test/ui/issues/issue-52496.rs | 2 +- src/test/ui/issues/issue-52496.stderr | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 43a263b8a6b0e..fe6fa5e97d721 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2002,10 +2002,10 @@ impl<'a> Parser<'a> { 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"); + .struct_span_err(sp, "float literals must have an integer part"); err.span_suggestion_with_applicability( sp, - "numeric float literals must have a significant", + "must have an integer part", format!("0.{}", val), Applicability::MachineApplicable, ); diff --git a/src/test/ui/issues/issue-52496.rs b/src/test/ui/issues/issue-52496.rs index 2e79079267540..e734e9bc51332 100644 --- a/src/test/ui/issues/issue-52496.rs +++ b/src/test/ui/issues/issue-52496.rs @@ -2,7 +2,7 @@ struct Foo { bar: f64, baz: i64, bat: i64 } fn main() { let _ = Foo { bar: .5, baz: 42 }; - //~^ ERROR numeric float literals must have a significant + //~^ ERROR float literals must have an integer part //~| ERROR missing field `bat` in initializer of `Foo` //~| ERROR mismatched types let bar = 1.5f32; diff --git a/src/test/ui/issues/issue-52496.stderr b/src/test/ui/issues/issue-52496.stderr index 3c8056316603b..e69b9b7c87f40 100644 --- a/src/test/ui/issues/issue-52496.stderr +++ b/src/test/ui/issues/issue-52496.stderr @@ -1,8 +1,8 @@ -error: numeric float literals must have a significant +error: float literals must have an integer part --> $DIR/issue-52496.rs:4:24 | LL | let _ = Foo { bar: .5, baz: 42 }; - | ^^ help: numeric float literals must have a significant: `0.5` + | ^^ help: must have an integer part: `0.5` error: expected one of `,` or `}`, found `.` --> $DIR/issue-52496.rs:9:22 From 15bad8bbfd3125b1e94d04f274910e24d0bb63eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 20 Jan 2019 14:25:53 -0800 Subject: [PATCH 4/6] Extend incorrect float literal recovery to account for suffixes --- src/libsyntax/parse/parser.rs | 20 +++++++-- src/test/ui/issues/issue-52496.rs | 1 - src/test/ui/issues/issue-52496.stderr | 20 +++------ .../ui/suggestions/recover-invalid-float.rs | 11 +++++ .../suggestions/recover-invalid-float.stderr | 42 +++++++++++++++++++ 5 files changed, 74 insertions(+), 20 deletions(-) create mode 100644 src/test/ui/suggestions/recover-invalid-float.rs create mode 100644 src/test/ui/suggestions/recover-invalid-float.stderr diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index fe6fa5e97d721..038d949d24aa6 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -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!(); }; diff --git a/src/test/ui/issues/issue-52496.rs b/src/test/ui/issues/issue-52496.rs index e734e9bc51332..e9ffeaf6c8901 100644 --- a/src/test/ui/issues/issue-52496.rs +++ b/src/test/ui/issues/issue-52496.rs @@ -4,7 +4,6 @@ fn main() { let _ = Foo { bar: .5, baz: 42 }; //~^ ERROR float literals must have an integer part //~| 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 e69b9b7c87f40..12fe7e7fc1f05 100644 --- a/src/test/ui/issues/issue-52496.stderr +++ b/src/test/ui/issues/issue-52496.stderr @@ -5,7 +5,7 @@ LL | let _ = Foo { bar: .5, baz: 42 }; | ^^ help: must have an integer part: `0.5` error: expected one of `,` or `}`, found `.` - --> $DIR/issue-52496.rs:9:22 + --> $DIR/issue-52496.rs:8:22 | LL | let _ = Foo { bar.into(), bat: -1, . }; | --- ^ expected one of `,` or `}` here @@ -13,23 +13,13 @@ LL | let _ = Foo { bar.into(), bat: -1, . }; | while parsing this struct error: expected identifier, found `.` - --> $DIR/issue-52496.rs:9:40 + --> $DIR/issue-52496.rs:8: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 | @@ -37,7 +27,7 @@ LL | let _ = Foo { bar: .5, baz: 42 }; | ^^^ missing `bat` error[E0308]: mismatched types - --> $DIR/issue-52496.rs:9:19 + --> $DIR/issue-52496.rs:8:19 | LL | let _ = Foo { bar.into(), bat: -1, . }; | ^^^ expected f64, found f32 @@ -47,12 +37,12 @@ LL | let _ = Foo { bar.into().into(), bat: -1, . }; | ^^^^^^^^^^ error[E0063]: missing field `baz` in initializer of `Foo` - --> $DIR/issue-52496.rs:9:13 + --> $DIR/issue-52496.rs:8:13 | LL | let _ = Foo { bar.into(), bat: -1, . }; | ^^^ missing `baz` -error: aborting due to 7 previous errors +error: aborting due to 6 previous errors Some errors occurred: E0063, E0308. For more information about an error, try `rustc --explain E0063`. diff --git a/src/test/ui/suggestions/recover-invalid-float.rs b/src/test/ui/suggestions/recover-invalid-float.rs new file mode 100644 index 0000000000000..506ef8900b881 --- /dev/null +++ b/src/test/ui/suggestions/recover-invalid-float.rs @@ -0,0 +1,11 @@ +fn main() { + let _: usize = .3; + //~^ ERROR float literals must have an integer part + //~| ERROR mismatched types + let _: usize = .42f32; + //~^ ERROR float literals must have an integer part + //~| ERROR mismatched types + let _: usize = .5f64; + //~^ ERROR float literals must have an integer part + //~| ERROR mismatched types +} diff --git a/src/test/ui/suggestions/recover-invalid-float.stderr b/src/test/ui/suggestions/recover-invalid-float.stderr new file mode 100644 index 0000000000000..c464676b444cc --- /dev/null +++ b/src/test/ui/suggestions/recover-invalid-float.stderr @@ -0,0 +1,42 @@ +error: float literals must have an integer part + --> $DIR/recover-invalid-float.rs:2:20 + | +LL | let _: usize = .3; + | ^^ help: must have an integer part: `0.3` + +error: float literals must have an integer part + --> $DIR/recover-invalid-float.rs:5:20 + | +LL | let _: usize = .42f32; + | ^^^^^^ help: must have an integer part: `0.42f32` + +error: float literals must have an integer part + --> $DIR/recover-invalid-float.rs:8:20 + | +LL | let _: usize = .5f64; + | ^^^^^ help: must have an integer part: `0.5f64` + +error[E0308]: mismatched types + --> $DIR/recover-invalid-float.rs:2:20 + | +LL | let _: usize = .3; + | ^^ expected usize, found floating-point number + | + = note: expected type `usize` + found type `{float}` + +error[E0308]: mismatched types + --> $DIR/recover-invalid-float.rs:5:20 + | +LL | let _: usize = .42f32; + | ^^^^^^ expected usize, found f32 + +error[E0308]: mismatched types + --> $DIR/recover-invalid-float.rs:8:20 + | +LL | let _: usize = .5f64; + | ^^^^^ expected usize, found f64 + +error: aborting due to 6 previous errors + +For more information about this error, try `rustc --explain E0308`. From defa61f3fb2612358b57c206c5e16da2751e6deb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sun, 20 Jan 2019 15:16:36 -0800 Subject: [PATCH 5/6] Tweak field parse error recovery --- src/libsyntax/parse/parser.rs | 25 +++++-------------- src/test/ui/issues/issue-52496.rs | 3 +-- src/test/ui/issues/issue-52496.stderr | 19 +++----------- src/test/ui/parser/removed-syntax-with-1.rs | 2 +- .../ui/parser/removed-syntax-with-1.stderr | 4 +-- src/test/ui/parser/removed-syntax-with-2.rs | 3 +-- .../ui/parser/removed-syntax-with-2.stderr | 19 ++++---------- 7 files changed, 20 insertions(+), 55 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 038d949d24aa6..a2d3595b47206 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2695,28 +2695,12 @@ impl<'a> Parser<'a> { break; } - let mut recovery_field = None; - if let token::Ident(ident, _) = self.token { - if !self.token.is_reserved_ident() { - let mut ident = ident.clone(); - ident.span = self.span; - recovery_field = Some(ast::Field { - ident, - span: self.span, - expr: self.mk_expr(self.span, ExprKind::Err, ThinVec::new()), - is_shorthand: true, - attrs: ThinVec::new(), - }); - } - } + let mut parsed_field = None; match self.parse_field() { - Ok(f) => fields.push(f), + Ok(f) => parsed_field = Some(f), Err(mut e) => { e.span_label(struct_sp, "while parsing this struct"); e.emit(); - if let Some(f) = recovery_field { - fields.push(f); - } // If the next token is a comma, then try to parse // what comes next as additional fields, rather than @@ -2732,7 +2716,10 @@ impl<'a> Parser<'a> { match self.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Brace)]) { - Ok(()) => {} + Ok(()) => if let Some(f) = parsed_field { + // only include the field if there's no parse error + fields.push(f); + } Err(mut e) => { e.span_label(struct_sp, "while parsing this struct"); e.emit(); diff --git a/src/test/ui/issues/issue-52496.rs b/src/test/ui/issues/issue-52496.rs index e9ffeaf6c8901..4e9453653735a 100644 --- a/src/test/ui/issues/issue-52496.rs +++ b/src/test/ui/issues/issue-52496.rs @@ -7,7 +7,6 @@ fn main() { let bar = 1.5f32; let _ = Foo { bar.into(), bat: -1, . }; //~^ ERROR expected one of - //~| ERROR mismatched types - //~| ERROR missing field `baz` in initializer of `Foo` + //~| ERROR missing fields `bar`, `baz` in initializer of `Foo` //~| ERROR expected identifier, found `.` } diff --git a/src/test/ui/issues/issue-52496.stderr b/src/test/ui/issues/issue-52496.stderr index 12fe7e7fc1f05..43009a15bd49a 100644 --- a/src/test/ui/issues/issue-52496.stderr +++ b/src/test/ui/issues/issue-52496.stderr @@ -26,23 +26,12 @@ error[E0063]: missing field `bat` in initializer of `Foo` LL | let _ = Foo { bar: .5, baz: 42 }; | ^^^ missing `bat` -error[E0308]: mismatched types - --> $DIR/issue-52496.rs:8: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.into().into(), bat: -1, . }; - | ^^^^^^^^^^ - -error[E0063]: missing field `baz` in initializer of `Foo` +error[E0063]: missing fields `bar`, `baz` in initializer of `Foo` --> $DIR/issue-52496.rs:8:13 | LL | let _ = Foo { bar.into(), bat: -1, . }; - | ^^^ missing `baz` + | ^^^ missing `bar`, `baz` -error: aborting due to 6 previous errors +error: aborting due to 5 previous errors -Some errors occurred: E0063, E0308. -For more information about an error, try `rustc --explain E0063`. +For more information about this error, try `rustc --explain E0063`. diff --git a/src/test/ui/parser/removed-syntax-with-1.rs b/src/test/ui/parser/removed-syntax-with-1.rs index 57cbe8d5be655..add024ea3907f 100644 --- a/src/test/ui/parser/removed-syntax-with-1.rs +++ b/src/test/ui/parser/removed-syntax-with-1.rs @@ -7,5 +7,5 @@ fn main() { let a = S { foo: (), bar: () }; let b = S { foo: () with a }; //~^ ERROR expected one of `,`, `.`, `?`, `}`, or an operator, found `with` - //~| ERROR missing field `bar` in initializer of `main::S` + //~| ERROR missing fields `bar`, `foo` in initializer of `main::S` } diff --git a/src/test/ui/parser/removed-syntax-with-1.stderr b/src/test/ui/parser/removed-syntax-with-1.stderr index b5956ad339db8..aae29efa85e54 100644 --- a/src/test/ui/parser/removed-syntax-with-1.stderr +++ b/src/test/ui/parser/removed-syntax-with-1.stderr @@ -6,11 +6,11 @@ LL | let b = S { foo: () with a }; | | | while parsing this struct -error[E0063]: missing field `bar` in initializer of `main::S` +error[E0063]: missing fields `bar`, `foo` in initializer of `main::S` --> $DIR/removed-syntax-with-1.rs:8:13 | LL | let b = S { foo: () with a }; - | ^ missing `bar` + | ^ missing `bar`, `foo` error: aborting due to 2 previous errors diff --git a/src/test/ui/parser/removed-syntax-with-2.rs b/src/test/ui/parser/removed-syntax-with-2.rs index 11db391c5489a..f666da49696ba 100644 --- a/src/test/ui/parser/removed-syntax-with-2.rs +++ b/src/test/ui/parser/removed-syntax-with-2.rs @@ -7,6 +7,5 @@ fn main() { let a = S { foo: (), bar: () }; let b = S { foo: (), with a }; //~^ ERROR expected one of `,` or `}`, found `a` - //~| ERROR cannot find value `with` in this scope - //~| ERROR struct `main::S` has no field named `with` + //~| ERROR missing field `bar` in initializer of `main::S` } diff --git a/src/test/ui/parser/removed-syntax-with-2.stderr b/src/test/ui/parser/removed-syntax-with-2.stderr index ee7560017a675..7717b49d3a2c7 100644 --- a/src/test/ui/parser/removed-syntax-with-2.stderr +++ b/src/test/ui/parser/removed-syntax-with-2.stderr @@ -6,21 +6,12 @@ LL | let b = S { foo: (), with a }; | | | while parsing this struct -error[E0425]: cannot find value `with` in this scope - --> $DIR/removed-syntax-with-2.rs:8:26 +error[E0063]: missing field `bar` in initializer of `main::S` + --> $DIR/removed-syntax-with-2.rs:8:13 | LL | let b = S { foo: (), with a }; - | ^^^^ not found in this scope + | ^ missing `bar` -error[E0560]: struct `main::S` has no field named `with` - --> $DIR/removed-syntax-with-2.rs:8:26 - | -LL | let b = S { foo: (), with a }; - | ^^^^ `main::S` does not have this field - | - = note: available fields are: `foo`, `bar` - -error: aborting due to 3 previous errors +error: aborting due to 2 previous errors -Some errors occurred: E0425, E0560. -For more information about an error, try `rustc --explain E0425`. +For more information about this error, try `rustc --explain E0063`. From 4745b86202f0e96b4c0d0de05220a5ac4b5308ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Mon, 21 Jan 2019 15:28:51 -0800 Subject: [PATCH 6/6] Accept more invalid code that is close to correct fields --- src/libsyntax/parse/parser.rs | 22 +++++++++++++++++-- src/test/ui/parser/removed-syntax-with-1.rs | 3 +-- .../ui/parser/removed-syntax-with-1.stderr | 11 ++-------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index a2d3595b47206..745f2c7dc19e4 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2695,6 +2695,21 @@ impl<'a> Parser<'a> { break; } + let mut recovery_field = None; + if let token::Ident(ident, _) = self.token { + if !self.token.is_reserved_ident() && self.look_ahead(1, |t| *t == token::Colon) { + // Use in case of error after field-looking code: `S { foo: () with a }` + let mut ident = ident.clone(); + ident.span = self.span; + recovery_field = Some(ast::Field { + ident, + span: self.span, + expr: self.mk_expr(self.span, ExprKind::Err, ThinVec::new()), + is_shorthand: false, + attrs: ThinVec::new(), + }); + } + } let mut parsed_field = None; match self.parse_field() { Ok(f) => parsed_field = Some(f), @@ -2716,11 +2731,14 @@ impl<'a> Parser<'a> { match self.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Brace)]) { - Ok(()) => if let Some(f) = parsed_field { - // only include the field if there's no parse error + Ok(()) => if let Some(f) = parsed_field.or(recovery_field) { + // only include the field if there's no parse error for the field name fields.push(f); } Err(mut e) => { + if let Some(f) = recovery_field { + fields.push(f); + } e.span_label(struct_sp, "while parsing this struct"); e.emit(); self.recover_stmt_(SemiColonMode::Comma, BlockMode::Ignore); diff --git a/src/test/ui/parser/removed-syntax-with-1.rs b/src/test/ui/parser/removed-syntax-with-1.rs index add024ea3907f..2c1e152dcee75 100644 --- a/src/test/ui/parser/removed-syntax-with-1.rs +++ b/src/test/ui/parser/removed-syntax-with-1.rs @@ -5,7 +5,6 @@ fn main() { } let a = S { foo: (), bar: () }; - let b = S { foo: () with a }; + let b = S { foo: () with a, bar: () }; //~^ ERROR expected one of `,`, `.`, `?`, `}`, or an operator, found `with` - //~| ERROR missing fields `bar`, `foo` in initializer of `main::S` } diff --git a/src/test/ui/parser/removed-syntax-with-1.stderr b/src/test/ui/parser/removed-syntax-with-1.stderr index aae29efa85e54..a157873916a64 100644 --- a/src/test/ui/parser/removed-syntax-with-1.stderr +++ b/src/test/ui/parser/removed-syntax-with-1.stderr @@ -1,17 +1,10 @@ error: expected one of `,`, `.`, `?`, `}`, or an operator, found `with` --> $DIR/removed-syntax-with-1.rs:8:25 | -LL | let b = S { foo: () with a }; +LL | let b = S { foo: () with a, bar: () }; | - ^^^^ expected one of `,`, `.`, `?`, `}`, or an operator here | | | while parsing this struct -error[E0063]: missing fields `bar`, `foo` in initializer of `main::S` - --> $DIR/removed-syntax-with-1.rs:8:13 - | -LL | let b = S { foo: () with a }; - | ^ missing `bar`, `foo` - -error: aborting due to 2 previous errors +error: aborting due to previous error -For more information about this error, try `rustc --explain E0063`.