From 157c67b7a8c47de44b47eed9611e5a97981553eb Mon Sep 17 00:00:00 2001 From: Esteban Kuber Date: Sun, 27 Mar 2022 06:05:18 +0000 Subject: [PATCH] Handle `,` to `;` substitution in arg params --- compiler/rustc_parse/src/parser/path.rs | 17 +++++++++++++++++ src/test/ui/parser/lifetime-semicolon.fixed | 10 ++++++++++ src/test/ui/parser/lifetime-semicolon.rs | 4 +++- src/test/ui/parser/lifetime-semicolon.stderr | 12 ++++++------ 4 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 src/test/ui/parser/lifetime-semicolon.fixed diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs index 596099bf2de..93663a349f5 100644 --- a/compiler/rustc_parse/src/parser/path.rs +++ b/compiler/rustc_parse/src/parser/path.rs @@ -478,6 +478,23 @@ impl<'a> Parser<'a> { while let Some(arg) = self.parse_angle_arg(ty_generics)? { args.push(arg); if !self.eat(&token::Comma) { + if self.token.kind == token::Semi + && self.look_ahead(1, |t| t.is_ident() || t.is_lifetime()) + { + // Add `>` to the list of expected tokens. + self.check(&token::Gt); + // Handle `,` to `;` substitution + let mut err = self.unexpected::<()>().unwrap_err(); + self.bump(); + err.span_suggestion_verbose( + self.prev_token.span.until(self.token.span), + "use a comma to separate type parameters", + ", ".to_string(), + Applicability::MachineApplicable, + ); + err.emit(); + continue; + } if !self.token.kind.should_end_const_arg() { if self.handle_ambiguous_unbraced_const_arg(&mut args)? { // We've managed to (partially) recover, so continue trying to parse diff --git a/src/test/ui/parser/lifetime-semicolon.fixed b/src/test/ui/parser/lifetime-semicolon.fixed new file mode 100644 index 00000000000..89e87fe9988 --- /dev/null +++ b/src/test/ui/parser/lifetime-semicolon.fixed @@ -0,0 +1,10 @@ +// run-rustfix +#![allow(unused)] +struct Foo<'a, 'b> { + a: &'a &'b i32 +} + +fn foo<'a, 'b>(_x: &mut Foo<'a, 'b>) {} +//~^ ERROR expected one of `,`, `:`, `=`, or `>`, found `;` + +fn main() {} diff --git a/src/test/ui/parser/lifetime-semicolon.rs b/src/test/ui/parser/lifetime-semicolon.rs index 7cc14971f63..744c93fc7c7 100644 --- a/src/test/ui/parser/lifetime-semicolon.rs +++ b/src/test/ui/parser/lifetime-semicolon.rs @@ -1,8 +1,10 @@ +// run-rustfix +#![allow(unused)] struct Foo<'a, 'b> { a: &'a &'b i32 } -fn foo<'a, 'b>(x: &mut Foo<'a; 'b>) {} +fn foo<'a, 'b>(_x: &mut Foo<'a; 'b>) {} //~^ ERROR expected one of `,`, `:`, `=`, or `>`, found `;` fn main() {} diff --git a/src/test/ui/parser/lifetime-semicolon.stderr b/src/test/ui/parser/lifetime-semicolon.stderr index 948f9655e1e..5de7a5f2d5d 100644 --- a/src/test/ui/parser/lifetime-semicolon.stderr +++ b/src/test/ui/parser/lifetime-semicolon.stderr @@ -1,13 +1,13 @@ error: expected one of `,`, `:`, `=`, or `>`, found `;` - --> $DIR/lifetime-semicolon.rs:5:30 + --> $DIR/lifetime-semicolon.rs:7:31 | -LL | fn foo<'a, 'b>(x: &mut Foo<'a; 'b>) {} - | ^ expected one of `,`, `:`, `=`, or `>` +LL | fn foo<'a, 'b>(_x: &mut Foo<'a; 'b>) {} + | ^ expected one of `,`, `:`, `=`, or `>` | -help: you might have meant to end the type parameters here +help: use a comma to separate type parameters | -LL | fn foo<'a, 'b>(x: &mut Foo<'a>; 'b>) {} - | + +LL | fn foo<'a, 'b>(_x: &mut Foo<'a, 'b>) {} + | ~ error: aborting due to previous error