Repository: rust-lang/rust
File: compiler/rustc_hir_analysis/src/collect.rs (Rust)

Code before:
    sig: &'tcx hir::FnSig<'tcx>,
    generics: &'tcx hir::Generics<'tcx>,
    def_id: LocalDefId,
) -> ty::PolyFnSig<'tcx> {
    if let Some(infer_ret_ty) = sig.decl.output.is_suggestable_infer_ty() {
        return recover_infer_ret_ty(icx, infer_ret_ty, generics, def_id);
    }

    icx.lowerer().lower_fn_ty(
        icx.tcx().local_def_id_to_hir_id(def_id),
        sig.header.safety(),
        sig.header.abi,
        sig.decl,
        Some(generics),
        None,
    )
}

/// Convert `ReLateParam`s in `value` back into `ReBound`s so the returned
/// `PolyFnSig` properly binds all late-bound vars.
fn late_param_regions_to_bound<'tcx, T>(
    tcx: TyCtxt<'tcx>,
    scope: DefId,
    bound_vars: &'tcx ty::List<ty::BoundVariableKind<'tcx>>,
    value: T,
) -> T
where
    T: ty::TypeFoldable<TyCtxt<'tcx>>,
{
    fold_regions(tcx, value, |r, debruijn| match r.kind() {
        ty::ReLateParam(lp) => {
            // Should be in scope, otherwise inconsistency happens somewhere.
            assert_eq!(lp.scope, scope);

            let br = match lp.kind {
                // These variants preserve the bound var index.
                kind @ (ty::LateParamRegionKind::Anon(idx)
                | ty::LateParamRegionKind::NamedAnon(idx, _)) => {
                    let idx = idx as usize;
                    let var = ty::BoundVar::from_usize(idx);

                    let Some(ty::BoundVariableKind::Region(kind)) = bound_vars.get(idx).copied()
                    else {
                        bug!("unexpected late-bound region {kind:?} for bound vars {bound_vars:?}");
                    };

                    ty::BoundRegion { var, kind }
                }

                // For named regions, look up the corresponding bound var.
                ty::LateParamRegionKind::Named(def_id) => bound_vars

Review comment (suggestion):
return a `Binder` instead of doing so in the caller :3

Code after:
    sig: &'tcx hir::FnSig<'tcx>,
    generics: &'tcx hir::Generics<'tcx>,
    def_id: LocalDefId,
) -> ty::PolyFnSig<'tcx> {
    if let Some(infer_ret_ty) = sig.decl.output.is_suggestable_infer_ty() {
        return recover_infer_ret_ty(icx, infer_ret_ty, generics, def_id);
    }

    icx.lowerer().lower_fn_ty(
        icx.tcx().local_def_id_to_hir_id(def_id),
        sig.header.safety(),
        sig.header.abi,
        sig.decl,
        Some(generics),
        None,
    )
}

/// Convert `ReLateParam`s in `value` back into `ReBound`s and bind it with `bound_vars`.
fn late_param_regions_to_bound<'tcx, T>(
    tcx: TyCtxt<'tcx>,
    scope: DefId,
    bound_vars: &'tcx ty::List<ty::BoundVariableKind<'tcx>>,
    value: T,
) -> ty::Binder<'tcx, T>
where
    T: ty::TypeFoldable<TyCtxt<'tcx>>,
{
    let value = fold_regions(tcx, value, |r, debruijn| match r.kind() {
        ty::ReLateParam(lp) => {
            // Should be in scope, otherwise inconsistency happens somewhere.
            assert_eq!(lp.scope, scope);

            let br = match lp.kind {
                // These variants preserve the bound var index.
                kind @ (ty::LateParamRegionKind::Anon(idx)
                | ty::LateParamRegionKind::NamedAnon(idx, _)) => {
                    let idx = idx as usize;
                    let var = ty::BoundVar::from_usize(idx);

                    let Some(ty::BoundVariableKind::Region(kind)) = bound_vars.get(idx).copied()
                    else {
                        bug!("unexpected late-bound region {kind:?} for bound vars {bound_vars:?}");
                    };

                    ty::BoundRegion { var, kind }
                }

                // For named regions, look up the corresponding bound var.
                ty::LateParamRegionKind::Named(def_id) => bound_vars
                    .iter()