@@ -39,6 +39,7 @@ use crate::{
39
39
pub struct TyLoweringContext < ' a > {
40
40
pub db : & ' a dyn HirDatabase ,
41
41
pub resolver : & ' a Resolver ,
42
+ in_binders : DebruijnIndex ,
42
43
/// Note: Conceptually, it's thinkable that we could be in a location where
43
44
/// some type params should be represented as placeholders, and others
44
45
/// should be converted to variables. I think in practice, this isn't
@@ -53,7 +54,27 @@ impl<'a> TyLoweringContext<'a> {
53
54
let impl_trait_counter = std:: cell:: Cell :: new ( 0 ) ;
54
55
let impl_trait_mode = ImplTraitLoweringMode :: Disallowed ;
55
56
let type_param_mode = TypeParamLoweringMode :: Placeholder ;
56
- Self { db, resolver, impl_trait_mode, impl_trait_counter, type_param_mode }
57
+ let in_binders = DebruijnIndex :: INNERMOST ;
58
+ Self { db, resolver, in_binders, impl_trait_mode, impl_trait_counter, type_param_mode }
59
+ }
60
+
61
+ pub fn with_shifted_in < T > (
62
+ & self ,
63
+ debruijn : DebruijnIndex ,
64
+ f : impl FnOnce ( & TyLoweringContext ) -> T ,
65
+ ) -> T {
66
+ let new_ctx = Self {
67
+ in_binders : self . in_binders . shifted_in_from ( debruijn) ,
68
+ impl_trait_counter : std:: cell:: Cell :: new ( self . impl_trait_counter . get ( ) ) ,
69
+ ..* self
70
+ } ;
71
+ let result = f ( & new_ctx) ;
72
+ self . impl_trait_counter . set ( new_ctx. impl_trait_counter . get ( ) ) ;
73
+ result
74
+ }
75
+
76
+ pub fn shifted_in ( self , debruijn : DebruijnIndex ) -> Self {
77
+ Self { in_binders : self . in_binders . shifted_in_from ( debruijn) , ..self }
57
78
}
58
79
59
80
pub fn with_impl_trait_mode ( self , impl_trait_mode : ImplTraitLoweringMode ) -> Self {
@@ -134,22 +155,26 @@ impl Ty {
134
155
}
135
156
TypeRef :: DynTrait ( bounds) => {
136
157
let self_ty = Ty :: Bound ( BoundVar :: new ( DebruijnIndex :: INNERMOST , 0 ) ) ;
137
- let predicates = bounds
138
- . iter ( )
139
- . flat_map ( |b| GenericPredicate :: from_type_bound ( ctx, b, self_ty. clone ( ) ) )
140
- . collect ( ) ;
158
+ let predicates = ctx. with_shifted_in ( DebruijnIndex :: ONE , |ctx| {
159
+ bounds
160
+ . iter ( )
161
+ . flat_map ( |b| GenericPredicate :: from_type_bound ( ctx, b, self_ty. clone ( ) ) )
162
+ . collect ( )
163
+ } ) ;
141
164
Ty :: Dyn ( predicates)
142
165
}
143
166
TypeRef :: ImplTrait ( bounds) => {
144
167
match ctx. impl_trait_mode {
145
168
ImplTraitLoweringMode :: Opaque => {
146
169
let self_ty = Ty :: Bound ( BoundVar :: new ( DebruijnIndex :: INNERMOST , 0 ) ) ;
147
- let predicates = bounds
148
- . iter ( )
149
- . flat_map ( |b| {
150
- GenericPredicate :: from_type_bound ( ctx, b, self_ty. clone ( ) )
151
- } )
152
- . collect ( ) ;
170
+ let predicates = ctx. with_shifted_in ( DebruijnIndex :: ONE , |ctx| {
171
+ bounds
172
+ . iter ( )
173
+ . flat_map ( |b| {
174
+ GenericPredicate :: from_type_bound ( ctx, b, self_ty. clone ( ) )
175
+ } )
176
+ . collect ( )
177
+ } ) ;
153
178
Ty :: Opaque ( predicates)
154
179
}
155
180
ImplTraitLoweringMode :: Param => {
@@ -180,7 +205,7 @@ impl Ty {
180
205
( 0 , 0 , 0 , 0 )
181
206
} ;
182
207
Ty :: Bound ( BoundVar :: new (
183
- DebruijnIndex :: INNERMOST ,
208
+ ctx . in_binders ,
184
209
idx as usize + parent_params + self_params + list_params,
185
210
) )
186
211
}
@@ -293,7 +318,7 @@ impl Ty {
293
318
TypeParamLoweringMode :: Placeholder => Ty :: Placeholder ( param_id) ,
294
319
TypeParamLoweringMode :: Variable => {
295
320
let idx = generics. param_idx ( param_id) . expect ( "matching generics" ) ;
296
- Ty :: Bound ( BoundVar :: new ( DebruijnIndex :: INNERMOST , idx) )
321
+ Ty :: Bound ( BoundVar :: new ( ctx . in_binders , idx) )
297
322
}
298
323
}
299
324
}
@@ -303,7 +328,9 @@ impl Ty {
303
328
TypeParamLoweringMode :: Placeholder => {
304
329
Substs :: type_params_for_generics ( & generics)
305
330
}
306
- TypeParamLoweringMode :: Variable => Substs :: bound_vars ( & generics) ,
331
+ TypeParamLoweringMode :: Variable => {
332
+ Substs :: bound_vars ( & generics, ctx. in_binders )
333
+ }
307
334
} ;
308
335
ctx. db . impl_self_ty ( impl_id) . subst ( & substs)
309
336
}
@@ -313,7 +340,9 @@ impl Ty {
313
340
TypeParamLoweringMode :: Placeholder => {
314
341
Substs :: type_params_for_generics ( & generics)
315
342
}
316
- TypeParamLoweringMode :: Variable => Substs :: bound_vars ( & generics) ,
343
+ TypeParamLoweringMode :: Variable => {
344
+ Substs :: bound_vars ( & generics, ctx. in_binders )
345
+ }
317
346
} ;
318
347
ctx. db . ty ( adt. into ( ) ) . subst ( & substs)
319
348
}
@@ -797,7 +826,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
797
826
/// function body.
798
827
fn type_for_fn ( db : & dyn HirDatabase , def : FunctionId ) -> Binders < Ty > {
799
828
let generics = generics ( db. upcast ( ) , def. into ( ) ) ;
800
- let substs = Substs :: bound_vars ( & generics) ;
829
+ let substs = Substs :: bound_vars ( & generics, DebruijnIndex :: INNERMOST ) ;
801
830
Binders :: new ( substs. len ( ) , Ty :: apply ( TypeCtor :: FnDef ( def. into ( ) ) , substs) )
802
831
}
803
832
@@ -851,7 +880,7 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<T
851
880
return type_for_adt ( db, def. into ( ) ) ;
852
881
}
853
882
let generics = generics ( db. upcast ( ) , def. into ( ) ) ;
854
- let substs = Substs :: bound_vars ( & generics) ;
883
+ let substs = Substs :: bound_vars ( & generics, DebruijnIndex :: INNERMOST ) ;
855
884
Binders :: new ( substs. len ( ) , Ty :: apply ( TypeCtor :: FnDef ( def. into ( ) ) , substs) )
856
885
}
857
886
@@ -876,13 +905,13 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -
876
905
return type_for_adt ( db, def. parent . into ( ) ) ;
877
906
}
878
907
let generics = generics ( db. upcast ( ) , def. parent . into ( ) ) ;
879
- let substs = Substs :: bound_vars ( & generics) ;
908
+ let substs = Substs :: bound_vars ( & generics, DebruijnIndex :: INNERMOST ) ;
880
909
Binders :: new ( substs. len ( ) , Ty :: apply ( TypeCtor :: FnDef ( def. into ( ) ) , substs) )
881
910
}
882
911
883
912
fn type_for_adt ( db : & dyn HirDatabase , adt : AdtId ) -> Binders < Ty > {
884
913
let generics = generics ( db. upcast ( ) , adt. into ( ) ) ;
885
- let substs = Substs :: bound_vars ( & generics) ;
914
+ let substs = Substs :: bound_vars ( & generics, DebruijnIndex :: INNERMOST ) ;
886
915
Binders :: new ( substs. len ( ) , Ty :: apply ( TypeCtor :: Adt ( adt) , substs) )
887
916
}
888
917
@@ -892,7 +921,7 @@ fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> {
892
921
let ctx =
893
922
TyLoweringContext :: new ( db, & resolver) . with_type_param_mode ( TypeParamLoweringMode :: Variable ) ;
894
923
let type_ref = & db. type_alias_data ( t) . type_ref ;
895
- let substs = Substs :: bound_vars ( & generics) ;
924
+ let substs = Substs :: bound_vars ( & generics, DebruijnIndex :: INNERMOST ) ;
896
925
let inner = Ty :: from_hir ( & ctx, type_ref. as_ref ( ) . unwrap_or ( & TypeRef :: Error ) ) ;
897
926
Binders :: new ( substs. len ( ) , inner)
898
927
}
0 commit comments