From 611ab684e109cf4d10e9989460cd50aaf6d1ef9a Mon Sep 17 00:00:00 2001 From: Lukas Markeffsky <@> Date: Sun, 19 Feb 2023 21:15:49 +0100 Subject: [PATCH] use span of semicolon for eager recovery in expression instead of the span of the token after the semicolon --- compiler/rustc_parse/src/parser/expr.rs | 2 +- .../issue-108242-semicolon-recovery.rs | 6 ++ .../issue-108242-semicolon-recovery.stderr | 62 +++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 tests/ui/argument-suggestions/issue-108242-semicolon-recovery.rs create mode 100644 tests/ui/argument-suggestions/issue-108242-semicolon-recovery.stderr diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index 12f65a436e3be..6e4612eee28a7 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -1389,7 +1389,7 @@ impl<'a> Parser<'a> { // 2 | foo(bar(; // | ^ expected expression self.bump(); - Ok(self.mk_expr_err(self.token.span)) + Ok(self.mk_expr_err(self.prev_token.span)) } else if self.token.uninterpolated_span().rust_2018() { // `Span::rust_2018()` is somewhat expensive; don't get it repeatedly. if self.check_keyword(kw::Async) { diff --git a/tests/ui/argument-suggestions/issue-108242-semicolon-recovery.rs b/tests/ui/argument-suggestions/issue-108242-semicolon-recovery.rs new file mode 100644 index 0000000000000..7cab377ea251a --- /dev/null +++ b/tests/ui/argument-suggestions/issue-108242-semicolon-recovery.rs @@ -0,0 +1,6 @@ +fn foo() {} +fn main() { + foo(; //~ ERROR this function takes 0 arguments but 2 arguments were supplied + foo(; //~ ERROR this function takes 0 arguments but 1 argument was supplied + //~^ ERROR expected one of +} //~ ERROR mismatched closing delimiter diff --git a/tests/ui/argument-suggestions/issue-108242-semicolon-recovery.stderr b/tests/ui/argument-suggestions/issue-108242-semicolon-recovery.stderr new file mode 100644 index 0000000000000..2ed8fd3ea8bbc --- /dev/null +++ b/tests/ui/argument-suggestions/issue-108242-semicolon-recovery.stderr @@ -0,0 +1,62 @@ +error: expected one of `)`, `,`, `.`, `?`, or an operator, found `foo` + --> $DIR/issue-108242-semicolon-recovery.rs:4:5 + | +LL | foo(; + | - + | | + | expected one of `)`, `,`, `.`, `?`, or an operator + | help: missing `,` +LL | foo(; + | ^^^ unexpected token + +error: mismatched closing delimiter: `}` + --> $DIR/issue-108242-semicolon-recovery.rs:4:8 + | +LL | fn main() { + | - closing delimiter possibly meant for this +LL | foo(; +LL | foo(; + | ^ unclosed delimiter +LL | +LL | } + | ^ mismatched closing delimiter + +error[E0061]: this function takes 0 arguments but 1 argument was supplied + --> $DIR/issue-108242-semicolon-recovery.rs:4:5 + | +LL | foo(; + | ^^^ - + | | + | unexpected argument + | help: remove the extra argument + | +note: function defined here + --> $DIR/issue-108242-semicolon-recovery.rs:1:4 + | +LL | fn foo() {} + | ^^^ + +error[E0061]: this function takes 0 arguments but 2 arguments were supplied + --> $DIR/issue-108242-semicolon-recovery.rs:3:5 + | +LL | foo(; + | ^^^ - unexpected argument +LL | / foo(; +LL | | +LL | | } + | |_- unexpected argument of type `()` + | +note: function defined here + --> $DIR/issue-108242-semicolon-recovery.rs:1:4 + | +LL | fn foo() {} + | ^^^ +help: remove the extra arguments + | +LL - foo(; +LL + foo( + | + +error: aborting due to 4 previous errors + +For more information about this error, try `rustc --explain E0061`.