From 86df9039b2a49dde3a2453d75c4c58540a568a1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 21 Oct 2020 14:25:09 -0700 Subject: [PATCH] Tweak "use `.await`" suggestion --- .../src/traits/error_reporting/suggestions.rs | 10 +++++----- .../src/check/fn_ctxt/suggestions.rs | 16 ++++++---------- .../incorrect-syntax-suggestions.stderr | 9 +++++---- src/test/ui/async-await/issue-61076.stderr | 18 ++++++++++-------- .../suggest-missing-await-closure.fixed | 4 ++-- .../suggest-missing-await-closure.rs | 4 ++-- .../suggest-missing-await-closure.stderr | 9 +++++---- .../ui/async-await/suggest-missing-await.fixed | 8 ++++---- .../ui/async-await/suggest-missing-await.rs | 8 ++++---- .../async-await/suggest-missing-await.stderr | 13 +++++++------ src/test/ui/suggestions/issue-72766.stderr | 9 +++++---- 11 files changed, 55 insertions(+), 53 deletions(-) diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs index efa9bd633ba8c..fa837e04db35e 100644 --- a/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs +++ b/compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs @@ -21,7 +21,7 @@ use rustc_middle::ty::{ }; use rustc_middle::ty::{TypeAndMut, TypeckResults}; use rustc_span::symbol::{kw, sym, Ident, Symbol}; -use rustc_span::{MultiSpan, Span, DUMMY_SP}; +use rustc_span::{BytePos, MultiSpan, Span, DUMMY_SP}; use rustc_target::spec::abi; use std::fmt; @@ -2114,10 +2114,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> { if self.predicate_may_hold(&try_obligation) && impls_future { if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) { if snippet.ends_with('?') { - err.span_suggestion( - span, - "consider using `.await` here", - format!("{}.await?", snippet.trim_end_matches('?')), + err.span_suggestion_verbose( + span.with_hi(span.hi() - BytePos(1)).shrink_to_hi(), + "consider `await`ing on the `Future`", + ".await".to_string(), Applicability::MaybeIncorrect, ); } diff --git a/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs b/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs index 9bad02c41b4b1..18db8d63850b2 100644 --- a/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs +++ b/compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs @@ -497,16 +497,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { if self.infcx.predicate_may_hold(&obligation) { debug!("suggest_missing_await: obligation held: {:?}", obligation); - if let Ok(code) = self.sess().source_map().span_to_snippet(sp) { - err.span_suggestion( - sp, - "consider using `.await` here", - format!("{}.await", code), - Applicability::MaybeIncorrect, - ); - } else { - debug!("suggest_missing_await: no snippet for {:?}", sp); - } + err.span_suggestion_verbose( + sp.shrink_to_hi(), + "consider `await`ing on the `Future`", + ".await".to_string(), + Applicability::MaybeIncorrect, + ); } else { debug!("suggest_missing_await: obligation did not hold: {:?}", obligation) } 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 6a653fc060b1d..d90ce0f1bb482 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 @@ -237,13 +237,14 @@ error[E0277]: the `?` operator can only be applied to values that implement `Try --> $DIR/incorrect-syntax-suggestions.rs:16:19 | LL | let _ = await bar()?; - | ^^^^^^ - | | - | the `?` operator cannot be applied to type `impl Future` - | help: consider using `.await` here: `bar().await?` + | ^^^^^^ the `?` operator cannot be applied to type `impl Future` | = help: the trait `Try` is not implemented for `impl Future` = note: required by `into_result` +help: consider `await`ing on the `Future` + | +LL | let _ = await bar().await?; + | ^^^^^^ error[E0277]: the `?` operator can only be applied to values that implement `Try` --> $DIR/incorrect-syntax-suggestions.rs:63:19 diff --git a/src/test/ui/async-await/issue-61076.stderr b/src/test/ui/async-await/issue-61076.stderr index 88ea7251eaf1f..afba889f014fe 100644 --- a/src/test/ui/async-await/issue-61076.stderr +++ b/src/test/ui/async-await/issue-61076.stderr @@ -2,25 +2,27 @@ error[E0277]: the `?` operator can only be applied to values that implement `Try --> $DIR/issue-61076.rs:42:5 | LL | foo()?; - | ^^^^^^ - | | - | the `?` operator cannot be applied to type `impl Future` - | help: consider using `.await` here: `foo().await?` + | ^^^^^^ the `?` operator cannot be applied to type `impl Future` | = help: the trait `Try` is not implemented for `impl Future` = note: required by `into_result` +help: consider `await`ing on the `Future` + | +LL | foo().await?; + | ^^^^^^ error[E0277]: the `?` operator can only be applied to values that implement `Try` --> $DIR/issue-61076.rs:56:5 | LL | t?; - | ^^ - | | - | the `?` operator cannot be applied to type `T` - | help: consider using `.await` here: `t.await?` + | ^^ the `?` operator cannot be applied to type `T` | = help: the trait `Try` is not implemented for `T` = note: required by `into_result` +help: consider `await`ing on the `Future` + | +LL | t.await?; + | ^^^^^^ error[E0609]: no field `0` on type `impl Future` --> $DIR/issue-61076.rs:58:26 diff --git a/src/test/ui/async-await/suggest-missing-await-closure.fixed b/src/test/ui/async-await/suggest-missing-await-closure.fixed index 37b30ffe6800f..febcd02184261 100644 --- a/src/test/ui/async-await/suggest-missing-await-closure.fixed +++ b/src/test/ui/async-await/suggest-missing-await-closure.fixed @@ -15,8 +15,8 @@ async fn suggest_await_in_async_closure() { let x = make_u32(); take_u32(x.await) //~^ ERROR mismatched types [E0308] - //~| HELP consider using `.await` here - //~| SUGGESTION x.await + //~| HELP consider `await`ing on the `Future` + //~| SUGGESTION .await }; } diff --git a/src/test/ui/async-await/suggest-missing-await-closure.rs b/src/test/ui/async-await/suggest-missing-await-closure.rs index 18076a1516171..faabf6ee3f16f 100644 --- a/src/test/ui/async-await/suggest-missing-await-closure.rs +++ b/src/test/ui/async-await/suggest-missing-await-closure.rs @@ -15,8 +15,8 @@ async fn suggest_await_in_async_closure() { let x = make_u32(); take_u32(x) //~^ ERROR mismatched types [E0308] - //~| HELP consider using `.await` here - //~| SUGGESTION x.await + //~| HELP consider `await`ing on the `Future` + //~| SUGGESTION .await }; } diff --git a/src/test/ui/async-await/suggest-missing-await-closure.stderr b/src/test/ui/async-await/suggest-missing-await-closure.stderr index ed2c4cbfccc98..2151057aa7fc0 100644 --- a/src/test/ui/async-await/suggest-missing-await-closure.stderr +++ b/src/test/ui/async-await/suggest-missing-await-closure.stderr @@ -5,13 +5,14 @@ LL | async fn make_u32() -> u32 { | --- the `Output` of this `async fn`'s found opaque type ... LL | take_u32(x) - | ^ - | | - | expected `u32`, found opaque type - | help: consider using `.await` here: `x.await` + | ^ expected `u32`, found opaque type | = note: expected type `u32` found opaque type `impl Future` +help: consider `await`ing on the `Future` + | +LL | take_u32(x.await) + | ^^^^^^ error: aborting due to previous error diff --git a/src/test/ui/async-await/suggest-missing-await.fixed b/src/test/ui/async-await/suggest-missing-await.fixed index 1ec59d906206e..e548fda7cb4f5 100644 --- a/src/test/ui/async-await/suggest-missing-await.fixed +++ b/src/test/ui/async-await/suggest-missing-await.fixed @@ -12,8 +12,8 @@ async fn suggest_await_in_async_fn() { let x = make_u32(); take_u32(x.await) //~^ ERROR mismatched types [E0308] - //~| HELP consider using `.await` here - //~| SUGGESTION x.await + //~| HELP consider `await`ing on the `Future` + //~| SUGGESTION .await } async fn dummy() {} @@ -23,8 +23,8 @@ async fn suggest_await_in_async_fn_return() { dummy().await; //~^ ERROR mismatched types [E0308] //~| HELP try adding a semicolon - //~| HELP consider using `.await` here - //~| SUGGESTION dummy().await + //~| HELP consider `await`ing on the `Future` + //~| SUGGESTION .await } fn main() {} diff --git a/src/test/ui/async-await/suggest-missing-await.rs b/src/test/ui/async-await/suggest-missing-await.rs index 70cc1f1d5a2c6..464a9602119e1 100644 --- a/src/test/ui/async-await/suggest-missing-await.rs +++ b/src/test/ui/async-await/suggest-missing-await.rs @@ -12,8 +12,8 @@ async fn suggest_await_in_async_fn() { let x = make_u32(); take_u32(x) //~^ ERROR mismatched types [E0308] - //~| HELP consider using `.await` here - //~| SUGGESTION x.await + //~| HELP consider `await`ing on the `Future` + //~| SUGGESTION .await } async fn dummy() {} @@ -23,8 +23,8 @@ async fn suggest_await_in_async_fn_return() { dummy() //~^ ERROR mismatched types [E0308] //~| HELP try adding a semicolon - //~| HELP consider using `.await` here - //~| SUGGESTION dummy().await + //~| HELP consider `await`ing on the `Future` + //~| SUGGESTION .await } fn main() {} diff --git a/src/test/ui/async-await/suggest-missing-await.stderr b/src/test/ui/async-await/suggest-missing-await.stderr index c6355680e253b..d729420e93019 100644 --- a/src/test/ui/async-await/suggest-missing-await.stderr +++ b/src/test/ui/async-await/suggest-missing-await.stderr @@ -5,13 +5,14 @@ LL | async fn make_u32() -> u32 { | --- the `Output` of this `async fn`'s found opaque type ... LL | take_u32(x) - | ^ - | | - | expected `u32`, found opaque type - | help: consider using `.await` here: `x.await` + | ^ expected `u32`, found opaque type | = note: expected type `u32` found opaque type `impl Future` +help: consider `await`ing on the `Future` + | +LL | take_u32(x.await) + | ^^^^^^ error[E0308]: mismatched types --> $DIR/suggest-missing-await.rs:23:5 @@ -28,10 +29,10 @@ help: try adding a semicolon | LL | dummy(); | ^ -help: consider using `.await` here +help: consider `await`ing on the `Future` | LL | dummy().await - | + | ^^^^^^ error: aborting due to 2 previous errors diff --git a/src/test/ui/suggestions/issue-72766.stderr b/src/test/ui/suggestions/issue-72766.stderr index a1a5949b19660..5c9c549fa0779 100644 --- a/src/test/ui/suggestions/issue-72766.stderr +++ b/src/test/ui/suggestions/issue-72766.stderr @@ -2,13 +2,14 @@ error[E0277]: the `?` operator can only be applied to values that implement `Try --> $DIR/issue-72766.rs:14:5 | LL | SadGirl {}.call()?; - | ^^^^^^^^^^^^^^^^^^ - | | - | the `?` operator cannot be applied to type `impl Future` - | help: consider using `.await` here: `SadGirl {}.call().await?` + | ^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `impl Future` | = help: the trait `Try` is not implemented for `impl Future` = note: required by `into_result` +help: consider `await`ing on the `Future` + | +LL | SadGirl {}.call().await?; + | ^^^^^^ error: aborting due to previous error