Skip to content

Commit 48730f9

Browse files
Don't call coroutines async fns
1 parent 389aee6 commit 48730f9

15 files changed

+36
-32
lines changed

compiler/rustc_middle/src/ty/util.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,13 @@ impl<'tcx> TyCtxt<'tcx> {
747747
match def_kind {
748748
DefKind::AssocFn if self.associated_item(def_id).fn_has_self_parameter => "method",
749749
DefKind::Coroutine => match self.coroutine_kind(def_id).unwrap() {
750-
rustc_hir::CoroutineKind::Async(..) => "async closure",
751-
rustc_hir::CoroutineKind::Coroutine => "coroutine",
752-
rustc_hir::CoroutineKind::Gen(..) => "gen closure",
750+
hir::CoroutineKind::Async(hir::CoroutineSource::Fn) => "async fn",
751+
hir::CoroutineKind::Async(hir::CoroutineSource::Block) => "async block",
752+
hir::CoroutineKind::Async(hir::CoroutineSource::Closure) => "async closure",
753+
hir::CoroutineKind::Gen(hir::CoroutineSource::Fn) => "gen fn",
754+
hir::CoroutineKind::Gen(hir::CoroutineSource::Block) => "gen block",
755+
hir::CoroutineKind::Gen(hir::CoroutineSource::Closure) => "gen closure",
756+
hir::CoroutineKind::Coroutine => "coroutine",
753757
},
754758
_ => def_kind.descr(def_id),
755759
}
@@ -765,9 +769,9 @@ impl<'tcx> TyCtxt<'tcx> {
765769
match def_kind {
766770
DefKind::AssocFn if self.associated_item(def_id).fn_has_self_parameter => "a",
767771
DefKind::Coroutine => match self.coroutine_kind(def_id).unwrap() {
768-
rustc_hir::CoroutineKind::Async(..) => "an",
769-
rustc_hir::CoroutineKind::Coroutine => "a",
770-
rustc_hir::CoroutineKind::Gen(..) => "a",
772+
hir::CoroutineKind::Async(..) => "an",
773+
hir::CoroutineKind::Coroutine => "a",
774+
hir::CoroutineKind::Gen(..) => "a",
771775
},
772776
_ => def_kind.article(),
773777
}

compiler/rustc_middle/src/values.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,11 @@ impl<'tcx, T> Value<TyCtxt<'tcx>> for Result<T, &'_ ty::layout::LayoutError<'_>>
138138
let guar = if cycle_error.cycle[0].query.dep_kind == dep_kinds::layout_of
139139
&& let Some(def_id) = cycle_error.cycle[0].query.ty_def_id
140140
&& let Some(def_id) = def_id.as_local()
141-
&& matches!(tcx.def_kind(def_id), DefKind::Coroutine)
141+
&& let def_kind = tcx.def_kind(def_id)
142+
&& matches!(def_kind, DefKind::Coroutine)
142143
{
143-
let hir = tcx.hir();
144-
let coroutine_kind = hir
145-
.body(hir.body_owned_by(def_id))
146-
.coroutine_kind
144+
let coroutine_kind = tcx
145+
.coroutine_kind(def_id)
147146
.expect("expected coroutine to have a coroutine_kind");
148147
// FIXME: `def_span` for an fn-like coroutine will point to the fn's body
149148
// due to interactions between the desugaring into a closure expr and the
@@ -158,12 +157,16 @@ impl<'tcx, T> Value<TyCtxt<'tcx>> for Result<T, &'_ ty::layout::LayoutError<'_>>
158157
tcx.sess,
159158
span,
160159
E0733,
161-
"recursion in an `async fn` requires boxing"
162-
);
163-
diag.note("a recursive `async fn` call must introduce indirection such as `Box::pin` to avoid an infinitely sized future");
164-
diag.note(
165-
"consider using the `async_recursion` crate: https://crates.io/crates/async_recursion",
160+
"recursion in {} {} requires boxing",
161+
tcx.def_kind_descr_article(def_kind, def_id.to_def_id()),
162+
tcx.def_kind_descr(def_kind, def_id.to_def_id()),
166163
);
164+
if matches!(coroutine_kind, hir::CoroutineKind::Async(_)) {
165+
diag.note("a recursive `async fn` call must introduce indirection such as `Box::pin` to avoid an infinitely sized future");
166+
diag.note(
167+
"consider using the `async_recursion` crate: https://crates.io/crates/async_recursion",
168+
);
169+
}
167170
let mut called = false;
168171
for (i, frame) in cycle_error.cycle.iter().enumerate() {
169172
if frame.query.dep_kind != dep_kinds::layout_of {

tests/ui/async-await/in-trait/async-recursive-generic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ trait MyTrait<T> {
66

77
impl<T> MyTrait<T> for T where T: Copy {
88
async fn foo_recursive(&self, n: usize) -> T {
9-
//~^ ERROR recursion in an `async fn` requires boxing
9+
//~^ ERROR recursion in an async fn requires boxing
1010
if n > 0 {
1111
self.foo_recursive(n - 1).await
1212
} else {

tests/ui/async-await/in-trait/async-recursive-generic.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0733]: recursion in an `async fn` requires boxing
1+
error[E0733]: recursion in an async fn requires boxing
22
--> $DIR/async-recursive-generic.rs:8:5
33
|
44
LL | async fn foo_recursive(&self, n: usize) -> T {

tests/ui/async-await/in-trait/async-recursive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ trait MyTrait {
66

77
impl MyTrait for i32 {
88
async fn foo_recursive(&self, n: usize) -> i32 {
9-
//~^ ERROR recursion in an `async fn` requires boxing
9+
//~^ ERROR recursion in an async fn requires boxing
1010
if n > 0 {
1111
self.foo_recursive(n - 1).await
1212
} else {

tests/ui/async-await/in-trait/async-recursive.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0733]: recursion in an `async fn` requires boxing
1+
error[E0733]: recursion in an async fn requires boxing
22
--> $DIR/async-recursive.rs:8:5
33
|
44
LL | async fn foo_recursive(&self, n: usize) -> i32 {

tests/ui/async-await/in-trait/indirect-recursion-issue-112047.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ where
3030
C: First,
3131
{
3232
async fn second(self) {
33-
//~^ ERROR recursion in an `async fn` requires boxing
33+
//~^ ERROR recursion in an async fn requires boxing
3434
self.first().await.second().await;
3535
}
3636
}

tests/ui/async-await/in-trait/indirect-recursion-issue-112047.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0733]: recursion in an `async fn` requires boxing
1+
error[E0733]: recursion in an async fn requires boxing
22
--> $DIR/indirect-recursion-issue-112047.rs:32:5
33
|
44
LL | async fn second(self) {

tests/ui/async-await/mutually-recursive-async-impl-trait-type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Test that impl trait does not allow creating recursive types that are
33
// otherwise forbidden when using `async` and `await`.
44

5-
async fn rec_1() { //~ ERROR recursion in an `async fn`
5+
async fn rec_1() { //~ ERROR recursion in an async fn
66
rec_2().await;
77
}
88

tests/ui/async-await/mutually-recursive-async-impl-trait-type.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0733]: recursion in an `async fn` requires boxing
1+
error[E0733]: recursion in an async fn requires boxing
22
--> $DIR/mutually-recursive-async-impl-trait-type.rs:5:1
33
|
44
LL | async fn rec_1() {

tests/ui/async-await/recursive-async-impl-trait-type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// otherwise forbidden when using `async` and `await`.
44

55
async fn recursive_async_function() -> () {
6-
//~^ ERROR recursion in an `async fn` requires boxing
6+
//~^ ERROR recursion in an async fn requires boxing
77
recursive_async_function().await;
88
}
99

tests/ui/async-await/recursive-async-impl-trait-type.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0733]: recursion in an `async fn` requires boxing
1+
error[E0733]: recursion in an async fn requires boxing
22
--> $DIR/recursive-async-impl-trait-type.rs:5:1
33
|
44
LL | async fn recursive_async_function() -> () {

tests/ui/impl-trait/recursive-impl-trait-type-indirect.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,14 @@ LL |
109109
LL | (substs_change::<&T>(),)
110110
| ------------------------ returning here with type `(impl Sized,)`
111111

112-
error[E0733]: recursion in an `async fn` requires boxing
112+
error[E0733]: recursion in a coroutine requires boxing
113113
--> $DIR/recursive-impl-trait-type-indirect.rs:73:5
114114
|
115115
LL | move || {
116116
| ^^^^^^^
117117
LL |
118118
LL | let x = coroutine_hold();
119119
| - recursive call here
120-
|
121-
= note: a recursive `async fn` call must introduce indirection such as `Box::pin` to avoid an infinitely sized future
122-
= note: consider using the `async_recursion` crate: https://crates.io/crates/async_recursion
123120

124121
error[E0720]: cannot resolve opaque type
125122
--> $DIR/recursive-impl-trait-type-indirect.rs:86:26

tests/ui/type-alias-impl-trait/indirect-recursion-issue-112047.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ impl Recur for () {
1919

2020
fn recur(self) -> Self::Recur {
2121
async move { recur(self).await; }
22-
//~^ ERROR recursion in an `async fn` requires boxing
22+
//~^ ERROR recursion in an async block requires boxing
2323
}
2424
}
2525

tests/ui/type-alias-impl-trait/indirect-recursion-issue-112047.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
error[E0733]: recursion in an `async fn` requires boxing
1+
error[E0733]: recursion in an async block requires boxing
22
--> $DIR/indirect-recursion-issue-112047.rs:21:9
33
|
44
LL | t.recur().await;

0 commit comments

Comments
 (0)