Skip to content
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

Suggest adding a return type for async functions #107685

Merged
merged 1 commit into from
Feb 6, 2023

Conversation

jieyouxu
Copy link
Member

@jieyouxu jieyouxu commented Feb 5, 2023

Fixes #90027.

@rustbot
Copy link
Collaborator

rustbot commented Feb 5, 2023

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Nilstrieb (or someone else) soon.

Please see the contribution instructions for more information.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Feb 5, 2023
// Check if async function's return type was omitted.
// Don't emit suggestions if the found type is `impl Future<...>`.
debug!("suggest_missing_return_type: found = {:?}", found);
if self.get_impl_future_output_ty(found).is_none() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

@jieyouxu jieyouxu Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to return false if I call found.is_suggestable(self.tcx, false), so it doesn't emit the diagnostic if I use is_suggestable instead of self.get_impl_future_output_ty(found).is_none() unfortunately. Test case is

async fn hello() {
    Ok(())
}

Is a Result<(), _> with an unknown Err type not suggestable?

EDIT: nvm I'm dumb, have to permit type inference variables, using found.is_suggestable(self.tcx, true) works.

@@ -723,7 +723,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Check if async function's return type was omitted.
// Don't emit suggestions if the found type is `impl Future<...>`.
debug!("suggest_missing_return_type: found = {:?}", found);
if self.get_impl_future_output_ty(found).is_none() {
if found.is_suggestable(self.tcx, true) {
Copy link
Member

@compiler-errors compiler-errors Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using is_suggestable with false is actually consistent with other diagnostics, which don't suggest anything if there's inference variables. See:

fn foo() {
    Ok(())
}

which does not suggest -> Result<(), _>, since that's not exactly a complete type.

I'm not sure if we should be suggesting something when there are inference variables in the return type -- you could probably just come up with a different example instead of Ok(()) in the UI test you added.

Either that, or the regular "return type is missing" suggestion should be modified in the non-async case, but then the subdiagnostic applicability would need to be bumped down to "has-placeholders" from "machine-applicable".

Copy link
Member Author

@jieyouxu jieyouxu Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that makes sense, I will change it back and modify the test. Not suggesting an incomplete type makes more sense to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Maybe worth adding a test for something that's "not suggestable" too, like a closure.

@compiler-errors
Copy link
Member

Can you squash these commits? Then I can approve, thanks.

@jieyouxu
Copy link
Member Author

jieyouxu commented Feb 6, 2023

Can you squash these commits? Then I can approve, thanks.

Squashed, thanks for the review

@compiler-errors
Copy link
Member

Thanks!

r? @compiler-errors @bors r+ rollup

If you'd like to look into making this suggestion + the non-async version do something for inference variable cases too (maybe suggest a "HasPlaceholders" level, and make the wording weaker, like suggesting filling in the _), I'd love to see a follow-up. No pressure to do that, though.

@bors
Copy link
Contributor

bors commented Feb 6, 2023

📌 Commit 74d9d4900c716cca9953b62de500f48c87b5d577 has been approved by compiler-errors

It is now in the queue for this repository.

@rustbot rustbot assigned compiler-errors and unassigned Noratrieb Feb 6, 2023
@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 6, 2023
@jieyouxu
Copy link
Member Author

jieyouxu commented Feb 6, 2023

Thanks!

r? @compiler-errors @bors r+ rollup

If you'd like to look into making this suggestion + the non-async version do something for inference variable cases too (maybe suggest a "HasPlaceholders" level, and make the wording weaker, like suggesting filling in the _), I'd love to see a follow-up. No pressure to do that, though.

I'll take a look at how to do that, sure

@jieyouxu
Copy link
Member Author

jieyouxu commented Feb 6, 2023

D'oh I forgot to remove the try adding a semicolon thing, sorry about that

@compiler-errors
Copy link
Member

@bors r+

@bors
Copy link
Contributor

bors commented Feb 6, 2023

📌 Commit 6b05b80 has been approved by compiler-errors

It is now in the queue for this repository.

bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 6, 2023
Rollup of 5 pull requests

Successful merges:

 - rust-lang#107553 (Suggest std::ptr::null if literal 0 is given to a raw pointer function argument)
 - rust-lang#107580 (Recover from lifetimes with default lifetimes in generic args)
 - rust-lang#107669 (rustdoc: combine duplicate rules in ayu CSS)
 - rust-lang#107685 (Suggest adding a return type for async functions)
 - rust-lang#107687 (Adapt SROA MIR opt for aggregated MIR)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 675976e into rust-lang:master Feb 6, 2023
@rustbot rustbot added this to the 1.69.0 milestone Feb 6, 2023
@jieyouxu jieyouxu deleted the issue-90027 branch October 22, 2024 12:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rust doesn't suggest adding a return type for async functions
5 participants