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

  1. File a rustc ICE with the minimized gen fn repro (note the unstable feature).
  2. Avoid the gen-fn form that triggers the region-naming path (rewrite as a gen block or iterator).
  3. Bisect with cargo bisect-rustc across nightlies.
  4. Pin to a known-good nightly.
Defensive patterns

Strategy: fallback

Prevention

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


AI-assisted analysis of rust-lang/rust@7088e4b63a (2026-08-10). Data as JSON: /api/errors/dca2127bad253c97. Report an issue: GitHub.