From 45bc47e4e26f29450dd679c3adac249eae28d220 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 12 Feb 2026 20:16:33 +0900 Subject: [PATCH] Apply review suggestions --- compiler/rustc_hir_analysis/src/collect.rs | 106 +++++++++++++-------- 1 file changed, 64 insertions(+), 42 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/collect.rs b/compiler/rustc_hir_analysis/src/collect.rs index 29a2e7e36220..1cd96cf6a54d 100644 --- a/compiler/rustc_hir_analysis/src/collect.rs +++ b/compiler/rustc_hir_analysis/src/collect.rs @@ -1063,6 +1063,69 @@ fn lower_fn_sig_recovering_infer_ret_ty<'tcx>( ) } +/// 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>, + value: T, +) -> T +where + T: ty::TypeFoldable>, +{ + 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() + .enumerate() + .find_map(|(idx, bv)| match bv { + ty::BoundVariableKind::Region(kind @ ty::BoundRegionKind::Named(did)) + if did == def_id => + { + Some(ty::BoundRegion { var: ty::BoundVar::from_usize(idx), kind }) + } + _ => None, + }) + .unwrap(), + + ty::LateParamRegionKind::ClosureEnv => bound_vars + .iter() + .enumerate() + .find_map(|(idx, bv)| match bv { + ty::BoundVariableKind::Region(kind @ ty::BoundRegionKind::ClosureEnv) => { + Some(ty::BoundRegion { var: ty::BoundVar::from_usize(idx), kind }) + } + _ => None, + }) + .unwrap(), + }; + + ty::Region::new_bound(tcx, debruijn, br) + } + _ => r, + }) +} + fn recover_infer_ret_ty<'tcx>( icx: &ItemCtxt<'tcx>, infer_ret_ty: &'tcx hir::Ty<'tcx>, @@ -1153,48 +1216,7 @@ fn recover_infer_ret_ty<'tcx>( fn_sig.abi, ); - let fn_sig = fold_regions(tcx, fn_sig, |r, _| match r.kind() { - ty::ReLateParam(lp) if lp.scope == scope => { - let br = match lp.kind { - ty::LateParamRegionKind::Anon(idx) | ty::LateParamRegionKind::NamedAnon(idx, _) => { - let idx = idx as usize; - match bound_vars.get(idx).copied() { - Some(ty::BoundVariableKind::Region(br_kind)) => Some(ty::BoundRegion { - var: ty::BoundVar::from_usize(idx), - kind: br_kind, - }), - _ => None, - } - } - ty::LateParamRegionKind::Named(def_id) => { - bound_vars.iter().enumerate().find_map(|(idx, bv)| match bv { - ty::BoundVariableKind::Region( - br_kind @ ty::BoundRegionKind::Named(did), - ) if did == def_id => Some(ty::BoundRegion { - var: ty::BoundVar::from_usize(idx), - kind: br_kind, - }), - _ => None, - }) - } - ty::LateParamRegionKind::ClosureEnv => { - bound_vars.iter().enumerate().find_map(|(idx, bv)| match bv { - ty::BoundVariableKind::Region( - br_kind @ ty::BoundRegionKind::ClosureEnv, - ) => Some(ty::BoundRegion { - var: ty::BoundVar::from_usize(idx), - kind: br_kind, - }), - _ => None, - }) - } - }; - - br.map(|br| ty::Region::new_bound(tcx, ty::INNERMOST, br)) - .unwrap_or_else(|| ty::Region::new_error(tcx, guar)) - } - _ => r, - }); + let fn_sig = late_param_regions_to_bound(tcx, scope, bound_vars, fn_sig); ty::Binder::bind_with_vars(fn_sig, bound_vars) }