forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#62067 - doctorn:await_diagnostic, r=matthew…
…jasper Add suggestion for missing `.await` keyword This commit adds a suggestion diagnostic for missing `.await`. In order to do this, the trait `Future` is promoted to a lang item. Compiling code of the form: ```rust #![feature(async_await)] fn take_u32(x: u32) {} async fn make_u32() -> u32 { 22 } async fn foo() { let x = make_u32(); take_u32(x) } fn main() {} ``` Will now result in the suggestion: ``` error[E0308]: mismatched types --> src/main.rs:11:18 | 11 | take_u32(x) | ^ | | | expected u32, found opaque type | help: consider using `.await` here: `x.await` | = note: expected type `u32` found type `impl std::future::Future` ``` This commit does not cover chained expressions and therefore only covers the case originally pointed out in rust-lang#61076. Cases I can think of that still need to be covered: - [ ] Return places for functions - [ ] Field access - [ ] Method invocation I'm planning to submit PRs for each of these separately as and when I have figured them out.
- Loading branch information
Showing
9 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// edition:2018 | ||
|
||
// This test ensures we don't make the suggestion in bodies that aren't `async`. | ||
|
||
#![feature(async_await)] | ||
|
||
fn take_u32(x: u32) {} | ||
|
||
async fn make_u32() -> u32 { | ||
22 | ||
} | ||
|
||
async fn dont_suggest_await_in_closure() { | ||
|| { | ||
let x = make_u32(); | ||
take_u32(x) | ||
//~^ ERROR mismatched types [E0308] | ||
}; | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/dont-suggest-missing-await.rs:16:18 | ||
| | ||
LL | take_u32(x) | ||
| ^ expected u32, found opaque type | ||
| | ||
= note: expected type `u32` | ||
found type `impl std::future::Future` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// edition:2018 | ||
// run-rustfix | ||
|
||
#![feature(async_await)] | ||
|
||
fn take_u32(_x: u32) {} | ||
|
||
async fn make_u32() -> u32 { | ||
22 | ||
} | ||
|
||
#[allow(unused)] | ||
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 | ||
} | ||
|
||
#[allow(unused)] | ||
async fn suggest_await_in_async_closure() { | ||
async || { | ||
let x = make_u32(); | ||
take_u32(x.await) | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider using `.await` here | ||
//~| SUGGESTION x.await | ||
}; | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// edition:2018 | ||
// run-rustfix | ||
|
||
#![feature(async_await)] | ||
|
||
fn take_u32(_x: u32) {} | ||
|
||
async fn make_u32() -> u32 { | ||
22 | ||
} | ||
|
||
#[allow(unused)] | ||
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 | ||
} | ||
|
||
#[allow(unused)] | ||
async fn suggest_await_in_async_closure() { | ||
async || { | ||
let x = make_u32(); | ||
take_u32(x) | ||
//~^ ERROR mismatched types [E0308] | ||
//~| HELP consider using `.await` here | ||
//~| SUGGESTION x.await | ||
}; | ||
} | ||
|
||
fn main() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/suggest-missing-await.rs:15:14 | ||
| | ||
LL | take_u32(x) | ||
| ^ | ||
| | | ||
| expected u32, found opaque type | ||
| help: consider using `.await` here: `x.await` | ||
| | ||
= note: expected type `u32` | ||
found type `impl std::future::Future` | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/suggest-missing-await.rs:25:18 | ||
| | ||
LL | take_u32(x) | ||
| ^ | ||
| | | ||
| expected u32, found opaque type | ||
| help: consider using `.await` here: `x.await` | ||
| | ||
= note: expected type `u32` | ||
found type `impl std::future::Future` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |