Skip to content

Commit bb33200

Browse files
Make sure all kinds of generators only return unit
1 parent 454bff7 commit bb33200

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

compiler/rustc_hir_typeck/src/closure.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -650,9 +650,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
650650
},
651651
)
652652
}
653-
// For a `gen {}` block created as a `gen fn` body, we need the return type to be
654-
// ().
655-
Some(hir::CoroutineKind::Gen(hir::CoroutineSource::Fn)) => self.tcx.types.unit,
653+
// All `gen {}` and `async gen {}` must return unit.
654+
Some(hir::CoroutineKind::Gen(_) | hir::CoroutineKind::AsyncGen(_)) => {
655+
self.tcx.types.unit
656+
}
656657

657658
_ => astconv.ty_infer(None, decl.output.span()),
658659
},

tests/ui/coroutine/return-types.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// compile-flags: --edition 2024 -Zunstable-options
2+
3+
#![feature(gen_blocks)]
4+
5+
async gen fn async_gen_fn() -> i32 { 0 }
6+
//~^ ERROR mismatched types
7+
8+
gen fn gen_fn() -> i32 { 0 }
9+
//~^ ERROR mismatched types
10+
11+
fn async_gen_block() {
12+
async gen { yield (); 1 };
13+
//~^ ERROR mismatched types
14+
}
15+
16+
fn gen_block() {
17+
gen { yield (); 1 };
18+
//~^ ERROR mismatched types
19+
}
20+
21+
fn main() {}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/return-types.rs:5:38
3+
|
4+
LL | async gen fn async_gen_fn() -> i32 { 0 }
5+
| --- ^ expected `()`, found integer
6+
| |
7+
| expected `()` because of return type
8+
9+
error[E0308]: mismatched types
10+
--> $DIR/return-types.rs:8:26
11+
|
12+
LL | gen fn gen_fn() -> i32 { 0 }
13+
| --- ^ expected `()`, found integer
14+
| |
15+
| expected `()` because of return type
16+
17+
error[E0308]: mismatched types
18+
--> $DIR/return-types.rs:12:27
19+
|
20+
LL | async gen { yield (); 1 };
21+
| ^ expected `()`, found integer
22+
23+
error[E0308]: mismatched types
24+
--> $DIR/return-types.rs:17:21
25+
|
26+
LL | gen { yield (); 1 };
27+
| ^ expected `()`, found integer
28+
29+
error: aborting due to 4 previous errors
30+
31+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)