Skip to content

Commit

Permalink
Tweak "use .await" suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Oct 23, 2020
1 parent 07a63e6 commit 86df903
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 53 deletions.
Expand Up @@ -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;

Expand Down Expand Up @@ -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,
);
}
Expand Down
16 changes: 6 additions & 10 deletions compiler/rustc_typeck/src/check/fn_ctxt/suggestions.rs
Expand Up @@ -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)
}
Expand Down
Expand Up @@ -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
Expand Down
18 changes: 10 additions & 8 deletions src/test/ui/async-await/issue-61076.stderr
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/async-await/suggest-missing-await-closure.fixed
Expand Up @@ -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
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/async-await/suggest-missing-await-closure.rs
Expand Up @@ -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
};
}

Expand Down
9 changes: 5 additions & 4 deletions src/test/ui/async-await/suggest-missing-await-closure.stderr
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/async-await/suggest-missing-await.fixed
Expand Up @@ -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() {}
Expand All @@ -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() {}
8 changes: 4 additions & 4 deletions src/test/ui/async-await/suggest-missing-await.rs
Expand Up @@ -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() {}
Expand All @@ -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() {}
13 changes: 7 additions & 6 deletions src/test/ui/async-await/suggest-missing-await.stderr
Expand Up @@ -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
Expand All @@ -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

Expand Down
9 changes: 5 additions & 4 deletions src/test/ui/suggestions/issue-72766.stderr
Expand Up @@ -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

Expand Down

0 comments on commit 86df903

Please sign in to comment.