Skip to content

describe_field_from_ty missing CoroutineClosure, borrow errors show field index instead of capture name #478

Description

@SebTardif

Bug

describe_field_from_ty in compiler/rustc_borrowck/src/diagnostics/mod.rs (line 529) matches ty::Closure(def_id, _) | ty::Coroutine(def_id, _) to resolve captured variable names for borrow checker diagnostics. ty::CoroutineClosure is not handled, causing it to fall through to the catch-all _ => which returns a numeric field index (e.g., "field 0") instead of the captured variable name.

Evidence

ty::Closure(def_id, _) | ty::Coroutine(def_id, _) => {
    let def_id = def_id.expect_local();
    let var_id = self.infcx.tcx.closure_captures(def_id)[field.index()].get_root_variable();
    Some(self.infcx.tcx.hir_name(var_id).to_string())
}
_ => {
    // Might need a revision when the fields in trait
    // `Fn(Arg, Args...)` -> (Ret1, Ret2, ..determine-)
    // can be referred to by the user
    None
}

Impact

When the borrow checker reports errors involving captured variables inside async closures, the error message shows "field 0", "field 1", etc. instead of the actual variable name. This makes borrow checker errors harder to understand for async closure patterns. Async closures have been stable since Rust 1.85.

PR #482

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsCompiler diagnosticsI-wrongWrong result or data corruptionP-mediumMedium impact: affects specific usage patternsbugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions