rust-lang/rust · error
coroutine lowered from gen fn should be in fn
Error message
coroutine lowered from gen fn should be in fn
What it means
region_name.rs:871: the gen-fn analogue of error 258. For a coroutine desugared from a `gen fn` (CoroutineDesugaring::Gen, CoroutineSource::Fn), the diagnostic does parent_item.fn_decl().expect("coroutine lowered from gen fn should be in fn"). The invariant: a gen-fn coroutine's parent is always an fn with a declaration.
Source
Thrown at compiler/rustc_borrowck/src/diagnostics/region_name.rs:871
)) => " of gen block",
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
hir::CoroutineDesugaring::Gen,
hir::CoroutineSource::Closure,
))
| hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Gen) => {
" of gen closure"
}
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
hir::CoroutineDesugaring::Gen,
hir::CoroutineSource::Fn,
)) => {
let parent_item =
tcx.hir_node_by_def_id(tcx.hir_get_parent_item(mir_hir_id).def_id);
let output = &parent_item
.fn_decl()
.expect("coroutine lowered from gen fn should be in fn")
.output;
span = output.span();
" of gen function"
}
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
hir::CoroutineDesugaring::AsyncGen,
hir::CoroutineSource::Block,
)) => " of async gen block",
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
hir::CoroutineDesugaring::AsyncGen,
hir::CoroutineSource::Closure,
))
| hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::AsyncGen) => {
" of async gen closure"
}
View on GitHub (pinned to 7088e4b63a)
Solutions
- File a rustc ICE with the minimized gen fn repro (note the unstable feature).
- Avoid the gen-fn form that triggers the region-naming path (rewrite as a gen block or iterator).
- Bisect with cargo bisect-rustc across nightlies.
- Pin to a known-good nightly.
Defensive patterns
Strategy: fallback
Prevention
- Treat as a borrowck diagnostic ICE on the unstable gen-fn feature; report with a minimized repro.
- Avoid gen-fn forms that trigger region-naming; rewrite as gen blocks or iterators.
- Bisect with cargo bisect-rustc across nightlies.
- Pin to a known-good nightly when using gen fn.
When it happens
Trigger: Region-naming for a borrowck error inside a gen fn (unstable gen blocks/fns feature) where the parent item unexpectedly lacks a fn_decl.
Common situations: A nightly ICE with the unstable gen-fn feature when reporting a lifetime/borrow error inside the generator.
Related errors
- coroutine lowered from async fn should be in fn
- `async gen` closures not supported yet
- immutable unnamed local
- return depth from strip_n_refs should be smaller than the in
- coroutine lowered from async gen fn should be in fn
AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10).
Data as JSON: /api/errors/dca2127bad253c97.
Report an issue: GitHub.