Skip to content

ConstructCoroutineInClosureShim missing track_caller field, async closure dyn dispatch loses caller location #498

@SebTardif

Description

@SebTardif

Summary

ConstructCoroutineInClosureShim (used for FnMut::call_mut / Fn::call dispatch on async closures) does not propagate #[track_caller], unlike ClosureOnceShim which has an explicit track_caller: bool field.

Location

compiler/rustc_middle/src/ty/instance.rs, line 311-323

Bug

pub fn requires_caller_location(&self, tcx: TyCt<'_>) -> bool {
    match *self {
        InstanceKind::Item(def_id)
        | InstanceKind::Virtual(def_id, _)
        | InstanceKind::VTableShim(def_id) => {
            tcx.body_codegen_attrs(def_id).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
        }
        InstanceKind::ClosureOnceShim { track_caller, .. } => track_caller,
        _ => false,  // ConstructCoroutineInClosureShim falls here
    }
}

ClosureOnceShim has track_caller: bool and checks it. ConstructCoroutineInClosureShim has no such field and returns false. If an async closure with #[track_caller] is called through dyn FnMut or dyn Fn (which goes through ConstructCoroutineInClosureShim), the caller location is lost.

Impact

Currently latent: both closure_track_caller (rust-lang#87417) and async_fn_track_caller (rust-lang#110011) are unstable feature gates. The bug would become user-facing when either feature stabilizes.

Fix

Add a track_caller: bool field to ConstructCoroutineInClosureShim and wire it through requires_caller_location.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-codegenArea: code generationA-type-systemType systemI-wrongWrong result or data corruptionP-lowLow impact: edge case or niche scenariobugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions