mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 23:04:33 +00:00
Make sure all kinds of generators only return unit
This commit is contained in:
parent
454bff7682
commit
bb33200047
@ -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()),
|
||||
},
|
||||
|
21
tests/ui/coroutine/return-types.rs
Normal file
21
tests/ui/coroutine/return-types.rs
Normal 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() {}
|
31
tests/ui/coroutine/return-types.stderr
Normal file
31
tests/ui/coroutine/return-types.stderr
Normal 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`.
|
Loading…
Reference in New Issue
Block a user