Tweak "use .await" suggestion

This commit is contained in:
Esteban Küber 2020-10-21 14:25:09 -07:00
parent 07a63e6d1f
commit 86df9039b2
11 changed files with 55 additions and 53 deletions

View File

@ -21,7 +21,7 @@ use rustc_middle::ty::{
}; };
use rustc_middle::ty::{TypeAndMut, TypeckResults}; use rustc_middle::ty::{TypeAndMut, TypeckResults};
use rustc_span::symbol::{kw, sym, Ident, Symbol}; 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 rustc_target::spec::abi;
use std::fmt; 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 self.predicate_may_hold(&try_obligation) && impls_future {
if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) { if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span) {
if snippet.ends_with('?') { if snippet.ends_with('?') {
err.span_suggestion( err.span_suggestion_verbose(
span, span.with_hi(span.hi() - BytePos(1)).shrink_to_hi(),
"consider using `.await` here", "consider `await`ing on the `Future`",
format!("{}.await?", snippet.trim_end_matches('?')), ".await".to_string(),
Applicability::MaybeIncorrect, Applicability::MaybeIncorrect,
); );
} }

View File

@ -497,16 +497,12 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
if self.infcx.predicate_may_hold(&obligation) { if self.infcx.predicate_may_hold(&obligation) {
debug!("suggest_missing_await: obligation held: {:?}", obligation); debug!("suggest_missing_await: obligation held: {:?}", obligation);
if let Ok(code) = self.sess().source_map().span_to_snippet(sp) { err.span_suggestion_verbose(
err.span_suggestion( sp.shrink_to_hi(),
sp, "consider `await`ing on the `Future`",
"consider using `.await` here", ".await".to_string(),
format!("{}.await", code), Applicability::MaybeIncorrect,
Applicability::MaybeIncorrect, );
);
} else {
debug!("suggest_missing_await: no snippet for {:?}", sp);
}
} else { } else {
debug!("suggest_missing_await: obligation did not hold: {:?}", obligation) debug!("suggest_missing_await: obligation did not hold: {:?}", obligation)
} }

View File

@ -237,13 +237,14 @@ error[E0277]: the `?` operator can only be applied to values that implement `Try
--> $DIR/incorrect-syntax-suggestions.rs:16:19 --> $DIR/incorrect-syntax-suggestions.rs:16:19
| |
LL | let _ = await bar()?; LL | let _ = await bar()?;
| ^^^^^^ | ^^^^^^ the `?` operator cannot be applied to type `impl Future`
| |
| the `?` operator cannot be applied to type `impl Future`
| help: consider using `.await` here: `bar().await?`
| |
= help: the trait `Try` is not implemented for `impl Future` = help: the trait `Try` is not implemented for `impl Future`
= note: required by `into_result` = 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` error[E0277]: the `?` operator can only be applied to values that implement `Try`
--> $DIR/incorrect-syntax-suggestions.rs:63:19 --> $DIR/incorrect-syntax-suggestions.rs:63:19

View File

@ -2,25 +2,27 @@ error[E0277]: the `?` operator can only be applied to values that implement `Try
--> $DIR/issue-61076.rs:42:5 --> $DIR/issue-61076.rs:42:5
| |
LL | foo()?; LL | foo()?;
| ^^^^^^ | ^^^^^^ the `?` operator cannot be applied to type `impl Future`
| |
| the `?` operator cannot be applied to type `impl Future`
| help: consider using `.await` here: `foo().await?`
| |
= help: the trait `Try` is not implemented for `impl Future` = help: the trait `Try` is not implemented for `impl Future`
= note: required by `into_result` = 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` error[E0277]: the `?` operator can only be applied to values that implement `Try`
--> $DIR/issue-61076.rs:56:5 --> $DIR/issue-61076.rs:56:5
| |
LL | t?; LL | t?;
| ^^ | ^^ the `?` operator cannot be applied to type `T`
| |
| the `?` operator cannot be applied to type `T`
| help: consider using `.await` here: `t.await?`
| |
= help: the trait `Try` is not implemented for `T` = help: the trait `Try` is not implemented for `T`
= note: required by `into_result` = note: required by `into_result`
help: consider `await`ing on the `Future`
|
LL | t.await?;
| ^^^^^^
error[E0609]: no field `0` on type `impl Future` error[E0609]: no field `0` on type `impl Future`
--> $DIR/issue-61076.rs:58:26 --> $DIR/issue-61076.rs:58:26

View File

@ -15,8 +15,8 @@ async fn suggest_await_in_async_closure() {
let x = make_u32(); let x = make_u32();
take_u32(x.await) take_u32(x.await)
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider using `.await` here //~| HELP consider `await`ing on the `Future`
//~| SUGGESTION x.await //~| SUGGESTION .await
}; };
} }

View File

@ -15,8 +15,8 @@ async fn suggest_await_in_async_closure() {
let x = make_u32(); let x = make_u32();
take_u32(x) take_u32(x)
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider using `.await` here //~| HELP consider `await`ing on the `Future`
//~| SUGGESTION x.await //~| SUGGESTION .await
}; };
} }

View File

@ -5,13 +5,14 @@ LL | async fn make_u32() -> u32 {
| --- the `Output` of this `async fn`'s found opaque type | --- the `Output` of this `async fn`'s found opaque type
... ...
LL | take_u32(x) LL | take_u32(x)
| ^ | ^ expected `u32`, found opaque type
| |
| expected `u32`, found opaque type
| help: consider using `.await` here: `x.await`
| |
= note: expected type `u32` = note: expected type `u32`
found opaque type `impl Future` found opaque type `impl Future`
help: consider `await`ing on the `Future`
|
LL | take_u32(x.await)
| ^^^^^^
error: aborting due to previous error error: aborting due to previous error

View File

@ -12,8 +12,8 @@ async fn suggest_await_in_async_fn() {
let x = make_u32(); let x = make_u32();
take_u32(x.await) take_u32(x.await)
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider using `.await` here //~| HELP consider `await`ing on the `Future`
//~| SUGGESTION x.await //~| SUGGESTION .await
} }
async fn dummy() {} async fn dummy() {}
@ -23,8 +23,8 @@ async fn suggest_await_in_async_fn_return() {
dummy().await; dummy().await;
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP try adding a semicolon //~| HELP try adding a semicolon
//~| HELP consider using `.await` here //~| HELP consider `await`ing on the `Future`
//~| SUGGESTION dummy().await //~| SUGGESTION .await
} }
fn main() {} fn main() {}

View File

@ -12,8 +12,8 @@ async fn suggest_await_in_async_fn() {
let x = make_u32(); let x = make_u32();
take_u32(x) take_u32(x)
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP consider using `.await` here //~| HELP consider `await`ing on the `Future`
//~| SUGGESTION x.await //~| SUGGESTION .await
} }
async fn dummy() {} async fn dummy() {}
@ -23,8 +23,8 @@ async fn suggest_await_in_async_fn_return() {
dummy() dummy()
//~^ ERROR mismatched types [E0308] //~^ ERROR mismatched types [E0308]
//~| HELP try adding a semicolon //~| HELP try adding a semicolon
//~| HELP consider using `.await` here //~| HELP consider `await`ing on the `Future`
//~| SUGGESTION dummy().await //~| SUGGESTION .await
} }
fn main() {} fn main() {}

View File

@ -5,13 +5,14 @@ LL | async fn make_u32() -> u32 {
| --- the `Output` of this `async fn`'s found opaque type | --- the `Output` of this `async fn`'s found opaque type
... ...
LL | take_u32(x) LL | take_u32(x)
| ^ | ^ expected `u32`, found opaque type
| |
| expected `u32`, found opaque type
| help: consider using `.await` here: `x.await`
| |
= note: expected type `u32` = note: expected type `u32`
found opaque type `impl Future` found opaque type `impl Future`
help: consider `await`ing on the `Future`
|
LL | take_u32(x.await)
| ^^^^^^
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/suggest-missing-await.rs:23:5 --> $DIR/suggest-missing-await.rs:23:5
@ -28,10 +29,10 @@ help: try adding a semicolon
| |
LL | dummy(); LL | dummy();
| ^ | ^
help: consider using `.await` here help: consider `await`ing on the `Future`
| |
LL | dummy().await LL | dummy().await
| | ^^^^^^
error: aborting due to 2 previous errors error: aborting due to 2 previous errors

View File

@ -2,13 +2,14 @@ error[E0277]: the `?` operator can only be applied to values that implement `Try
--> $DIR/issue-72766.rs:14:5 --> $DIR/issue-72766.rs:14:5
| |
LL | SadGirl {}.call()?; LL | SadGirl {}.call()?;
| ^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `impl Future`
| |
| the `?` operator cannot be applied to type `impl Future`
| help: consider using `.await` here: `SadGirl {}.call().await?`
| |
= help: the trait `Try` is not implemented for `impl Future` = help: the trait `Try` is not implemented for `impl Future`
= note: required by `into_result` = note: required by `into_result`
help: consider `await`ing on the `Future`
|
LL | SadGirl {}.call().await?;
| ^^^^^^
error: aborting due to previous error error: aborting due to previous error