From cde60e82a079f0d900b35d5f41101111bbe56d7b Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Tue, 22 Oct 2019 08:48:01 +0900 Subject: [PATCH 1/3] Add long error explanation for E0728 --- src/librustc/error_codes.rs | 73 ++++++++++++++++++- .../incorrect-syntax-suggestions.stderr | 3 +- .../ui/async-await/issues/issue-51719.stderr | 1 + .../ui/async-await/issues/issue-51751.stderr | 1 + .../async-await/issues/issue-62009-1.stderr | 3 +- .../async-await/issues/issue-62009-2.stderr | 1 + .../issues/non-async-enclosing-span.stderr | 1 + 7 files changed, 78 insertions(+), 5 deletions(-) diff --git a/src/librustc/error_codes.rs b/src/librustc/error_codes.rs index 3d501cacf6f4c..bc66beab86262 100644 --- a/src/librustc/error_codes.rs +++ b/src/librustc/error_codes.rs @@ -2045,8 +2045,8 @@ so that a generator can then be constructed: async fn bar() -> () {} async fn foo() { - bar::().await; - // ^^^^^^^^ specify type explicitly + bar::().await; + // ^^^^^^^^ specify type explicitly } ``` "##, @@ -2126,6 +2126,74 @@ static X: u32 = 42; ``` "##, +E0728: r##" +`await` has been used outside `async` function or block. + +Erroneous code examples: + +```edition2018,compile_fail,E0728 +# use std::pin::Pin; +# use std::future::Future; +# use std::task::{Context, Poll}; + +# struct WakeOnceThenComplete(bool); + +# fn wake_and_yield_once() -> WakeOnceThenComplete { +# WakeOnceThenComplete(false) +# } + +# impl Future for WakeOnceThenComplete { +# type Output = (); +# fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> { +# if self.0 { +# Poll::Ready(()) +# } else { +# cx.waker().wake_by_ref(); +# self.0 = true; +# Poll::Pending +# } +# } +# } + +fn foo() { + wake_and_yield_once().await // `await` is used outside `async` context +} +``` + +`await` is used to suspend the current computation until the given +future is ready to produce a value. So it is legal only within +an async context, like an `async fn` or an `async` block. + +```edition2018 +# use std::pin::Pin; +# use std::future::Future; +# use std::task::{Context, Poll}; + +# struct WakeOnceThenComplete(bool); + +# fn wake_and_yield_once() -> WakeOnceThenComplete { +# WakeOnceThenComplete(false) +# } + +# impl Future for WakeOnceThenComplete { +# type Output = (); +# fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> { +# if self.0 { +# Poll::Ready(()) +# } else { +# cx.waker().wake_by_ref(); +# self.0 = true; +# Poll::Pending +# } +# } +# } + +async fn foo() { + wake_and_yield_once().await // `await` is used within `async` context +} +``` +"##, + E0734: r##" A stability attribute has been used outside of the standard library. @@ -2218,6 +2286,5 @@ See [RFC 2091] for details on this and other limitations. // E0702, // replaced with a generic attribute input check E0726, // non-explicit (not `'_`) elided lifetime in unsupported position E0727, // `async` generators are not yet supported - E0728, // `await` must be in an `async` function or block E0739, // invalid track_caller application/syntax } diff --git a/src/test/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr b/src/test/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr index 7caa9f26bc2f8..4b5e2d59e38c9 100644 --- a/src/test/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr +++ b/src/test/ui/async-await/await-keyword/incorrect-syntax-suggestions.stderr @@ -244,4 +244,5 @@ LL | let _ = await bar()?; error: aborting due to 35 previous errors -For more information about this error, try `rustc --explain E0277`. +Some errors have detailed explanations: E0277, E0728. +For more information about an error, try `rustc --explain E0277`. diff --git a/src/test/ui/async-await/issues/issue-51719.stderr b/src/test/ui/async-await/issues/issue-51719.stderr index 6c3c8889da7ce..5b9adb253d968 100644 --- a/src/test/ui/async-await/issues/issue-51719.stderr +++ b/src/test/ui/async-await/issues/issue-51719.stderr @@ -8,3 +8,4 @@ LL | let _gen = || foo().await; error: aborting due to previous error +For more information about this error, try `rustc --explain E0728`. diff --git a/src/test/ui/async-await/issues/issue-51751.stderr b/src/test/ui/async-await/issues/issue-51751.stderr index e50c78534f852..f120bd119c540 100644 --- a/src/test/ui/async-await/issues/issue-51751.stderr +++ b/src/test/ui/async-await/issues/issue-51751.stderr @@ -9,3 +9,4 @@ LL | let finished = result.await; error: aborting due to previous error +For more information about this error, try `rustc --explain E0728`. diff --git a/src/test/ui/async-await/issues/issue-62009-1.stderr b/src/test/ui/async-await/issues/issue-62009-1.stderr index f63eaa4c48a97..538430290d299 100644 --- a/src/test/ui/async-await/issues/issue-62009-1.stderr +++ b/src/test/ui/async-await/issues/issue-62009-1.stderr @@ -40,4 +40,5 @@ LL | F: Future error: aborting due to 4 previous errors -For more information about this error, try `rustc --explain E0277`. +Some errors have detailed explanations: E0277, E0728. +For more information about an error, try `rustc --explain E0277`. diff --git a/src/test/ui/async-await/issues/issue-62009-2.stderr b/src/test/ui/async-await/issues/issue-62009-2.stderr index 79b6803263eec..47b74b5574fea 100644 --- a/src/test/ui/async-await/issues/issue-62009-2.stderr +++ b/src/test/ui/async-await/issues/issue-62009-2.stderr @@ -8,3 +8,4 @@ LL | (async || 2333)().await; error: aborting due to previous error +For more information about this error, try `rustc --explain E0728`. diff --git a/src/test/ui/async-await/issues/non-async-enclosing-span.stderr b/src/test/ui/async-await/issues/non-async-enclosing-span.stderr index 49ebf414c550b..f826a86f08985 100644 --- a/src/test/ui/async-await/issues/non-async-enclosing-span.stderr +++ b/src/test/ui/async-await/issues/non-async-enclosing-span.stderr @@ -9,3 +9,4 @@ LL | let y = do_the_thing().await; error: aborting due to previous error +For more information about this error, try `rustc --explain E0728`. From 4fcc784c9ad6748ab9a5754e75c38281d4e7a249 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Tue, 22 Oct 2019 09:00:01 +0900 Subject: [PATCH 2/3] Apply suggestions --- src/librustc/error_codes.rs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/librustc/error_codes.rs b/src/librustc/error_codes.rs index bc66beab86262..4ee3445235ee9 100644 --- a/src/librustc/error_codes.rs +++ b/src/librustc/error_codes.rs @@ -2135,13 +2135,13 @@ Erroneous code examples: # use std::pin::Pin; # use std::future::Future; # use std::task::{Context, Poll}; - +# # struct WakeOnceThenComplete(bool); - +# # fn wake_and_yield_once() -> WakeOnceThenComplete { # WakeOnceThenComplete(false) # } - +# # impl Future for WakeOnceThenComplete { # type Output = (); # fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> { @@ -2154,7 +2154,7 @@ Erroneous code examples: # } # } # } - +# fn foo() { wake_and_yield_once().await // `await` is used outside `async` context } @@ -2168,13 +2168,13 @@ an async context, like an `async fn` or an `async` block. # use std::pin::Pin; # use std::future::Future; # use std::task::{Context, Poll}; - +# # struct WakeOnceThenComplete(bool); - +# # fn wake_and_yield_once() -> WakeOnceThenComplete { # WakeOnceThenComplete(false) # } - +# # impl Future for WakeOnceThenComplete { # type Output = (); # fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> { @@ -2187,9 +2187,16 @@ an async context, like an `async fn` or an `async` block. # } # } # } - +# async fn foo() { - wake_and_yield_once().await // `await` is used within `async` context + wake_and_yield_once().await // `await` is used within `async` function +} + +fn bar(x: u8) -> impl Future { + async move { + wake_and_yield_once().await; // `await` is used within `async` block + x + } } ``` "##, From a1f65895a071885141061590e24022eacec6e858 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Tue, 22 Oct 2019 21:10:51 +0900 Subject: [PATCH 3/3] Add link to async/await --- src/librustc/error_codes.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/librustc/error_codes.rs b/src/librustc/error_codes.rs index 4ee3445235ee9..122ae4a6cf66f 100644 --- a/src/librustc/error_codes.rs +++ b/src/librustc/error_codes.rs @@ -2127,7 +2127,7 @@ static X: u32 = 42; "##, E0728: r##" -`await` has been used outside `async` function or block. +[`await`] has been used outside [`async`] function or block. Erroneous code examples: @@ -2160,9 +2160,9 @@ fn foo() { } ``` -`await` is used to suspend the current computation until the given +[`await`] is used to suspend the current computation until the given future is ready to produce a value. So it is legal only within -an async context, like an `async fn` or an `async` block. +an [`async`] context, like an `async fn` or an `async` block. ```edition2018 # use std::pin::Pin; @@ -2199,6 +2199,9 @@ fn bar(x: u8) -> impl Future { } } ``` + +[`async`]: https://doc.rust-lang.org/std/keyword.async.html +[`await`]: https://doc.rust-lang.org/std/keyword.await.html "##, E0734: r##"