-
-
Notifications
You must be signed in to change notification settings - Fork 15.5k
Suggest add async for function sig with return expr in body #160663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rust-bors
merged 1 commit into
rust-lang:main
from
chenyukang:yukang-fix-159495-suggest-async-return-future
Aug 9, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
29 changes: 29 additions & 0 deletions
29
tests/ui/async-await/suggest-async-fn-for-returned-future-ineligible-issue-159495.rs
This file contains hidden or 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,29 @@ | ||
| //@ edition: 2018 | ||
|
|
||
| // Do not suggest changing a function when the future is not its return value or when doing so | ||
| // would make a trait method incompatible with its declaration. | ||
|
|
||
| async fn number() -> i32 { | ||
| 42 | ||
| } | ||
|
|
||
| // A local block tail does not contribute to the function's return value. | ||
| fn local_block() { | ||
| let _: i32 = { number() }; | ||
| //~^ ERROR mismatched types | ||
| } | ||
|
|
||
| struct Wrapper; | ||
|
|
||
| trait Trait { | ||
| fn trait_method() -> i32; | ||
| } | ||
|
|
||
| impl Trait for Wrapper { | ||
| fn trait_method() -> i32 { | ||
| number() | ||
| //~^ ERROR mismatched types | ||
| } | ||
| } | ||
|
|
||
| fn main() {} |
17 changes: 17 additions & 0 deletions
17
tests/ui/async-await/suggest-async-fn-for-returned-future-ineligible-issue-159495.stderr
This file contains hidden or 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,17 @@ | ||
| error[E0308]: mismatched types | ||
| --> $DIR/suggest-async-fn-for-returned-future-ineligible-issue-159495.rs:12:20 | ||
| | | ||
| LL | let _: i32 = { number() }; | ||
| | ^^^^^^^^ expected `i32`, found future | ||
|
|
||
| error[E0308]: mismatched types | ||
| --> $DIR/suggest-async-fn-for-returned-future-ineligible-issue-159495.rs:24:9 | ||
| | | ||
| LL | fn trait_method() -> i32 { | ||
| | --- expected `i32` because of return type | ||
| LL | number() | ||
| | ^^^^^^^^ expected `i32`, found future | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0308`. | ||
36 changes: 36 additions & 0 deletions
36
tests/ui/async-await/suggest-async-fn-for-returned-future-issue-159495.fixed
This file contains hidden or 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,36 @@ | ||
| //@ edition: 2021 | ||
| //@ run-rustfix | ||
|
|
||
| #![allow(dead_code, unused_must_use)] | ||
|
|
||
| // Suggest making eligible enclosing functions async when a return expression produces a future. | ||
|
|
||
| async fn number() -> i32 { | ||
| 42 | ||
| } | ||
|
|
||
| async fn unit() {} | ||
|
|
||
| async fn wrapped_number() -> i32 { | ||
| number().await | ||
| //~^ ERROR mismatched types | ||
| } | ||
|
|
||
| async fn explicit_return() -> i32 { | ||
| return number().await; | ||
| //~^ ERROR mismatched types | ||
| } | ||
|
|
||
| struct Wrapper; | ||
|
|
||
| impl Wrapper { | ||
| pub async unsafe fn inherent_method() -> i32 { | ||
| number().await | ||
| //~^ ERROR mismatched types | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| unit(); | ||
| //~^ ERROR mismatched types | ||
| } |
36 changes: 36 additions & 0 deletions
36
tests/ui/async-await/suggest-async-fn-for-returned-future-issue-159495.rs
This file contains hidden or 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,36 @@ | ||
| //@ edition: 2021 | ||
| //@ run-rustfix | ||
|
|
||
| #![allow(dead_code, unused_must_use)] | ||
|
|
||
| // Suggest making eligible enclosing functions async when a return expression produces a future. | ||
|
|
||
| async fn number() -> i32 { | ||
| 42 | ||
| } | ||
|
|
||
| async fn unit() {} | ||
|
|
||
| fn wrapped_number() -> i32 { | ||
| number() | ||
| //~^ ERROR mismatched types | ||
| } | ||
|
|
||
| fn explicit_return() -> i32 { | ||
| return number(); | ||
| //~^ ERROR mismatched types | ||
| } | ||
|
|
||
| struct Wrapper; | ||
|
|
||
| impl Wrapper { | ||
| pub unsafe fn inherent_method() -> i32 { | ||
| number() | ||
| //~^ ERROR mismatched types | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| unit() | ||
| //~^ ERROR mismatched types | ||
| } |
55 changes: 55 additions & 0 deletions
55
tests/ui/async-await/suggest-async-fn-for-returned-future-issue-159495.stderr
This file contains hidden or 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,55 @@ | ||
| error[E0308]: mismatched types | ||
| --> $DIR/suggest-async-fn-for-returned-future-issue-159495.rs:15:5 | ||
| | | ||
| LL | fn wrapped_number() -> i32 { | ||
| | --- expected `i32` because of return type | ||
| LL | number() | ||
| | ^^^^^^^^ expected `i32`, found future | ||
| | | ||
| help: consider making the function `async` and `await`ing on the `Future` | ||
| | | ||
| LL ~ async fn wrapped_number() -> i32 { | ||
| LL ~ number().await | ||
| | | ||
|
|
||
| error[E0308]: mismatched types | ||
| --> $DIR/suggest-async-fn-for-returned-future-issue-159495.rs:20:12 | ||
| | | ||
| LL | fn explicit_return() -> i32 { | ||
| | --- expected `i32` because of return type | ||
| LL | return number(); | ||
| | ^^^^^^^^ expected `i32`, found future | ||
| | | ||
| help: consider making the function `async` and `await`ing on the `Future` | ||
| | | ||
| LL ~ async fn explicit_return() -> i32 { | ||
| LL ~ return number().await; | ||
| | | ||
|
|
||
| error[E0308]: mismatched types | ||
| --> $DIR/suggest-async-fn-for-returned-future-issue-159495.rs:28:9 | ||
| | | ||
| LL | pub unsafe fn inherent_method() -> i32 { | ||
| | --- expected `i32` because of return type | ||
| LL | number() | ||
| | ^^^^^^^^ expected `i32`, found future | ||
| | | ||
| help: consider making the function `async` and `await`ing on the `Future` | ||
| | | ||
| LL ~ pub async unsafe fn inherent_method() -> i32 { | ||
| LL ~ number().await | ||
| | | ||
|
|
||
| error[E0308]: mismatched types | ||
| --> $DIR/suggest-async-fn-for-returned-future-issue-159495.rs:34:5 | ||
| | | ||
| LL | fn main() { | ||
| | - expected `()` because of default return type | ||
| LL | unit() | ||
| | ^^^^^^- help: consider using a semicolon here: `;` | ||
| | | | ||
| | expected `()`, found future | ||
|
|
||
| error: aborting due to 4 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0308`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if having a help saying "if the function was async, you could await" would make sense for some subset of cases that we are less certain of.
View changes since the review