Skip to content

Commit df86d04

Browse files
authored
Rollup merge of #97721 - compiler-errors:issue-97704, r=jackh726
Do `suggest_await_before_try` with infer variables in self, and clean up binders Fixes #97704 Also cleans up binders in this fn, since everything is a `Poly*` and we really shouldn't have stray escaping late-bound regions everywhere. That's why the function changed so much. This isn't necessary, so I can revert if necessary.
2 parents f2b3808 + d268b34 commit df86d04

File tree

4 files changed

+71
-30
lines changed

4 files changed

+71
-30
lines changed

Diff for: compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+18-30
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{
55

66
use crate::autoderef::Autoderef;
77
use crate::infer::InferCtxt;
8-
use crate::traits::normalize_projection_type;
8+
use crate::traits::normalize_to;
99

1010
use rustc_data_structures::fx::FxHashSet;
1111
use rustc_data_structures::stack::ensure_sufficient_stack;
@@ -2706,55 +2706,43 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
27062706
let future_trait = self.tcx.require_lang_item(LangItem::Future, None);
27072707

27082708
let self_ty = self.resolve_vars_if_possible(trait_pred.self_ty());
2709-
2710-
// Do not check on infer_types to avoid panic in evaluate_obligation.
2711-
if self_ty.has_infer_types() {
2712-
return;
2713-
}
2714-
let self_ty = self.tcx.erase_regions(self_ty);
2715-
27162709
let impls_future = self.type_implements_trait(
27172710
future_trait,
2718-
self_ty.skip_binder(),
2711+
self.tcx.erase_late_bound_regions(self_ty),
27192712
ty::List::empty(),
27202713
obligation.param_env,
27212714
);
2715+
if !impls_future.must_apply_modulo_regions() {
2716+
return;
2717+
}
27222718

27232719
let item_def_id = self.tcx.associated_item_def_ids(future_trait)[0];
27242720
// `<T as Future>::Output`
2725-
let projection_ty = ty::ProjectionTy {
2726-
// `T`
2727-
substs: self.tcx.mk_substs_trait(
2728-
trait_pred.self_ty().skip_binder(),
2729-
&self.fresh_substs_for_item(span, item_def_id)[1..],
2730-
),
2731-
// `Future::Output`
2732-
item_def_id,
2733-
};
2734-
2735-
let mut selcx = SelectionContext::new(self);
2736-
2737-
let mut obligations = vec![];
2738-
let normalized_ty = normalize_projection_type(
2739-
&mut selcx,
2721+
let projection_ty = trait_pred.map_bound(|trait_pred| {
2722+
self.tcx.mk_projection(
2723+
item_def_id,
2724+
// Future::Output has no substs
2725+
self.tcx.mk_substs_trait(trait_pred.self_ty(), &[]),
2726+
)
2727+
});
2728+
let projection_ty = normalize_to(
2729+
&mut SelectionContext::new(self),
27402730
obligation.param_env,
2741-
projection_ty,
27422731
obligation.cause.clone(),
2743-
0,
2744-
&mut obligations,
2732+
projection_ty,
2733+
&mut vec![],
27452734
);
27462735

27472736
debug!(
27482737
"suggest_await_before_try: normalized_projection_type {:?}",
2749-
self.resolve_vars_if_possible(normalized_ty)
2738+
self.resolve_vars_if_possible(projection_ty)
27502739
);
27512740
let try_obligation = self.mk_trait_obligation_with_new_self_ty(
27522741
obligation.param_env,
2753-
trait_pred.map_bound(|trait_pred| (trait_pred, normalized_ty.ty().unwrap())),
2742+
trait_pred.map_bound(|trait_pred| (trait_pred, projection_ty.skip_binder())),
27542743
);
27552744
debug!("suggest_await_before_try: try_trait_obligation {:?}", try_obligation);
27562745
if self.predicate_may_hold(&try_obligation)
2757-
&& impls_future.must_apply_modulo_regions()
27582746
&& let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(span)
27592747
&& snippet.ends_with('?')
27602748
{

Diff for: src/test/ui/suggestions/issue-97704.fixed

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// edition:2021
2+
// run-rustfix
3+
4+
#![allow(unused)]
5+
6+
use std::future::Future;
7+
8+
async fn foo() -> Result<(), i32> {
9+
func(async { Ok::<_, i32>(()) }).await?;
10+
//~^ ERROR the `?` operator can only be applied to values that implement `Try`
11+
12+
Ok(())
13+
}
14+
15+
async fn func<T>(fut: impl Future<Output = T>) -> T {
16+
fut.await
17+
}
18+
19+
fn main() {}

Diff for: src/test/ui/suggestions/issue-97704.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// edition:2021
2+
// run-rustfix
3+
4+
#![allow(unused)]
5+
6+
use std::future::Future;
7+
8+
async fn foo() -> Result<(), i32> {
9+
func(async { Ok::<_, i32>(()) })?;
10+
//~^ ERROR the `?` operator can only be applied to values that implement `Try`
11+
12+
Ok(())
13+
}
14+
15+
async fn func<T>(fut: impl Future<Output = T>) -> T {
16+
fut.await
17+
}
18+
19+
fn main() {}

Diff for: src/test/ui/suggestions/issue-97704.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0277]: the `?` operator can only be applied to values that implement `Try`
2+
--> $DIR/issue-97704.rs:9:5
3+
|
4+
LL | func(async { Ok::<_, i32>(()) })?;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the `?` operator cannot be applied to type `impl Future<Output = Result<(), i32>>`
6+
|
7+
= help: the trait `Try` is not implemented for `impl Future<Output = Result<(), i32>>`
8+
help: consider `await`ing on the `Future`
9+
|
10+
LL | func(async { Ok::<_, i32>(()) }).await?;
11+
| ++++++
12+
13+
error: aborting due to previous error
14+
15+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)