Skip to content

Suggest to await future before ? operator #71948

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
merged 7 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions src/librustc_middle/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,11 +390,7 @@ impl<'hir> Map<'hir> {
/// Given a `HirId`, returns the `BodyId` associated with it,
/// if the node is a body owner, otherwise returns `None`.
pub fn maybe_body_owned_by(&self, hir_id: HirId) -> Option<BodyId> {
if let Some(node) = self.find(hir_id) {
associated_body(node)
} else {
bug!("no entry for id `{}`", hir_id)
}
if let Some(node) = self.find(hir_id) { associated_body(node) } else { None }
}

/// Given a body owner's id, returns the `BodyId` associated with it.
Expand Down
1 change: 1 addition & 0 deletions src/librustc_trait_selection/traits/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
self.suggest_remove_reference(&obligation, &mut err, &trait_ref);
self.suggest_semicolon_removal(&obligation, &mut err, span, &trait_ref);
self.note_version_mismatch(&mut err, &trait_ref);
self.suggest_await_before_try(&mut err, &obligation, &trait_ref, span);
if self.suggest_impl_trait(&mut err, span, &obligation, &trait_ref) {
err.emit();
return;
Expand Down
100 changes: 100 additions & 0 deletions src/librustc_trait_selection/traits/error_reporting/suggestions.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use super::{
EvaluationResult, Obligation, ObligationCause, ObligationCauseCode, PredicateObligation,
SelectionContext,
};

use crate::infer::InferCtxt;
use crate::traits::normalize_projection_type;

use rustc_errors::{error_code, struct_span_err, Applicability, DiagnosticBuilder, Style};
use rustc_hir as hir;
Expand Down Expand Up @@ -150,6 +152,15 @@ pub trait InferCtxtExt<'tcx> {
T: fmt::Display;

fn suggest_new_overflow_limit(&self, err: &mut DiagnosticBuilder<'_>);

/// Suggest to await before try: future? => future.await?
fn suggest_await_before_try(
&self,
err: &mut DiagnosticBuilder<'_>,
obligation: &PredicateObligation<'tcx>,
trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
span: Span,
);
}

fn predicate_constraint(generics: &hir::Generics<'_>, pred: String) -> (Span, String) {
Expand Down Expand Up @@ -1765,6 +1776,95 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
suggested_limit, self.tcx.crate_name,
));
}

fn suggest_await_before_try(
&self,
err: &mut DiagnosticBuilder<'_>,
obligation: &PredicateObligation<'tcx>,
trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
span: Span,
) {
debug!(
"suggest_await_befor_try: obligation={:?}, span={:?}, trait_ref={:?}",
obligation, span, trait_ref
);
let body_hir_id = obligation.cause.body_id;
let item_id = self.tcx.hir().get_parent_node(body_hir_id);

let mut is_future = false;
if let ty::Opaque(def_id, substs) = trait_ref.self_ty().kind {
let preds = self.tcx.predicates_of(def_id).instantiate(self.tcx, substs);
for p in preds.predicates {
if let Some(trait_ref) = p.to_opt_poly_trait_ref() {
if Some(trait_ref.def_id()) == self.tcx.lang_items().future_trait() {
is_future = true;
break;
}
}
}
}

if let Some(body_id) = self.tcx.hir().maybe_body_owned_by(item_id) {
let body = self.tcx.hir().body(body_id);
if let Some(hir::GeneratorKind::Async(_)) = body.generator_kind {
let future_trait = self.tcx.lang_items().future_trait().unwrap();
let item_def_id = self
.tcx
.associated_items(future_trait)
.in_definition_order()
.next()
.unwrap()
.def_id;
debug!("trait_ref_self_ty: {:?}", trait_ref.self_ty());
// `<T as Future>::Output`
let projection_ty = ty::ProjectionTy {
// `T`
substs: self.tcx.mk_substs_trait(
trait_ref.self_ty(),
self.fresh_substs_for_item(span, item_def_id),
),
// `Future::Output`
item_def_id,
};

let mut selcx = SelectionContext::new(self);

let mut obligations = vec![];
let normalized_ty = normalize_projection_type(
&mut selcx,
obligation.param_env,
projection_ty,
obligation.cause.clone(),
0,
&mut obligations,
);

debug!(
"suggest_await_befor_try: normalized_projection_type {:?}",
self.resolve_vars_if_possible(&normalized_ty)
);
let try_obligation = self.mk_obligation_for_def_id(
trait_ref.def_id(),
normalized_ty,
obligation.cause.clone(),
obligation.param_env,
);
debug!("suggest_await_befor_try: try_trait_obligation {:?}", try_obligation);
if self.predicate_may_hold(&try_obligation) && is_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('?')),
Applicability::MaybeIncorrect,
);
}
}
}
}
}
}
}

/// Collect all the returned expressions within the input expression.
Expand Down
24 changes: 14 additions & 10 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5289,6 +5289,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
expected: Ty<'tcx>,
found: Ty<'tcx>,
) {
debug!("suggest_missing_await: expr={:?} expected={:?}, found={:?}", expr, expected, found);
// `.await` is not permitted outside of `async` bodies, so don't bother to suggest if the
// body isn't `async`.
let item_id = self.tcx().hir().get_parent_node(self.body_id);
Expand All @@ -5306,22 +5307,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.next()
.unwrap()
.def_id;
// `<T as Future>::Output`
let projection_ty = ty::ProjectionTy {
// `T`
substs: self
.tcx
.mk_substs_trait(found, self.fresh_substs_for_item(sp, item_def_id)),
// `Future::Output`
item_def_id,
};

let predicate =
ty::Predicate::Projection(ty::Binder::bind(ty::ProjectionPredicate {
// `<T as Future>::Output`
projection_ty: ty::ProjectionTy {
// `T`
substs: self.tcx.mk_substs_trait(
found,
self.fresh_substs_for_item(sp, item_def_id),
),
// `Future::Output`
item_def_id,
},
projection_ty,
ty: expected,
}));
let obligation = traits::Obligation::new(self.misc(sp), self.param_env, predicate);

debug!("suggest_missing_await: trying obligation {:?}", obligation);

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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,10 @@ error[E0277]: the `?` operator can only be applied to values that implement `std
--> $DIR/incorrect-syntax-suggestions.rs:16:19
|
LL | let _ = await bar()?;
| ^^^^^^ the `?` operator cannot be applied to type `impl std::future::Future`
| ^^^^^^
| |
| the `?` operator cannot be applied to type `impl std::future::Future`
| help: consider using `.await` here: `bar().await?`
|
= help: the trait `std::ops::Try` is not implemented for `impl std::future::Future`
= note: required by `std::ops::Try::into_result`
Expand Down
12 changes: 12 additions & 0 deletions src/test/ui/async-await/issue-61076.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// edition:2018

async fn foo() -> Result<(), ()> {
Ok(())
}

async fn bar() -> Result<(), ()> {
foo()?; //~ ERROR the `?` operator can only be applied to values that implement `std::ops::Try`
Ok(())
}

fn main() {}
15 changes: 15 additions & 0 deletions src/test/ui/async-await/issue-61076.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0277]: the `?` operator can only be applied to values that implement `std::ops::Try`
--> $DIR/issue-61076.rs:8:5
|
LL | foo()?;
| ^^^^^^
| |
| the `?` operator cannot be applied to type `impl std::future::Future`
| help: consider using `.await` here: `foo().await?`
|
= help: the trait `std::ops::Try` is not implemented for `impl std::future::Future`
= note: required by `std::ops::Try::into_result`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.