From b1f169fe7a19cf10f70ee2aa2513276185c70e9b Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Sun, 20 Jan 2019 00:37:06 -0800 Subject: 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 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 8 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 7e15b231276..9b20937cf93 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(); } } _ => { -- cgit 1.4.1-3-g733a5 From acbda76f23b8945fd8f45332352269044ecbf2ca Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Sun, 20 Jan 2019 01:49:04 -0800 Subject: 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(-) (limited to 'src/libsyntax/parse') 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`. -- cgit 1.4.1-3-g733a5 From e387597a8f789ab6e37e6ce1bf67c8c45d4827c3 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Sun, 20 Jan 2019 13:59:35 -0800 Subject: 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(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 43a263b8a6b..fe6fa5e97d7 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 2e790792675..e734e9bc513 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 3c805631660..e69b9b7c87f 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 -- cgit 1.4.1-3-g733a5 From 15bad8bbfd3125b1e94d04f274910e24d0bb63eb Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Sun, 20 Jan 2019 14:25:53 -0800 Subject: 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 +++-------- src/test/ui/suggestions/recover-invalid-float.rs | 11 ++++++ .../ui/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 (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index fe6fa5e97d7..038d949d24a 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 e734e9bc513..e9ffeaf6c89 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 e69b9b7c87f..12fe7e7fc1f 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 00000000000..506ef8900b8 --- /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 00000000000..c464676b444 --- /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`. -- cgit 1.4.1-3-g733a5 From defa61f3fb2612358b57c206c5e16da2751e6deb Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Sun, 20 Jan 2019 15:16:36 -0800 Subject: 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 +- src/test/ui/parser/removed-syntax-with-1.stderr | 4 ++-- src/test/ui/parser/removed-syntax-with-2.rs | 3 +-- src/test/ui/parser/removed-syntax-with-2.stderr | 19 +++++-------------- 7 files changed, 20 insertions(+), 55 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 038d949d24a..a2d3595b472 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 e9ffeaf6c89..4e945365373 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 12fe7e7fc1f..43009a15bd4 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 57cbe8d5be6..add024ea390 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 b5956ad339d..aae29efa85e 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 11db391c548..f666da49696 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 ee7560017a6..7717b49d3a2 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`. -- cgit 1.4.1-3-g733a5 From 45a95b512c6fb491518d6a3f4b667d6dd82cd56b Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Sun, 20 Jan 2019 19:37:38 -0800 Subject: Use structured suggestion in stead of notes --- src/librustc_typeck/check/method/suggest.rs | 14 ++++++++++++-- src/libsyntax/parse/parser.rs | 13 ++++++++++--- src/test/ui/auto-ref-slice-plus-ref.stderr | 3 +-- src/test/ui/block-result/issue-3563.stderr | 4 +--- src/test/ui/empty/empty-struct-braces-expr.stderr | 10 ++++------ src/test/ui/issues/issue-23217.stderr | 5 ++--- src/test/ui/issues/issue-28344.stderr | 6 ++---- src/test/ui/issues/issue-28971.stderr | 7 ++++--- .../issue-50264-inner-deref-trait/result-deref-err.stderr | 3 +-- src/test/ui/parser/issue-17718-const-mut.rs | 2 +- src/test/ui/parser/issue-17718-const-mut.stderr | 6 +++--- src/test/ui/suggestions/suggest-methods.stderr | 12 +++--------- 12 files changed, 44 insertions(+), 41 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 23bcd88d6af..f71a163cee2 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -304,7 +304,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { ); if let Some(suggestion) = suggestion { // enum variant - err.help(&format!("did you mean `{}`?", suggestion)); + err.span_suggestion_with_applicability( + item_name.span, + "did you mean", + suggestion.to_string(), + Applicability::MaybeIncorrect, + ); } err } @@ -440,7 +445,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } if let Some(lev_candidate) = lev_candidate { - err.help(&format!("did you mean `{}`?", lev_candidate.ident)); + err.span_suggestion_with_applicability( + span, + "did you mean", + lev_candidate.ident.to_string(), + Applicability::MaybeIncorrect, + ); } err.emit(); } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 7e15b231276..d4a3411f463 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -7271,9 +7271,16 @@ impl<'a> Parser<'a> { // CONST ITEM if self.eat_keyword(keywords::Mut) { let prev_span = self.prev_span; - self.diagnostic().struct_span_err(prev_span, "const globals cannot be mutable") - .help("did you mean to declare a static?") - .emit(); + let mut err = self.diagnostic() + .struct_span_err(prev_span, "const globals cannot be mutable"); + err.span_label(prev_span, "cannot be mutable"); + err.span_suggestion_with_applicability( + const_span, + "you might want to declare a static instead", + "static".to_owned(), + Applicability::MaybeIncorrect, + ); + err.emit(); } let (ident, item_, extra_attrs) = self.parse_item_const(None)?; let prev_span = self.prev_span; diff --git a/src/test/ui/auto-ref-slice-plus-ref.stderr b/src/test/ui/auto-ref-slice-plus-ref.stderr index ab57fec0e73..356e24d18a7 100644 --- a/src/test/ui/auto-ref-slice-plus-ref.stderr +++ b/src/test/ui/auto-ref-slice-plus-ref.stderr @@ -2,12 +2,11 @@ error[E0599]: no method named `test_mut` found for type `std::vec::Vec<{integer} --> $DIR/auto-ref-slice-plus-ref.rs:7:7 | LL | a.test_mut(); //~ ERROR no method named `test_mut` found - | ^^^^^^^^ + | ^^^^^^^^ help: did you mean: `get_mut` | = help: items from traits can only be used if the trait is implemented and in scope = note: the following trait defines an item `test_mut`, perhaps you need to implement it: candidate #1: `MyIter` - = help: did you mean `get_mut`? error[E0599]: no method named `test` found for type `std::vec::Vec<{integer}>` in the current scope --> $DIR/auto-ref-slice-plus-ref.rs:8:7 diff --git a/src/test/ui/block-result/issue-3563.stderr b/src/test/ui/block-result/issue-3563.stderr index 7f386630de5..a6346a5233f 100644 --- a/src/test/ui/block-result/issue-3563.stderr +++ b/src/test/ui/block-result/issue-3563.stderr @@ -2,9 +2,7 @@ error[E0599]: no method named `b` found for type `&Self` in the current scope --> $DIR/issue-3563.rs:3:17 | LL | || self.b() - | ^ - | - = help: did you mean `a`? + | ^ help: did you mean: `a` error: aborting due to previous error diff --git a/src/test/ui/empty/empty-struct-braces-expr.stderr b/src/test/ui/empty/empty-struct-braces-expr.stderr index e595e0ccb92..19844503a48 100644 --- a/src/test/ui/empty/empty-struct-braces-expr.stderr +++ b/src/test/ui/empty/empty-struct-braces-expr.stderr @@ -51,20 +51,18 @@ error[E0599]: no variant named `Empty3` found for type `empty_struct::XE` in the | LL | let xe3 = XE::Empty3; //~ ERROR no variant named `Empty3` found for type | ----^^^^^^ - | | + | | | + | | help: did you mean: `XEmpty3` | variant not found in `empty_struct::XE` - | - = help: did you mean `XEmpty3`? error[E0599]: no variant named `Empty3` found for type `empty_struct::XE` in the current scope --> $DIR/empty-struct-braces-expr.rs:23:19 | LL | let xe3 = XE::Empty3(); //~ ERROR no variant named `Empty3` found for type | ----^^^^^^ - | | + | | | + | | help: did you mean: `XEmpty3` | variant not found in `empty_struct::XE` - | - = help: did you mean `XEmpty3`? error: aborting due to 8 previous errors diff --git a/src/test/ui/issues/issue-23217.stderr b/src/test/ui/issues/issue-23217.stderr index 208d0cc499a..9cad002036f 100644 --- a/src/test/ui/issues/issue-23217.stderr +++ b/src/test/ui/issues/issue-23217.stderr @@ -5,10 +5,9 @@ LL | pub enum SomeEnum { | ----------------- variant `A` not found here LL | B = SomeEnum::A, | ----------^ - | | + | | | + | | help: did you mean: `B` | variant not found in `SomeEnum` - | - = help: did you mean `B`? error: aborting due to previous error diff --git a/src/test/ui/issues/issue-28344.stderr b/src/test/ui/issues/issue-28344.stderr index 146ebad6ce1..b6f520c644b 100644 --- a/src/test/ui/issues/issue-28344.stderr +++ b/src/test/ui/issues/issue-28344.stderr @@ -11,8 +11,7 @@ LL | let x: u8 = BitXor::bitor(0 as u8, 0 as u8); | --------^^^^^ | | | function or associated item not found in `dyn std::ops::BitXor<_>` - | - = help: did you mean `bitxor`? + | help: did you mean: `bitxor` error[E0191]: the value of the associated type `Output` (from the trait `std::ops::BitXor`) must be specified --> $DIR/issue-28344.rs:8:13 @@ -27,8 +26,7 @@ LL | let g = BitXor::bitor; | --------^^^^^ | | | function or associated item not found in `dyn std::ops::BitXor<_>` - | - = help: did you mean `bitxor`? + | help: did you mean: `bitxor` error: aborting due to 4 previous errors diff --git a/src/test/ui/issues/issue-28971.stderr b/src/test/ui/issues/issue-28971.stderr index d5dbd5f6488..77d0b53ad21 100644 --- a/src/test/ui/issues/issue-28971.stderr +++ b/src/test/ui/issues/issue-28971.stderr @@ -5,9 +5,10 @@ LL | enum Foo { | -------- variant `Baz` not found here ... LL | Foo::Baz(..) => (), - | -----^^^---- variant not found in `Foo` - | - = help: did you mean `Bar`? + | -----^^^---- + | | | + | | help: did you mean: `Bar` + | variant not found in `Foo` error: aborting due to previous error diff --git a/src/test/ui/issues/issue-50264-inner-deref-trait/result-deref-err.stderr b/src/test/ui/issues/issue-50264-inner-deref-trait/result-deref-err.stderr index 99c4a5b03b3..96d6814b0fe 100644 --- a/src/test/ui/issues/issue-50264-inner-deref-trait/result-deref-err.stderr +++ b/src/test/ui/issues/issue-50264-inner-deref-trait/result-deref-err.stderr @@ -2,11 +2,10 @@ error[E0599]: no method named `deref_err` found for type `std::result::Result<_, --> $DIR/result-deref-err.rs:4:28 | LL | let _result = &Err(41).deref_err(); - | ^^^^^^^^^ + | ^^^^^^^^^ help: did you mean: `deref_ok` | = note: the method `deref_err` exists but the following trait bounds were not satisfied: `{integer} : std::ops::Deref` - = help: did you mean `deref_ok`? error: aborting due to previous error diff --git a/src/test/ui/parser/issue-17718-const-mut.rs b/src/test/ui/parser/issue-17718-const-mut.rs index 4e74516d6b6..795a8c7631d 100644 --- a/src/test/ui/parser/issue-17718-const-mut.rs +++ b/src/test/ui/parser/issue-17718-const-mut.rs @@ -1,6 +1,6 @@ const mut //~ ERROR: const globals cannot be mutable -//~^ HELP did you mean to declare a static? +//~^^ HELP you might want to declare a static instead FOO: usize = 3; fn main() { diff --git a/src/test/ui/parser/issue-17718-const-mut.stderr b/src/test/ui/parser/issue-17718-const-mut.stderr index 29a65ebe418..19f9fe19ef5 100644 --- a/src/test/ui/parser/issue-17718-const-mut.stderr +++ b/src/test/ui/parser/issue-17718-const-mut.stderr @@ -1,10 +1,10 @@ error: const globals cannot be mutable --> $DIR/issue-17718-const-mut.rs:2:1 | +LL | const + | ----- help: you might want to declare a static instead: `static` LL | mut //~ ERROR: const globals cannot be mutable - | ^^^ - | - = help: did you mean to declare a static? + | ^^^ cannot be mutable error: aborting due to previous error diff --git a/src/test/ui/suggestions/suggest-methods.stderr b/src/test/ui/suggestions/suggest-methods.stderr index 39d96a943a1..b7727cf03a4 100644 --- a/src/test/ui/suggestions/suggest-methods.stderr +++ b/src/test/ui/suggestions/suggest-methods.stderr @@ -5,25 +5,19 @@ LL | struct Foo; | ----------- method `bat` not found for this ... LL | f.bat(1.0); //~ ERROR no method named - | ^^^ - | - = help: did you mean `bar`? + | ^^^ help: did you mean: `bar` error[E0599]: no method named `is_emtpy` found for type `std::string::String` in the current scope --> $DIR/suggest-methods.rs:21:15 | LL | let _ = s.is_emtpy(); //~ ERROR no method named - | ^^^^^^^^ - | - = help: did you mean `is_empty`? + | ^^^^^^^^ help: did you mean: `is_empty` error[E0599]: no method named `count_eos` found for type `u32` in the current scope --> $DIR/suggest-methods.rs:25:19 | LL | let _ = 63u32.count_eos(); //~ ERROR no method named - | ^^^^^^^^^ - | - = help: did you mean `count_zeros`? + | ^^^^^^^^^ help: did you mean: `count_zeros` error[E0599]: no method named `count_o` found for type `u32` in the current scope --> $DIR/suggest-methods.rs:28:19 -- cgit 1.4.1-3-g733a5 From 6c399d155c6307563a2022fe98bc2e596af1cfc4 Mon Sep 17 00:00:00 2001 From: David Wood Date: Mon, 21 Jan 2019 19:42:06 +0100 Subject: Add error for trailing angle brackets. This commit adds a error (and accompanying machine applicable suggestion) for trailing angle brackets on function calls with a turbofish. --- src/libsyntax/ast.rs | 14 +++++ src/libsyntax/parse/parser.rs | 97 +++++++++++++++++++++++++++++++++++ src/test/ui/issues/issue-54521-1.rs | 16 ++++++ src/test/ui/issues/issue-54521.fixed | 22 ++++++++ src/test/ui/issues/issue-54521.rs | 22 ++++++++ src/test/ui/issues/issue-54521.stderr | 26 ++++++++++ 6 files changed, 197 insertions(+) create mode 100644 src/test/ui/issues/issue-54521-1.rs create mode 100644 src/test/ui/issues/issue-54521.fixed create mode 100644 src/test/ui/issues/issue-54521.rs create mode 100644 src/test/ui/issues/issue-54521.stderr (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 405cf612543..e520ac3bdd4 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -140,6 +140,20 @@ pub enum GenericArgs { } impl GenericArgs { + pub fn is_parenthesized(&self) -> bool { + match *self { + Parenthesized(..) => true, + _ => false, + } + } + + pub fn is_angle_bracketed(&self) -> bool { + match *self { + AngleBracketed(..) => true, + _ => false, + } + } + pub fn span(&self) -> Span { match *self { AngleBracketed(ref data) => data.span, diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 439eec5b0c4..d7c209d12a8 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2757,6 +2757,8 @@ impl<'a> Parser<'a> { // Assuming we have just parsed `.`, continue parsing into an expression. fn parse_dot_suffix(&mut self, self_arg: P, lo: Span) -> PResult<'a, P> { let segment = self.parse_path_segment(PathStyle::Expr, true)?; + self.check_trailing_angle_brackets(&segment); + Ok(match self.token { token::OpenDelim(token::Paren) => { // Method call `expr.f()` @@ -2784,6 +2786,101 @@ impl<'a> Parser<'a> { }) } + /// This function checks if there are trailing angle brackets and produces + /// a diagnostic to suggest removing them. + /// + /// ```ignore (diagnostic) + /// let _ = vec![1, 2, 3].into_iter().collect::>>>(); + /// ^^ help: remove extra angle brackets + /// ``` + fn check_trailing_angle_brackets(&mut self, segment: &PathSegment) { + // This function is intended to be invoked from `parse_dot_suffix` where there are two + // cases: + // + // - A field access (eg. `x.foo`) + // - A method call (eg. `x.foo()`) + // + // This function is called after parsing `.foo` and before parsing any parenthesis (if + // present). This includes any angle bracket arguments, such as `.foo::`. + + // We only care about trailing angle brackets if we previously parsed angle bracket + // arguments. This helps stop us incorrectly suggesting that extra angle brackets be + // removed in this case: + // + // `x.foo >> (3)` (where `x.foo` is a `u32` for example) + // + // This case is particularly tricky as we won't notice it just looking at the tokens - + // it will appear the same (in terms of upcoming tokens) as below (since the `::` will + // have already been parsed): + // + // `x.foo::>>(3)` + let parsed_angle_bracket_args = segment.args + .as_ref() + .map(|args| args.is_angle_bracketed()) + .unwrap_or(false); + + debug!( + "check_trailing_angle_brackets: parsed_angle_bracket_args={:?}", + parsed_angle_bracket_args, + ); + if !parsed_angle_bracket_args { + return; + } + + // Keep the span at the start so we can highlight the sequence of `>` characters to be + // removed. + let lo = self.span; + + // We need to look-ahead to see if we have `>` characters without moving the cursor forward + // (since we might have the field access case and the characters we're eating are + // actual operators and not trailing characters - ie `x.foo >> 3`). + let mut position = 0; + + // The first tokens we will encounter are shift right tokens (`>>`) since pairs of `>` + // characters will have been grouped together by the tokenizer. + let mut number_of_shr = 0; + while self.look_ahead(position, |t| *t == token::BinOp(token::BinOpToken::Shr)) { + number_of_shr += 1; + position += 1; + } + + // Afterwards, there will be at most one `>` character remaining (more than one and it'd + // have shown up as a `>>`). + let encountered_gt = self.look_ahead(position, |t| *t == token::Gt); + if encountered_gt { + position += 1; + } + + // If we didn't find any trailing `>>` characters or a trailing `>`, then we have + // nothing to error about. + debug!( + "check_trailing_angle_brackets: encountered_gt={:?} number_of_shr={:?}", + encountered_gt, number_of_shr, + ); + if !encountered_gt && number_of_shr < 1 { + return; + } + + // Finally, double check that we have a left parenthesis next as otherwise this is the + // field case. + if self.look_ahead(position, |t| *t == token::OpenDelim(token::Paren)) { + // Eat from where we started until the left parenthesis so that parsing can continue + // as if we didn't have those extra angle brackets. + self.eat_to_tokens(&[&token::OpenDelim(token::Paren)]); + let span = lo.until(self.span); + + self.diagnostic() + .struct_span_err(span, "unmatched angle bracket") + .span_suggestion_with_applicability( + span, + "remove extra angle bracket", + String::new(), + Applicability::MachineApplicable, + ) + .emit(); + } + } + fn parse_dot_or_call_expr_with_(&mut self, e0: P, lo: Span) -> PResult<'a, P> { let mut e = e0; let mut hi; diff --git a/src/test/ui/issues/issue-54521-1.rs b/src/test/ui/issues/issue-54521-1.rs new file mode 100644 index 00000000000..d6a14a6e11f --- /dev/null +++ b/src/test/ui/issues/issue-54521-1.rs @@ -0,0 +1,16 @@ +// compile-pass + +// This test checks that the `remove extra angle brackets` error doesn't happen for some +// potential edge-cases.. + +struct X { + len: u32, +} + +fn main() { + let x = X { len: 3 }; + + let _ = x.len > (3); + + let _ = x.len >> (3); +} diff --git a/src/test/ui/issues/issue-54521.fixed b/src/test/ui/issues/issue-54521.fixed new file mode 100644 index 00000000000..84ab6866cf1 --- /dev/null +++ b/src/test/ui/issues/issue-54521.fixed @@ -0,0 +1,22 @@ +// run-rustfix + +// This test checks that the following error is emitted and the suggestion works: +// +// ``` +// let _ = vec![1, 2, 3].into_iter().collect::>>>(); +// ^^ help: remove extra angle brackets +// ``` + +fn main() { + let _ = vec![1, 2, 3].into_iter().collect::>(); + //~^ ERROR unmatched angle bracket + + let _ = vec![1, 2, 3].into_iter().collect::>(); + //~^ ERROR unmatched angle bracket + + let _ = vec![1, 2, 3].into_iter().collect::>(); + //~^ ERROR unmatched angle bracket + + let _ = vec![1, 2, 3].into_iter().collect::>(); + //~^ ERROR unmatched angle bracket +} diff --git a/src/test/ui/issues/issue-54521.rs b/src/test/ui/issues/issue-54521.rs new file mode 100644 index 00000000000..f1d68504178 --- /dev/null +++ b/src/test/ui/issues/issue-54521.rs @@ -0,0 +1,22 @@ +// run-rustfix + +// This test checks that the following error is emitted and the suggestion works: +// +// ``` +// let _ = vec![1, 2, 3].into_iter().collect::>>>(); +// ^^ help: remove extra angle brackets +// ``` + +fn main() { + let _ = vec![1, 2, 3].into_iter().collect::>>>>>(); + //~^ ERROR unmatched angle bracket + + let _ = vec![1, 2, 3].into_iter().collect::>>>>(); + //~^ ERROR unmatched angle bracket + + let _ = vec![1, 2, 3].into_iter().collect::>>>(); + //~^ ERROR unmatched angle bracket + + let _ = vec![1, 2, 3].into_iter().collect::>>(); + //~^ ERROR unmatched angle bracket +} diff --git a/src/test/ui/issues/issue-54521.stderr b/src/test/ui/issues/issue-54521.stderr new file mode 100644 index 00000000000..a67e9ca8daf --- /dev/null +++ b/src/test/ui/issues/issue-54521.stderr @@ -0,0 +1,26 @@ +error: unmatched angle bracket + --> $DIR/issue-54521.rs:11:60 + | +LL | let _ = vec![1, 2, 3].into_iter().collect::>>>>>(); + | ^^^^ help: remove extra angle bracket + +error: unmatched angle bracket + --> $DIR/issue-54521.rs:14:60 + | +LL | let _ = vec![1, 2, 3].into_iter().collect::>>>>(); + | ^^^ help: remove extra angle bracket + +error: unmatched angle bracket + --> $DIR/issue-54521.rs:17:60 + | +LL | let _ = vec![1, 2, 3].into_iter().collect::>>>(); + | ^^ help: remove extra angle bracket + +error: unmatched angle bracket + --> $DIR/issue-54521.rs:20:60 + | +LL | let _ = vec![1, 2, 3].into_iter().collect::>>(); + | ^ help: remove extra angle bracket + +error: aborting due to 4 previous errors + -- cgit 1.4.1-3-g733a5 From 3f0fc9b03569e03dbdf5fdc3a67f246aad3b40b8 Mon Sep 17 00:00:00 2001 From: David Wood Date: Mon, 21 Jan 2019 21:16:46 +0100 Subject: Pluralize error messages. This commit pluralizes error messages when more than a single trailing `>` character is present. --- src/libsyntax/parse/parser.rs | 11 +++++++++-- src/test/ui/issues/issue-54521.stderr | 12 ++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index d7c209d12a8..6a881eb6241 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2869,11 +2869,18 @@ impl<'a> Parser<'a> { self.eat_to_tokens(&[&token::OpenDelim(token::Paren)]); let span = lo.until(self.span); + // We needn't check `encountered_gt` to determine if we should pluralize "bracket". + // `encountered_gt` can only represent a single `>` character, if `number_of_shr >= 1` + // then there is either `>>` or `>>>` - in either case a plural is warranted. + let plural = number_of_shr >= 1; self.diagnostic() - .struct_span_err(span, "unmatched angle bracket") + .struct_span_err( + span, + &format!("unmatched angle bracket{}", if plural { "s" } else { "" }), + ) .span_suggestion_with_applicability( span, - "remove extra angle bracket", + &format!("remove extra angle bracket{}", if plural { "s" } else { "" }), String::new(), Applicability::MachineApplicable, ) diff --git a/src/test/ui/issues/issue-54521.stderr b/src/test/ui/issues/issue-54521.stderr index a67e9ca8daf..ffefbfd0348 100644 --- a/src/test/ui/issues/issue-54521.stderr +++ b/src/test/ui/issues/issue-54521.stderr @@ -1,20 +1,20 @@ -error: unmatched angle bracket +error: unmatched angle brackets --> $DIR/issue-54521.rs:11:60 | LL | let _ = vec![1, 2, 3].into_iter().collect::>>>>>(); - | ^^^^ help: remove extra angle bracket + | ^^^^ help: remove extra angle brackets -error: unmatched angle bracket +error: unmatched angle brackets --> $DIR/issue-54521.rs:14:60 | LL | let _ = vec![1, 2, 3].into_iter().collect::>>>>(); - | ^^^ help: remove extra angle bracket + | ^^^ help: remove extra angle brackets -error: unmatched angle bracket +error: unmatched angle brackets --> $DIR/issue-54521.rs:17:60 | LL | let _ = vec![1, 2, 3].into_iter().collect::>>>(); - | ^^ help: remove extra angle bracket + | ^^ help: remove extra angle brackets error: unmatched angle bracket --> $DIR/issue-54521.rs:20:60 -- cgit 1.4.1-3-g733a5 From 914d142c02b558a597055c66a0e7e09115416211 Mon Sep 17 00:00:00 2001 From: David Wood Date: Tue, 22 Jan 2019 00:35:31 +0100 Subject: Extend trailing `>` detection for paths. This commit extends the trailing `>` detection to also work for paths such as `Foo::>:Baz`. This involves making the existing check take the token that is expected to follow the path being checked as a parameter. Care is taken to ensure that this only happens on the construction of a whole path segment and not a partial path segment (during recursion). Through this enhancement, it was also observed that the ordering of right shift token and greater than tokens was overfitted to the examples being tested. In practice, given a sequence of `>` characters: `>>>>>>>>>` ..then they will be split into `>>` eagerly: `>> >> >> >> >`. ..but when a `<` is prepended, then the first `>>` is split: ` > >> >> >> >` ..and then when another `<` is prepended, a right shift is first again: `Vec<> >> >> >> >` In the previous commits, a example that had two `<<` characters was always used and therefore it was incorrectly assumed that `>>` would always be first - but when there is a single `<`, this is not the case. --- src/libsyntax/parse/parser.rs | 94 +++++++++++++++++++++------------ src/test/ui/issues/issue-54521-2.fixed | 22 ++++++++ src/test/ui/issues/issue-54521-2.rs | 22 ++++++++ src/test/ui/issues/issue-54521-2.stderr | 26 +++++++++ 4 files changed, 131 insertions(+), 33 deletions(-) create mode 100644 src/test/ui/issues/issue-54521-2.fixed create mode 100644 src/test/ui/issues/issue-54521-2.rs create mode 100644 src/test/ui/issues/issue-54521-2.stderr (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 6a881eb6241..d380948b891 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2149,7 +2149,27 @@ impl<'a> Parser<'a> { enable_warning: bool) -> PResult<'a, ()> { loop { - segments.push(self.parse_path_segment(style, enable_warning)?); + let segment = self.parse_path_segment(style, enable_warning)?; + if style == PathStyle::Expr { + // In order to check for trailing angle brackets, we must have finished + // recursing (`parse_path_segment` can indirectly call this function), + // that is, the next token must be the highlighted part of the below example: + // + // `Foo::>::Qux` + // ^ here + // + // As opposed to the below highlight (if we had only finished the first + // recursion): + // + // `Foo::>::Qux` + // ^ here + // + // `PathStyle::Expr` is only provided at the root invocation and never in + // `parse_path_segment` to recurse and therefore can be checked to maintain + // this invariant. + self.check_trailing_angle_brackets(&segment, token::ModSep); + } + segments.push(segment); if self.is_import_coupler() || !self.eat(&token::ModSep) { return Ok(()); @@ -2757,7 +2777,7 @@ impl<'a> Parser<'a> { // Assuming we have just parsed `.`, continue parsing into an expression. fn parse_dot_suffix(&mut self, self_arg: P, lo: Span) -> PResult<'a, P> { let segment = self.parse_path_segment(PathStyle::Expr, true)?; - self.check_trailing_angle_brackets(&segment); + self.check_trailing_angle_brackets(&segment, token::OpenDelim(token::Paren)); Ok(match self.token { token::OpenDelim(token::Paren) => { @@ -2793,15 +2813,19 @@ impl<'a> Parser<'a> { /// let _ = vec![1, 2, 3].into_iter().collect::>>>(); /// ^^ help: remove extra angle brackets /// ``` - fn check_trailing_angle_brackets(&mut self, segment: &PathSegment) { - // This function is intended to be invoked from `parse_dot_suffix` where there are two + fn check_trailing_angle_brackets(&mut self, segment: &PathSegment, end: token::Token) { + // This function is intended to be invoked after parsing a path segment where there are two // cases: // - // - A field access (eg. `x.foo`) - // - A method call (eg. `x.foo()`) + // 1. A specific token is expected after the path segment. + // eg. `x.foo(`, `x.foo::(` (parenthesis - method call), + // `Foo::`, or `Foo::::` (mod sep - continued path). + // 2. No specific token is expected after the path segment. + // eg. `x.foo` (field access) // - // This function is called after parsing `.foo` and before parsing any parenthesis (if - // present). This includes any angle bracket arguments, such as `.foo::`. + // This function is called after parsing `.foo` and before parsing the token `end` (if + // present). This includes any angle bracket arguments, such as `.foo::` or + // `Foo::`. // We only care about trailing angle brackets if we previously parsed angle bracket // arguments. This helps stop us incorrectly suggesting that extra angle brackets be @@ -2836,43 +2860,47 @@ impl<'a> Parser<'a> { // actual operators and not trailing characters - ie `x.foo >> 3`). let mut position = 0; - // The first tokens we will encounter are shift right tokens (`>>`) since pairs of `>` - // characters will have been grouped together by the tokenizer. + // We can encounter `>` or `>>` tokens in any order, so we need to keep track of how + // many of each (so we can correctly pluralize our error messages) and continue to + // advance. let mut number_of_shr = 0; - while self.look_ahead(position, |t| *t == token::BinOp(token::BinOpToken::Shr)) { - number_of_shr += 1; - position += 1; - } - - // Afterwards, there will be at most one `>` character remaining (more than one and it'd - // have shown up as a `>>`). - let encountered_gt = self.look_ahead(position, |t| *t == token::Gt); - if encountered_gt { + let mut number_of_gt = 0; + while self.look_ahead(position, |t| { + trace!("check_trailing_angle_brackets: t={:?}", t); + if *t == token::BinOp(token::BinOpToken::Shr) { + number_of_shr += 1; + true + } else if *t == token::Gt { + number_of_gt += 1; + true + } else { + false + } + }) { position += 1; } - // If we didn't find any trailing `>>` characters or a trailing `>`, then we have - // nothing to error about. + // If we didn't find any trailing `>` characters, then we have nothing to error about. debug!( - "check_trailing_angle_brackets: encountered_gt={:?} number_of_shr={:?}", - encountered_gt, number_of_shr, + "check_trailing_angle_brackets: number_of_gt={:?} number_of_shr={:?}", + number_of_gt, number_of_shr, ); - if !encountered_gt && number_of_shr < 1 { + if number_of_gt < 1 && number_of_shr < 1 { return; } - // Finally, double check that we have a left parenthesis next as otherwise this is the - // field case. - if self.look_ahead(position, |t| *t == token::OpenDelim(token::Paren)) { - // Eat from where we started until the left parenthesis so that parsing can continue + // Finally, double check that we have our end token as otherwise this is the + // second case. + if self.look_ahead(position, |t| { + trace!("check_trailing_angle_brackets: t={:?}", t); + *t == end + }) { + // Eat from where we started until the end token so that parsing can continue // as if we didn't have those extra angle brackets. - self.eat_to_tokens(&[&token::OpenDelim(token::Paren)]); + self.eat_to_tokens(&[&end]); let span = lo.until(self.span); - // We needn't check `encountered_gt` to determine if we should pluralize "bracket". - // `encountered_gt` can only represent a single `>` character, if `number_of_shr >= 1` - // then there is either `>>` or `>>>` - in either case a plural is warranted. - let plural = number_of_shr >= 1; + let plural = number_of_gt > 1 || number_of_shr >= 1; self.diagnostic() .struct_span_err( span, diff --git a/src/test/ui/issues/issue-54521-2.fixed b/src/test/ui/issues/issue-54521-2.fixed new file mode 100644 index 00000000000..a91c4fe43ea --- /dev/null +++ b/src/test/ui/issues/issue-54521-2.fixed @@ -0,0 +1,22 @@ +// run-rustfix + +// This test checks that the following error is emitted and the suggestion works: +// +// ``` +// let _ = Vec::>>::new(); +// ^^ help: remove extra angle brackets +// ``` + +fn main() { + let _ = Vec::::new(); + //~^ ERROR unmatched angle bracket + + let _ = Vec::::new(); + //~^ ERROR unmatched angle bracket + + let _ = Vec::::new(); + //~^ ERROR unmatched angle bracket + + let _ = Vec::::new(); + //~^ ERROR unmatched angle bracket +} diff --git a/src/test/ui/issues/issue-54521-2.rs b/src/test/ui/issues/issue-54521-2.rs new file mode 100644 index 00000000000..3639aac87ee --- /dev/null +++ b/src/test/ui/issues/issue-54521-2.rs @@ -0,0 +1,22 @@ +// run-rustfix + +// This test checks that the following error is emitted and the suggestion works: +// +// ``` +// let _ = Vec::>>::new(); +// ^^ help: remove extra angle brackets +// ``` + +fn main() { + let _ = Vec::>>>>::new(); + //~^ ERROR unmatched angle bracket + + let _ = Vec::>>>::new(); + //~^ ERROR unmatched angle bracket + + let _ = Vec::>>::new(); + //~^ ERROR unmatched angle bracket + + let _ = Vec::>::new(); + //~^ ERROR unmatched angle bracket +} diff --git a/src/test/ui/issues/issue-54521-2.stderr b/src/test/ui/issues/issue-54521-2.stderr new file mode 100644 index 00000000000..9556b83b730 --- /dev/null +++ b/src/test/ui/issues/issue-54521-2.stderr @@ -0,0 +1,26 @@ +error: unmatched angle brackets + --> $DIR/issue-54521-2.rs:11:25 + | +LL | let _ = Vec::>>>>::new(); + | ^^^^ help: remove extra angle brackets + +error: unmatched angle brackets + --> $DIR/issue-54521-2.rs:14:25 + | +LL | let _ = Vec::>>>::new(); + | ^^^ help: remove extra angle brackets + +error: unmatched angle brackets + --> $DIR/issue-54521-2.rs:17:25 + | +LL | let _ = Vec::>>::new(); + | ^^ help: remove extra angle brackets + +error: unmatched angle bracket + --> $DIR/issue-54521-2.rs:20:25 + | +LL | let _ = Vec::>::new(); + | ^ help: remove extra angle bracket + +error: aborting due to 4 previous errors + -- cgit 1.4.1-3-g733a5 From 4745b86202f0e96b4c0d0de05220a5ac4b5308ef Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Mon, 21 Jan 2019 15:28:51 -0800 Subject: 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 +-- src/test/ui/parser/removed-syntax-with-1.stderr | 11 ++--------- 3 files changed, 23 insertions(+), 13 deletions(-) (limited to 'src/libsyntax/parse') diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index a2d3595b472..745f2c7dc19 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 add024ea390..2c1e152dcee 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 aae29efa85e..a157873916a 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`. -- cgit 1.4.1-3-g733a5