diff options
| -rw-r--r-- | compiler/rustc_hir_typeck/src/expr.rs | 25 | ||||
| -rw-r--r-- | tests/ui/async-await/issue-61076.rs | 2 | ||||
| -rw-r--r-- | tests/ui/async-await/issue-61076.stderr | 16 |
3 files changed, 20 insertions, 23 deletions
diff --git a/compiler/rustc_hir_typeck/src/expr.rs b/compiler/rustc_hir_typeck/src/expr.rs index c5a312a6605..7e235ea7f46 100644 --- a/compiler/rustc_hir_typeck/src/expr.rs +++ b/compiler/rustc_hir_typeck/src/expr.rs @@ -2418,11 +2418,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { field_ident: Ident, base: &'tcx hir::Expr<'tcx>, ty: Ty<'tcx>, - ) -> bool { + ) { let Some(output_ty) = self.get_impl_future_output_ty(ty) else { - return false; + return; }; - let mut add_label = true; if let ty::Adt(def, _) = output_ty.kind() { // no field access on enum type if !def.is_enum() { @@ -2432,7 +2431,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { .iter() .any(|field| field.ident(self.tcx) == field_ident) { - add_label = false; err.span_label( field_ident.span, "field not available in `impl Future`, but it is available in its `Output`", @@ -2446,10 +2444,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } } } - if add_label { - err.span_label(field_ident.span, format!("field not found in `{ty}`")); - } - true } fn ban_nonexisting_field( @@ -2465,29 +2459,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { ); let mut err = self.no_such_field_err(ident, base_ty, base.hir_id); - let has_label = match *base_ty.peel_refs().kind() { + match *base_ty.peel_refs().kind() { ty::Array(_, len) => { self.maybe_suggest_array_indexing(&mut err, expr, base, ident, len); - false } ty::RawPtr(..) => { self.suggest_first_deref_field(&mut err, expr, base, ident); - false } ty::Param(param_ty) => { self.point_at_param_definition(&mut err, param_ty); - false } ty::Alias(ty::Opaque, _) => { - self.suggest_await_on_field_access(&mut err, ident, base, base_ty.peel_refs()) + self.suggest_await_on_field_access(&mut err, ident, base, base_ty.peel_refs()); } - _ => false, - }; - - if !has_label { - err.span_label(ident.span, "unknown field"); + _ => {} } + err.span_label(ident.span, "unknown field"); + self.suggest_fn_call(&mut err, base, base_ty, |output_ty| { if let ty::Adt(def, _) = output_ty.kind() && !def.is_enum() diff --git a/tests/ui/async-await/issue-61076.rs b/tests/ui/async-await/issue-61076.rs index cf6e5b4e436..ef6a7055c78 100644 --- a/tests/ui/async-await/issue-61076.rs +++ b/tests/ui/async-await/issue-61076.rs @@ -71,10 +71,12 @@ async fn baz() -> Result<(), ()> { let _: i32 = tuple().0; //~ ERROR no field `0` //~^ HELP consider `await`ing on the `Future` //~| NOTE field not available in `impl Future` + //~| NOTE unknown field let _: i32 = struct_().a; //~ ERROR no field `a` //~^ HELP consider `await`ing on the `Future` //~| NOTE field not available in `impl Future` + //~| NOTE unknown field struct_().method(); //~ ERROR no method named //~^ NOTE method not found in `impl Future<Output = Struct>` diff --git a/tests/ui/async-await/issue-61076.stderr b/tests/ui/async-await/issue-61076.stderr index 44de282988b..13a0464fdb2 100644 --- a/tests/ui/async-await/issue-61076.stderr +++ b/tests/ui/async-await/issue-61076.stderr @@ -26,7 +26,10 @@ error[E0609]: no field `0` on type `impl Future<Output = Tuple>` --> $DIR/issue-61076.rs:71:26 | LL | let _: i32 = tuple().0; - | ^ field not available in `impl Future`, but it is available in its `Output` + | ^ + | | + | field not available in `impl Future`, but it is available in its `Output` + | unknown field | help: consider `await`ing on the `Future` and access the field of its `Output` | @@ -34,10 +37,13 @@ LL | let _: i32 = tuple().await.0; | ++++++ error[E0609]: no field `a` on type `impl Future<Output = Struct>` - --> $DIR/issue-61076.rs:75:28 + --> $DIR/issue-61076.rs:76:28 | LL | let _: i32 = struct_().a; - | ^ field not available in `impl Future`, but it is available in its `Output` + | ^ + | | + | field not available in `impl Future`, but it is available in its `Output` + | unknown field | help: consider `await`ing on the `Future` and access the field of its `Output` | @@ -45,7 +51,7 @@ LL | let _: i32 = struct_().await.a; | ++++++ error[E0599]: no method named `method` found for opaque type `impl Future<Output = Struct>` in the current scope - --> $DIR/issue-61076.rs:79:15 + --> $DIR/issue-61076.rs:81:15 | LL | struct_().method(); | ^^^^^^ method not found in `impl Future<Output = Struct>` @@ -56,7 +62,7 @@ LL | struct_().await.method(); | ++++++ error[E0308]: mismatched types - --> $DIR/issue-61076.rs:88:9 + --> $DIR/issue-61076.rs:90:9 | LL | match tuple() { | ------- this expression has type `impl Future<Output = Tuple>` |
