From 6d256d9d0ec3a42084ae0675139680e71657aeeb Mon Sep 17 00:00:00 2001 From: darklyspaced Date: Mon, 7 Aug 2023 22:10:21 +0800 Subject: [PATCH 1/6] test infra added --- tests/ui/parser/issue-113203.rs | 8 ++++++++ tests/ui/parser/issue-113203.stderr | 30 +++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/ui/parser/issue-113203.rs create mode 100644 tests/ui/parser/issue-113203.stderr diff --git a/tests/ui/parser/issue-113203.rs b/tests/ui/parser/issue-113203.rs new file mode 100644 index 0000000000000..eeefd941da453 --- /dev/null +++ b/tests/ui/parser/issue-113203.rs @@ -0,0 +1,8 @@ +// Checks what happens when we attempt to use the await keyword as a prefix. Span +// incorrectly emitted an `.await` in E0277 which does not exist +// edition:2018 +fn main() { + await {}() + //~^ ERROR `await` is only allowed inside `async` functions and blocks + //~| ERROR incorrect use of `await` +} diff --git a/tests/ui/parser/issue-113203.stderr b/tests/ui/parser/issue-113203.stderr new file mode 100644 index 0000000000000..f205f4addcdd2 --- /dev/null +++ b/tests/ui/parser/issue-113203.stderr @@ -0,0 +1,30 @@ +error: incorrect use of `await` + --> $DIR/issue-113203.rs:5:5 + | +LL | await {}() + | ^^^^^^^^ help: `await` is a postfix operation: `{}.await` + +error[E0728]: `await` is only allowed inside `async` functions and blocks + --> $DIR/issue-113203.rs:5:5 + | +LL | fn main() { + | ---- this is not `async` +LL | await {}() + | ^^^^^ only allowed inside `async` functions and blocks + +error[E0277]: `()` is not a future + --> $DIR/issue-113203.rs:5:5 + | +LL | await {}() + | ^^^^^ - help: remove the `.await` + | | + | `()` is not a future + | + = help: the trait `Future` is not implemented for `()` + = note: () must be a future or must implement `IntoFuture` to be awaited + = note: required for `()` to implement `IntoFuture` + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0277, E0728. +For more information about an error, try `rustc --explain E0277`. From 9ed5267e61b9f39e729807bd7d37858243257e24 Mon Sep 17 00:00:00 2001 From: darklyspaced Date: Mon, 7 Aug 2023 22:31:32 +0800 Subject: [PATCH 2/6] fix tests --- tests/ui/parser/issue-113203.rs | 3 +-- tests/ui/parser/issue-113203.stderr | 24 +----------------------- 2 files changed, 2 insertions(+), 25 deletions(-) diff --git a/tests/ui/parser/issue-113203.rs b/tests/ui/parser/issue-113203.rs index eeefd941da453..1103251c14038 100644 --- a/tests/ui/parser/issue-113203.rs +++ b/tests/ui/parser/issue-113203.rs @@ -3,6 +3,5 @@ // edition:2018 fn main() { await {}() - //~^ ERROR `await` is only allowed inside `async` functions and blocks - //~| ERROR incorrect use of `await` + //~^ ERROR incorrect use of `await` } diff --git a/tests/ui/parser/issue-113203.stderr b/tests/ui/parser/issue-113203.stderr index f205f4addcdd2..97304a89c9e49 100644 --- a/tests/ui/parser/issue-113203.stderr +++ b/tests/ui/parser/issue-113203.stderr @@ -4,27 +4,5 @@ error: incorrect use of `await` LL | await {}() | ^^^^^^^^ help: `await` is a postfix operation: `{}.await` -error[E0728]: `await` is only allowed inside `async` functions and blocks - --> $DIR/issue-113203.rs:5:5 - | -LL | fn main() { - | ---- this is not `async` -LL | await {}() - | ^^^^^ only allowed inside `async` functions and blocks - -error[E0277]: `()` is not a future - --> $DIR/issue-113203.rs:5:5 - | -LL | await {}() - | ^^^^^ - help: remove the `.await` - | | - | `()` is not a future - | - = help: the trait `Future` is not implemented for `()` - = note: () must be a future or must implement `IntoFuture` to be awaited - = note: required for `()` to implement `IntoFuture` - -error: aborting due to 3 previous errors +error: aborting due to previous error -Some errors have detailed explanations: E0277, E0728. -For more information about an error, try `rustc --explain E0277`. From 13ac0234c6fa9335741084f1dd66b98ffb938e78 Mon Sep 17 00:00:00 2001 From: darklyspaced Date: Mon, 7 Aug 2023 22:32:28 +0800 Subject: [PATCH 3/6] always return ExprKind::Err --- compiler/rustc_parse/src/parser/diagnostics.rs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 4e639a54cf7c9..f5896c71fbc85 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1717,12 +1717,7 @@ impl<'a> Parser<'a> { self.recover_await_prefix(await_sp)? }; let sp = self.error_on_incorrect_await(lo, hi, &expr, is_question); - let kind = match expr.kind { - // Avoid knock-down errors as we don't know whether to interpret this as `foo().await?` - // or `foo()?.await` (the very reason we went with postfix syntax 😅). - ExprKind::Try(_) => ExprKind::Err, - _ => ExprKind::Await(expr, await_sp), - }; + let kind = ExprKind::Err; let expr = self.mk_expr(lo.to(sp), kind); self.maybe_recover_from_bad_qpath(expr) } From 7c3a8aeea55816455108ee21ffddf99441437cec Mon Sep 17 00:00:00 2001 From: darklyspaced Date: Mon, 7 Aug 2023 22:40:09 +0800 Subject: [PATCH 4/6] relocate tests to pass tidy --- tests/ui/parser/{ => issues}/issue-113203.rs | 0 tests/ui/parser/{ => issues}/issue-113203.stderr | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename tests/ui/parser/{ => issues}/issue-113203.rs (100%) rename tests/ui/parser/{ => issues}/issue-113203.stderr (100%) diff --git a/tests/ui/parser/issue-113203.rs b/tests/ui/parser/issues/issue-113203.rs similarity index 100% rename from tests/ui/parser/issue-113203.rs rename to tests/ui/parser/issues/issue-113203.rs diff --git a/tests/ui/parser/issue-113203.stderr b/tests/ui/parser/issues/issue-113203.stderr similarity index 100% rename from tests/ui/parser/issue-113203.stderr rename to tests/ui/parser/issues/issue-113203.stderr From 3aa0411c3c21ee99f88df8aa4d19c74d99f11b7c Mon Sep 17 00:00:00 2001 From: darklyspaced Date: Tue, 8 Aug 2023 10:51:35 +0800 Subject: [PATCH 5/6] blessed the tests --- .../incorrect-syntax-suggestions.rs | 11 +-- .../incorrect-syntax-suggestions.stderr | 78 +++++-------------- 2 files changed, 22 insertions(+), 67 deletions(-) diff --git a/tests/ui/async-await/await-keyword/incorrect-syntax-suggestions.rs b/tests/ui/async-await/await-keyword/incorrect-syntax-suggestions.rs index 554ac673d5155..ab675d0a1a609 100644 --- a/tests/ui/async-await/await-keyword/incorrect-syntax-suggestions.rs +++ b/tests/ui/async-await/await-keyword/incorrect-syntax-suggestions.rs @@ -49,13 +49,11 @@ async fn foo8() -> Result<(), ()> { Ok(()) } fn foo9() -> Result<(), ()> { - let _ = await bar(); //~ ERROR `await` is only allowed inside `async` functions and blocks - //~^ ERROR incorrect use of `await` + let _ = await bar(); //~ ERROR incorrect use of `await` Ok(()) } fn foo10() -> Result<(), ()> { - let _ = await? bar(); //~ ERROR `await` is only allowed inside `async` functions and blocks - //~^ ERROR incorrect use of `await` + let _ = await? bar(); //~ ERROR incorrect use of `await` Ok(()) } fn foo11() -> Result<(), ()> { @@ -63,8 +61,7 @@ fn foo11() -> Result<(), ()> { Ok(()) } fn foo12() -> Result<(), ()> { - let _ = (await bar())?; //~ ERROR `await` is only allowed inside `async` functions and blocks - //~^ ERROR incorrect use of `await` + let _ = (await bar())?; //~ ERROR incorrect use of `await` Ok(()) } fn foo13() -> Result<(), ()> { @@ -111,7 +108,6 @@ async fn foo27() -> Result<(), ()> { fn foo28() -> Result<(), ()> { fn foo() -> Result<(), ()> { let _ = await!(bar())?; //~ ERROR incorrect use of `await` - //~^ ERROR `await` is only allowed inside `async` functions Ok(()) } foo() @@ -119,7 +115,6 @@ fn foo28() -> Result<(), ()> { fn foo29() -> Result<(), ()> { let foo = || { let _ = await!(bar())?; //~ ERROR incorrect use of `await` - //~^ ERROR `await` is only allowed inside `async` functions Ok(()) }; foo() diff --git a/tests/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr b/tests/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr index 7b03e56662a74..928eb0b821db1 100644 --- a/tests/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr +++ b/tests/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr @@ -59,61 +59,61 @@ LL | let _ = await bar(); | ^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await` error: incorrect use of `await` - --> $DIR/incorrect-syntax-suggestions.rs:57:13 + --> $DIR/incorrect-syntax-suggestions.rs:56:13 | LL | let _ = await? bar(); | ^^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await?` error: incorrect use of `await` - --> $DIR/incorrect-syntax-suggestions.rs:62:13 + --> $DIR/incorrect-syntax-suggestions.rs:60:13 | LL | let _ = await bar()?; | ^^^^^^^^^^^^ help: `await` is a postfix operation: `bar()?.await` error: incorrect use of `await` - --> $DIR/incorrect-syntax-suggestions.rs:66:14 + --> $DIR/incorrect-syntax-suggestions.rs:64:14 | LL | let _ = (await bar())?; | ^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await` error: incorrect use of `await` - --> $DIR/incorrect-syntax-suggestions.rs:71:24 + --> $DIR/incorrect-syntax-suggestions.rs:68:24 | LL | let _ = bar().await(); | ^^ help: `await` is not a method call, remove the parentheses error: incorrect use of `await` - --> $DIR/incorrect-syntax-suggestions.rs:76:24 + --> $DIR/incorrect-syntax-suggestions.rs:73:24 | LL | let _ = bar().await()?; | ^^ help: `await` is not a method call, remove the parentheses error: incorrect use of `await` - --> $DIR/incorrect-syntax-suggestions.rs:104:13 + --> $DIR/incorrect-syntax-suggestions.rs:101:13 | LL | let _ = await!(bar()); | ^^^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await` error: incorrect use of `await` - --> $DIR/incorrect-syntax-suggestions.rs:108:13 + --> $DIR/incorrect-syntax-suggestions.rs:105:13 | LL | let _ = await!(bar())?; | ^^^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await` error: incorrect use of `await` - --> $DIR/incorrect-syntax-suggestions.rs:113:17 + --> $DIR/incorrect-syntax-suggestions.rs:110:17 | LL | let _ = await!(bar())?; | ^^^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await` error: incorrect use of `await` - --> $DIR/incorrect-syntax-suggestions.rs:121:17 + --> $DIR/incorrect-syntax-suggestions.rs:117:17 | LL | let _ = await!(bar())?; | ^^^^^^^^^^^^^ help: `await` is a postfix operation: `bar().await` error: expected expression, found `=>` - --> $DIR/incorrect-syntax-suggestions.rs:129:25 + --> $DIR/incorrect-syntax-suggestions.rs:124:25 | LL | match await { await => () } | ----- ^^ expected expression @@ -121,13 +121,13 @@ LL | match await { await => () } | while parsing this incorrect await expression error: incorrect use of `await` - --> $DIR/incorrect-syntax-suggestions.rs:129:11 + --> $DIR/incorrect-syntax-suggestions.rs:124:11 | LL | match await { await => () } | ^^^^^^^^^^^^^^^^^^^^^ help: `await` is a postfix operation: `{ await => () }.await` error: expected one of `.`, `?`, `{`, or an operator, found `}` - --> $DIR/incorrect-syntax-suggestions.rs:132:1 + --> $DIR/incorrect-syntax-suggestions.rs:127:1 | LL | match await { await => () } | ----- - expected one of `.`, `?`, `{`, or an operator @@ -138,31 +138,7 @@ LL | } | ^ unexpected token error[E0728]: `await` is only allowed inside `async` functions and blocks - --> $DIR/incorrect-syntax-suggestions.rs:52:13 - | -LL | fn foo9() -> Result<(), ()> { - | ---- this is not `async` -LL | let _ = await bar(); - | ^^^^^ only allowed inside `async` functions and blocks - -error[E0728]: `await` is only allowed inside `async` functions and blocks - --> $DIR/incorrect-syntax-suggestions.rs:57:13 - | -LL | fn foo10() -> Result<(), ()> { - | ----- this is not `async` -LL | let _ = await? bar(); - | ^^^^^ only allowed inside `async` functions and blocks - -error[E0728]: `await` is only allowed inside `async` functions and blocks - --> $DIR/incorrect-syntax-suggestions.rs:66:14 - | -LL | fn foo12() -> Result<(), ()> { - | ----- this is not `async` -LL | let _ = (await bar())?; - | ^^^^^ only allowed inside `async` functions and blocks - -error[E0728]: `await` is only allowed inside `async` functions and blocks - --> $DIR/incorrect-syntax-suggestions.rs:71:19 + --> $DIR/incorrect-syntax-suggestions.rs:68:19 | LL | fn foo13() -> Result<(), ()> { | ----- this is not `async` @@ -170,7 +146,7 @@ LL | let _ = bar().await(); | ^^^^^ only allowed inside `async` functions and blocks error[E0728]: `await` is only allowed inside `async` functions and blocks - --> $DIR/incorrect-syntax-suggestions.rs:76:19 + --> $DIR/incorrect-syntax-suggestions.rs:73:19 | LL | fn foo14() -> Result<(), ()> { | ----- this is not `async` @@ -178,7 +154,7 @@ LL | let _ = bar().await()?; | ^^^^^ only allowed inside `async` functions and blocks error[E0728]: `await` is only allowed inside `async` functions and blocks - --> $DIR/incorrect-syntax-suggestions.rs:81:19 + --> $DIR/incorrect-syntax-suggestions.rs:78:19 | LL | fn foo15() -> Result<(), ()> { | ----- this is not `async` @@ -186,7 +162,7 @@ LL | let _ = bar().await; | ^^^^^ only allowed inside `async` functions and blocks error[E0728]: `await` is only allowed inside `async` functions and blocks - --> $DIR/incorrect-syntax-suggestions.rs:85:19 + --> $DIR/incorrect-syntax-suggestions.rs:82:19 | LL | fn foo16() -> Result<(), ()> { | ----- this is not `async` @@ -194,7 +170,7 @@ LL | let _ = bar().await?; | ^^^^^ only allowed inside `async` functions and blocks error[E0728]: `await` is only allowed inside `async` functions and blocks - --> $DIR/incorrect-syntax-suggestions.rs:90:23 + --> $DIR/incorrect-syntax-suggestions.rs:87:23 | LL | fn foo() -> Result<(), ()> { | --- this is not `async` @@ -202,29 +178,13 @@ LL | let _ = bar().await?; | ^^^^^ only allowed inside `async` functions and blocks error[E0728]: `await` is only allowed inside `async` functions and blocks - --> $DIR/incorrect-syntax-suggestions.rs:97:23 + --> $DIR/incorrect-syntax-suggestions.rs:94:23 | LL | let foo = || { | -- this is not `async` LL | let _ = bar().await?; | ^^^^^ only allowed inside `async` functions and blocks -error[E0728]: `await` is only allowed inside `async` functions and blocks - --> $DIR/incorrect-syntax-suggestions.rs:113:17 - | -LL | fn foo() -> Result<(), ()> { - | --- this is not `async` -LL | let _ = await!(bar())?; - | ^^^^^ only allowed inside `async` functions and blocks - -error[E0728]: `await` is only allowed inside `async` functions and blocks - --> $DIR/incorrect-syntax-suggestions.rs:121:17 - | -LL | let foo = || { - | -- this is not `async` -LL | let _ = await!(bar())?; - | ^^^^^ only allowed inside `async` functions and blocks - -error: aborting due to 33 previous errors +error: aborting due to 28 previous errors For more information about this error, try `rustc --explain E0728`. From 89284af8fe412a6eaa46d92423762d1fcd1161fa Mon Sep 17 00:00:00 2001 From: darklyspaced Date: Tue, 8 Aug 2023 10:59:15 +0800 Subject: [PATCH 6/6] inlined kind --- compiler/rustc_parse/src/parser/diagnostics.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index f5896c71fbc85..6c8ef34063f87 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -1717,8 +1717,7 @@ impl<'a> Parser<'a> { self.recover_await_prefix(await_sp)? }; let sp = self.error_on_incorrect_await(lo, hi, &expr, is_question); - let kind = ExprKind::Err; - let expr = self.mk_expr(lo.to(sp), kind); + let expr = self.mk_expr(lo.to(sp), ExprKind::Err); self.maybe_recover_from_bad_qpath(expr) }