Skip to content

Commit c8b71ef

Browse files
Also note for fields
1 parent 5e8820c commit c8b71ef

File tree

4 files changed

+53
-9
lines changed

4 files changed

+53
-9
lines changed

compiler/rustc_hir_typeck/src/expr.rs

+21-7
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ use rustc_data_structures::stack::ensure_sufficient_stack;
1010
use rustc_data_structures::unord::UnordMap;
1111
use rustc_errors::codes::*;
1212
use rustc_errors::{
13-
Applicability, Diag, ErrorGuaranteed, StashKey, Subdiagnostic, pluralize, struct_span_code_err,
13+
Applicability, Diag, ErrorGuaranteed, MultiSpan, StashKey, Subdiagnostic, pluralize,
14+
struct_span_code_err,
1415
};
1516
use rustc_hir::def::{CtorKind, DefKind, Res};
1617
use rustc_hir::def_id::DefId;
@@ -2757,12 +2758,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
27572758
field_ident.span,
27582759
"field not available in `impl Future`, but it is available in its `Output`",
27592760
);
2760-
err.span_suggestion_verbose(
2761-
base.span.shrink_to_hi(),
2762-
"consider `await`ing on the `Future` and access the field of its `Output`",
2763-
".await",
2764-
Applicability::MaybeIncorrect,
2765-
);
2761+
match self.tcx.coroutine_kind(self.body_id) {
2762+
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => {
2763+
err.span_suggestion_verbose(
2764+
base.span.shrink_to_hi(),
2765+
"consider `await`ing on the `Future` to access the field",
2766+
".await",
2767+
Applicability::MaybeIncorrect,
2768+
);
2769+
}
2770+
_ => {
2771+
let mut span: MultiSpan = base.span.into();
2772+
span.push_span_label(self.tcx.def_span(self.body_id), "this is not `async`");
2773+
err.span_note(
2774+
span,
2775+
"this implements `Future` and its output type has the field, \
2776+
but the future cannot be awaited in a synchronous function",
2777+
);
2778+
}
2779+
}
27662780
}
27672781

27682782
fn ban_nonexisting_field(

tests/ui/async-await/field-in-sync.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@ edition: 2021
2+
3+
struct S {
4+
field: (),
5+
}
6+
7+
async fn foo() -> S { todo!() }
8+
9+
fn main() -> Result<(), ()> {
10+
foo().field;
11+
//~^ ERROR no field `field` on type `impl Future<Output = S>`
12+
Ok(())
13+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0609]: no field `field` on type `impl Future<Output = S>`
2+
--> $DIR/field-in-sync.rs:10:11
3+
|
4+
LL | foo().field;
5+
| ^^^^^ field not available in `impl Future`, but it is available in its `Output`
6+
|
7+
note: this implements `Future` and its output type has the field, but the future cannot be awaited in a synchronous function
8+
--> $DIR/field-in-sync.rs:10:5
9+
|
10+
LL | fn main() -> Result<(), ()> {
11+
| --------------------------- this is not `async`
12+
LL | foo().field;
13+
| ^^^^^
14+
15+
error: aborting due to 1 previous error
16+
17+
For more information about this error, try `rustc --explain E0609`.

tests/ui/async-await/issue-61076.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ error[E0609]: no field `0` on type `impl Future<Output = Tuple>`
2828
LL | let _: i32 = tuple().0;
2929
| ^ field not available in `impl Future`, but it is available in its `Output`
3030
|
31-
help: consider `await`ing on the `Future` and access the field of its `Output`
31+
help: consider `await`ing on the `Future` to access the field
3232
|
3333
LL | let _: i32 = tuple().await.0;
3434
| ++++++
@@ -39,7 +39,7 @@ error[E0609]: no field `a` on type `impl Future<Output = Struct>`
3939
LL | let _: i32 = struct_().a;
4040
| ^ field not available in `impl Future`, but it is available in its `Output`
4141
|
42-
help: consider `await`ing on the `Future` and access the field of its `Output`
42+
help: consider `await`ing on the `Future` to access the field
4343
|
4444
LL | let _: i32 = struct_().await.a;
4545
| ++++++

0 commit comments

Comments
 (0)