Make sure all kinds of generators only return unit

This commit is contained in:
Michael Goulet 2023-12-18 01:45:09 +00:00
parent 454bff7682
commit bb33200047
3 changed files with 56 additions and 3 deletions

View File

@ -650,9 +650,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
},
)
}
// For a `gen {}` block created as a `gen fn` body, we need the return type to be
// ().
Some(hir::CoroutineKind::Gen(hir::CoroutineSource::Fn)) => self.tcx.types.unit,
// All `gen {}` and `async gen {}` must return unit.
Some(hir::CoroutineKind::Gen(_) | hir::CoroutineKind::AsyncGen(_)) => {
self.tcx.types.unit
}
_ => astconv.ty_infer(None, decl.output.span()),
},

View File

@ -0,0 +1,21 @@
// compile-flags: --edition 2024 -Zunstable-options
#![feature(gen_blocks)]
async gen fn async_gen_fn() -> i32 { 0 }
//~^ ERROR mismatched types
gen fn gen_fn() -> i32 { 0 }
//~^ ERROR mismatched types
fn async_gen_block() {
async gen { yield (); 1 };
//~^ ERROR mismatched types
}
fn gen_block() {
gen { yield (); 1 };
//~^ ERROR mismatched types
}
fn main() {}

View File

@ -0,0 +1,31 @@
error[E0308]: mismatched types
--> $DIR/return-types.rs:5:38
|
LL | async gen fn async_gen_fn() -> i32 { 0 }
| --- ^ expected `()`, found integer
| |
| expected `()` because of return type
error[E0308]: mismatched types
--> $DIR/return-types.rs:8:26
|
LL | gen fn gen_fn() -> i32 { 0 }
| --- ^ expected `()`, found integer
| |
| expected `()` because of return type
error[E0308]: mismatched types
--> $DIR/return-types.rs:12:27
|
LL | async gen { yield (); 1 };
| ^ expected `()`, found integer
error[E0308]: mismatched types
--> $DIR/return-types.rs:17:21
|
LL | gen { yield (); 1 };
| ^ expected `()`, found integer
error: aborting due to 4 previous errors
For more information about this error, try `rustc --explain E0308`.