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

Await on mismatched future types #72784

Merged
merged 5 commits into from
Aug 27, 2020
Merged

Await on mismatched future types #72784

merged 5 commits into from
Aug 27, 2020

Conversation

csmoe
Copy link
Member

@csmoe csmoe commented May 30, 2020

Closes #61076
This PR suggests to await on:

  1. async_fn().bar() => async_fn().await.bar()
  2. async_fn().field => async_fn().await.field
  3. if let x = async() {} => if let x = async().await {}

r? @tmandry @estebank

@bors
Copy link
Contributor

bors commented Aug 25, 2020

☔ The latest upstream changes (presumably #75666) made this pull request unmergeable. Please resolve the merge conflicts.

@bors bors added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Aug 25, 2020
@csmoe csmoe force-pushed the issue-61076 branch 2 times, most recently from b33a812 to 305c09f Compare August 25, 2020 06:07
@estebank
Copy link
Contributor

@csmoe sorry, this fell through the cracks of my queue. Love the changes. Left some nitpicks but otherwise it LGTM!

@estebank
Copy link
Contributor

r? @estebank

@csmoe csmoe added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 26, 2020
Comment on lines 1521 to 1549
let future_trait = self.tcx.require_lang_item(LangItem::Future, None);
// Future::Output
let item_def_id =
self.tcx.associated_items(future_trait).in_definition_order().next().unwrap().def_id;

let mut projection_ty = None;
for (predicate, _) in self.tcx.predicates_of(def_id).predicates {
if let ty::PredicateAtom::Projection(projection_predicate) = predicate.skip_binders() {
if item_def_id == projection_predicate.projection_ty.item_def_id {
projection_ty = Some(projection_predicate.projection_ty);
break;
}
}
}
debug!("suggest_await_on_field_access: projection_ty={:?}", projection_ty);

let cause = self.misc(expr.span);
let mut selcx = SelectionContext::new(&self.infcx);

let mut obligations = vec![];
if let Some(projection_ty) = projection_ty {
let normalized_ty = rustc_trait_selection::traits::normalize_projection_type(
&mut selcx,
param_env,
projection_ty,
cause,
0,
&mut obligations,
);
Copy link
Member

Choose a reason for hiding this comment

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

Is there a central place somewhere that we could define a helper to do this? I'd rather not duplicate it in all these places.

(If we can't extract the normalizing code that's fine, I'd prefer to, but we could just extract the parts before it.)

Copy link
Contributor

Choose a reason for hiding this comment

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

Having a src/librustc_typeck/check/diagnostics.rs would make sense, and would follow the pattern in other parts of the compiler.

I made a quick search for SelectionContext::new( as I would imagine that any code doing this same thing would be using it, but I couldn't find any diagnostics use in typeck.

@csmoe
Copy link
Member Author

csmoe commented Aug 27, 2020

@tmandry @estebank so I query'fied the loop to finding Future::Output from predicates.

@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Aug 27, 2020

📌 Commit 7cfcefd has been approved by estebank

@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 Aug 27, 2020
@bors
Copy link
Contributor

bors commented Aug 27, 2020

⌛ Testing commit 7cfcefd with merge f7cbb7a...

@bors
Copy link
Contributor

bors commented Aug 27, 2020

☀️ Test successful - checks-actions, checks-azure
Approved by: estebank
Pushing f7cbb7a to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Aug 27, 2020
@bors bors merged commit f7cbb7a into rust-lang:master Aug 27, 2020
@estebank
Copy link
Contributor

estebank commented Oct 23, 2020

These suggestions became dead code at some point (😩), but will be emitted again after #78214, except for the tuple field access and method calling ones.

To avoid these kind of suggestion regressions it is best to annotate with //~^ HELP as much as possible (which I don't always do).

estebank added a commit to estebank/rust that referenced this pull request Oct 23, 2020
When encountering a failing method or field resolution on a `Future`,
look at the `Output` and try the same operation on it. If successful,
suggest calling `.await` on the `Future`.

This had already been introduced in rust-lang#72784, but at some point they
stopped working.
estebank added a commit to estebank/rust that referenced this pull request Oct 26, 2020
When encountering a failing method or field resolution on a `Future`,
look at the `Output` and try the same operation on it. If successful,
suggest calling `.await` on the `Future`.

This had already been introduced in rust-lang#72784, but at some point they
stopped working.
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this pull request Oct 28, 2020
Suggest calling await on method call and field access

When encountering a failing method or field resolution on a `Future`,
look at the `Output` and try the same operation on it. If successful,
suggest calling `.await` on the `Future`.

This had already been introduced in rust-lang#72784, but at some point they
stopped working.

Built on top of rust-lang#78214, only last commit is relevant.

r? @oli-obk
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

suggest await for future-related type errors
4 participants