Skip to content

Desugar yield in async gen correctly, ensure gen always returns unit #119061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions compiler/rustc_hir_typeck/src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
},
Expand Down
21 changes: 21 additions & 0 deletions tests/ui/coroutine/return-types.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 31 additions & 0 deletions tests/ui/coroutine/return-types.stderr
Original file line number Diff line number Diff line change
@@ -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`.