aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/lower.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src/lower.rs')
-rw-r--r--crates/ra_hir_ty/src/lower.rs69
1 files changed, 49 insertions, 20 deletions
diff --git a/crates/ra_hir_ty/src/lower.rs b/crates/ra_hir_ty/src/lower.rs
index cc1ac8e3e..c2812e178 100644
--- a/crates/ra_hir_ty/src/lower.rs
+++ b/crates/ra_hir_ty/src/lower.rs
@@ -39,6 +39,7 @@ use crate::{
39pub struct TyLoweringContext<'a> { 39pub struct TyLoweringContext<'a> {
40 pub db: &'a dyn HirDatabase, 40 pub db: &'a dyn HirDatabase,
41 pub resolver: &'a Resolver, 41 pub resolver: &'a Resolver,
42 in_binders: DebruijnIndex,
42 /// Note: Conceptually, it's thinkable that we could be in a location where 43 /// Note: Conceptually, it's thinkable that we could be in a location where
43 /// some type params should be represented as placeholders, and others 44 /// some type params should be represented as placeholders, and others
44 /// should be converted to variables. I think in practice, this isn't 45 /// should be converted to variables. I think in practice, this isn't
@@ -53,7 +54,27 @@ impl<'a> TyLoweringContext<'a> {
53 let impl_trait_counter = std::cell::Cell::new(0); 54 let impl_trait_counter = std::cell::Cell::new(0);
54 let impl_trait_mode = ImplTraitLoweringMode::Disallowed; 55 let impl_trait_mode = ImplTraitLoweringMode::Disallowed;
55 let type_param_mode = TypeParamLoweringMode::Placeholder; 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 pub fn with_impl_trait_mode(self, impl_trait_mode: ImplTraitLoweringMode) -> Self { 80 pub fn with_impl_trait_mode(self, impl_trait_mode: ImplTraitLoweringMode) -> Self {
@@ -134,22 +155,26 @@ impl Ty {
134 } 155 }
135 TypeRef::DynTrait(bounds) => { 156 TypeRef::DynTrait(bounds) => {
136 let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); 157 let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0));
137 let predicates = bounds 158 let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| {
138 .iter() 159 bounds
139 .flat_map(|b| GenericPredicate::from_type_bound(ctx, b, self_ty.clone())) 160 .iter()
140 .collect(); 161 .flat_map(|b| GenericPredicate::from_type_bound(ctx, b, self_ty.clone()))
162 .collect()
163 });
141 Ty::Dyn(predicates) 164 Ty::Dyn(predicates)
142 } 165 }
143 TypeRef::ImplTrait(bounds) => { 166 TypeRef::ImplTrait(bounds) => {
144 match ctx.impl_trait_mode { 167 match ctx.impl_trait_mode {
145 ImplTraitLoweringMode::Opaque => { 168 ImplTraitLoweringMode::Opaque => {
146 let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); 169 let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0));
147 let predicates = bounds 170 let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| {
148 .iter() 171 bounds
149 .flat_map(|b| { 172 .iter()
150 GenericPredicate::from_type_bound(ctx, b, self_ty.clone()) 173 .flat_map(|b| {
151 }) 174 GenericPredicate::from_type_bound(ctx, b, self_ty.clone())
152 .collect(); 175 })
176 .collect()
177 });
153 Ty::Opaque(predicates) 178 Ty::Opaque(predicates)
154 } 179 }
155 ImplTraitLoweringMode::Param => { 180 ImplTraitLoweringMode::Param => {
@@ -180,7 +205,7 @@ impl Ty {
180 (0, 0, 0, 0) 205 (0, 0, 0, 0)
181 }; 206 };
182 Ty::Bound(BoundVar::new( 207 Ty::Bound(BoundVar::new(
183 DebruijnIndex::INNERMOST, 208 ctx.in_binders,
184 idx as usize + parent_params + self_params + list_params, 209 idx as usize + parent_params + self_params + list_params,
185 )) 210 ))
186 } 211 }
@@ -293,7 +318,7 @@ impl Ty {
293 TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), 318 TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id),
294 TypeParamLoweringMode::Variable => { 319 TypeParamLoweringMode::Variable => {
295 let idx = generics.param_idx(param_id).expect("matching generics"); 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 TypeParamLoweringMode::Placeholder => { 328 TypeParamLoweringMode::Placeholder => {
304 Substs::type_params_for_generics(&generics) 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 ctx.db.impl_self_ty(impl_id).subst(&substs) 335 ctx.db.impl_self_ty(impl_id).subst(&substs)
309 } 336 }
@@ -313,7 +340,9 @@ impl Ty {
313 TypeParamLoweringMode::Placeholder => { 340 TypeParamLoweringMode::Placeholder => {
314 Substs::type_params_for_generics(&generics) 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 ctx.db.ty(adt.into()).subst(&substs) 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/// function body. 826/// function body.
798fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { 827fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> {
799 let generics = generics(db.upcast(), def.into()); 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 Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) 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 return type_for_adt(db, def.into()); 880 return type_for_adt(db, def.into());
852 } 881 }
853 let generics = generics(db.upcast(), def.into()); 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 Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) 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 return type_for_adt(db, def.parent.into()); 905 return type_for_adt(db, def.parent.into());
877 } 906 }
878 let generics = generics(db.upcast(), def.parent.into()); 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 Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) 909 Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs))
881} 910}
882 911
883fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> { 912fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
884 let generics = generics(db.upcast(), adt.into()); 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 Binders::new(substs.len(), Ty::apply(TypeCtor::Adt(adt), substs)) 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 let ctx = 921 let ctx =
893 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 922 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
894 let type_ref = &db.type_alias_data(t).type_ref; 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 let inner = Ty::from_hir(&ctx, type_ref.as_ref().unwrap_or(&TypeRef::Error)); 925 let inner = Ty::from_hir(&ctx, type_ref.as_ref().unwrap_or(&TypeRef::Error));
897 Binders::new(substs.len(), inner) 926 Binders::new(substs.len(), inner)
898} 927}