@@ -9,7 +9,7 @@ use std::ops::ControlFlow;
99use std:: { cmp, iter} ;
1010
1111use hir:: def:: DefKind ;
12- use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap , FxIndexSet } ;
12+ use rustc_data_structures:: fx:: { FxIndexMap , FxIndexSet } ;
1313use rustc_data_structures:: stack:: ensure_sufficient_stack;
1414use rustc_errors:: { Diag , EmissionGuarantee } ;
1515use rustc_hir as hir;
@@ -25,7 +25,6 @@ use rustc_middle::dep_graph::{DepNodeIndex, dep_kinds};
2525pub use rustc_middle:: traits:: select:: * ;
2626use rustc_middle:: ty:: abstract_const:: NotConstEvaluatable ;
2727use rustc_middle:: ty:: error:: TypeErrorToStringExt ;
28- use rustc_middle:: ty:: fold:: fold_regions;
2928use rustc_middle:: ty:: print:: { PrintTraitRefExt as _, with_no_trimmed_paths} ;
3029use rustc_middle:: ty:: {
3130 self , GenericArgsRef , PolyProjectionPredicate , Ty , TyCtxt , TypeFoldable , TypeVisitableExt ,
@@ -2199,8 +2198,8 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
21992198 }
22002199
22012200 ty:: CoroutineWitness ( def_id, args) => {
2202- let hidden_types = bind_coroutine_hidden_types_above (
2203- self . infcx ,
2201+ let hidden_types = rebind_coroutine_witness_types (
2202+ self . infcx . tcx ,
22042203 def_id,
22052204 args,
22062205 obligation. predicate . bound_vars ( ) ,
@@ -2348,7 +2347,7 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
23482347 }
23492348
23502349 ty:: CoroutineWitness ( def_id, args) => {
2351- bind_coroutine_hidden_types_above ( self . infcx , def_id, args, t. bound_vars ( ) )
2350+ rebind_coroutine_witness_types ( self . infcx . tcx , def_id, args, t. bound_vars ( ) )
23522351 }
23532352
23542353 // For `PhantomData<T>`, we pass `T`.
@@ -2843,6 +2842,23 @@ impl<'tcx> SelectionContext<'_, 'tcx> {
28432842 }
28442843}
28452844
2845+ fn rebind_coroutine_witness_types < ' tcx > (
2846+ tcx : TyCtxt < ' tcx > ,
2847+ def_id : DefId ,
2848+ args : ty:: GenericArgsRef < ' tcx > ,
2849+ bound_vars : & ' tcx ty:: List < ty:: BoundVariableKind > ,
2850+ ) -> ty:: Binder < ' tcx , Vec < Ty < ' tcx > > > {
2851+ let bound_coroutine_types = tcx. coroutine_hidden_types ( def_id) . skip_binder ( ) ;
2852+ let shifted_coroutine_types =
2853+ tcx. shift_bound_var_indices ( bound_vars. len ( ) , bound_coroutine_types. skip_binder ( ) ) ;
2854+ ty:: Binder :: bind_with_vars (
2855+ ty:: EarlyBinder :: bind ( shifted_coroutine_types. to_vec ( ) ) . instantiate ( tcx, args) ,
2856+ tcx. mk_bound_variable_kinds_from_iter (
2857+ bound_vars. iter ( ) . chain ( bound_coroutine_types. bound_vars ( ) ) ,
2858+ ) ,
2859+ )
2860+ }
2861+
28462862impl < ' o , ' tcx > TraitObligationStack < ' o , ' tcx > {
28472863 fn list ( & ' o self ) -> TraitObligationStackList < ' o , ' tcx > {
28482864 TraitObligationStackList :: with ( self )
@@ -3151,56 +3167,3 @@ pub(crate) enum ProjectionMatchesProjection {
31513167 Ambiguous ,
31523168 No ,
31533169}
3154-
3155- /// Replace all regions inside the coroutine interior with late bound regions.
3156- /// Note that each region slot in the types gets a new fresh late bound region, which means that
3157- /// none of the regions inside relate to any other, even if typeck had previously found constraints
3158- /// that would cause them to be related.
3159- #[ instrument( level = "trace" , skip( infcx) , ret) ]
3160- fn bind_coroutine_hidden_types_above < ' tcx > (
3161- infcx : & InferCtxt < ' tcx > ,
3162- def_id : DefId ,
3163- args : ty:: GenericArgsRef < ' tcx > ,
3164- bound_vars : & ty:: List < ty:: BoundVariableKind > ,
3165- ) -> ty:: Binder < ' tcx , Vec < Ty < ' tcx > > > {
3166- let tcx = infcx. tcx ;
3167- let mut seen_tys = FxHashSet :: default ( ) ;
3168-
3169- let considering_regions = infcx. considering_regions ;
3170-
3171- let num_bound_variables = bound_vars. len ( ) as u32 ;
3172- let mut counter = num_bound_variables;
3173-
3174- let hidden_types: Vec < _ > = tcx
3175- . coroutine_hidden_types ( def_id)
3176- // Deduplicate tys to avoid repeated work.
3177- . filter ( |bty| seen_tys. insert ( * bty) )
3178- . map ( |mut bty| {
3179- // Only remap erased regions if we use them.
3180- if considering_regions {
3181- bty = bty. map_bound ( |ty| {
3182- fold_regions ( tcx, ty, |r, current_depth| match r. kind ( ) {
3183- ty:: ReErased => {
3184- let br = ty:: BoundRegion {
3185- var : ty:: BoundVar :: from_u32 ( counter) ,
3186- kind : ty:: BoundRegionKind :: Anon ,
3187- } ;
3188- counter += 1 ;
3189- ty:: Region :: new_bound ( tcx, current_depth, br)
3190- }
3191- r => bug ! ( "unexpected region: {r:?}" ) ,
3192- } )
3193- } )
3194- }
3195-
3196- bty. instantiate ( tcx, args)
3197- } )
3198- . collect ( ) ;
3199- let bound_vars = tcx. mk_bound_variable_kinds_from_iter (
3200- bound_vars. iter ( ) . chain (
3201- ( num_bound_variables..counter)
3202- . map ( |_| ty:: BoundVariableKind :: Region ( ty:: BoundRegionKind :: Anon ) ) ,
3203- ) ,
3204- ) ;
3205- ty:: Binder :: bind_with_vars ( hidden_types, bound_vars)
3206- }
0 commit comments