@@ -142,12 +142,14 @@ fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
142
142
&& tcx. associated_item ( def_id) . container == ty:: AssocItemContainer :: TraitContainer
143
143
{
144
144
let sig = tcx. fn_sig ( def_id) . subst_identity ( ) ;
145
- sig. visit_with ( & mut ImplTraitInTraitFinder {
145
+ // We accounted for the binder of the fn sig, so skip the binder.
146
+ sig. skip_binder ( ) . visit_with ( & mut ImplTraitInTraitFinder {
146
147
tcx,
147
148
fn_def_id : def_id,
148
149
bound_vars : sig. bound_vars ( ) ,
149
150
predicates : & mut predicates,
150
151
seen : FxHashSet :: default ( ) ,
152
+ depth : ty:: INNERMOST ,
151
153
} ) ;
152
154
}
153
155
@@ -244,15 +246,36 @@ struct ImplTraitInTraitFinder<'a, 'tcx> {
244
246
fn_def_id : DefId ,
245
247
bound_vars : & ' tcx ty:: List < ty:: BoundVariableKind > ,
246
248
seen : FxHashSet < DefId > ,
249
+ depth : ty:: DebruijnIndex ,
247
250
}
248
251
249
252
impl < ' tcx > TypeVisitor < TyCtxt < ' tcx > > for ImplTraitInTraitFinder < ' _ , ' tcx > {
253
+ fn visit_binder < T : TypeVisitable < TyCtxt < ' tcx > > > (
254
+ & mut self ,
255
+ binder : & ty:: Binder < ' tcx , T > ,
256
+ ) -> std:: ops:: ControlFlow < Self :: BreakTy > {
257
+ self . depth . shift_in ( 1 ) ;
258
+ let binder = binder. super_visit_with ( self ) ;
259
+ self . depth . shift_out ( 1 ) ;
260
+ binder
261
+ }
262
+
250
263
fn visit_ty ( & mut self , ty : Ty < ' tcx > ) -> std:: ops:: ControlFlow < Self :: BreakTy > {
251
264
if let ty:: Alias ( ty:: Projection , alias_ty) = * ty. kind ( )
252
265
&& self . tcx . def_kind ( alias_ty. def_id ) == DefKind :: ImplTraitPlaceholder
253
266
&& self . tcx . impl_trait_in_trait_parent ( alias_ty. def_id ) == self . fn_def_id
254
267
&& self . seen . insert ( alias_ty. def_id )
255
268
{
269
+ // We have entered some binders as we've walked into the
270
+ // bounds of the RPITIT. Shift these binders back out when
271
+ // constructing the top-level projection predicate.
272
+ let alias_ty = self . tcx . fold_regions ( alias_ty, |re, _| {
273
+ if let ty:: ReLateBound ( index, bv) = re. kind ( ) {
274
+ self . tcx . mk_re_late_bound ( index. shifted_out_to_binder ( self . depth ) , bv)
275
+ } else {
276
+ re
277
+ }
278
+ } ) ;
256
279
self . predicates . push (
257
280
ty:: Binder :: bind_with_vars (
258
281
ty:: ProjectionPredicate {
0 commit comments