aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/lower.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/lower.rs')
-rw-r--r--crates/hir_ty/src/lower.rs82
1 files changed, 41 insertions, 41 deletions
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs
index 19465b2ed..59d2a157f 100644
--- a/crates/hir_ty/src/lower.rs
+++ b/crates/hir_ty/src/lower.rs
@@ -31,7 +31,7 @@ use crate::{
31 traits::chalk::{Interner, ToChalk}, 31 traits::chalk::{Interner, ToChalk},
32 utils::{ 32 utils::{
33 all_super_trait_refs, associated_type_by_name_including_super_traits, generics, 33 all_super_trait_refs, associated_type_by_name_including_super_traits, generics,
34 variant_data, 34 variant_data, Generics,
35 }, 35 },
36 AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig, 36 AliasEq, AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, DynTy, FnPointer, FnSig,
37 ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses, 37 ImplTraitId, OpaqueTy, PolyFnSig, ProjectionTy, QuantifiedWhereClause, QuantifiedWhereClauses,
@@ -196,7 +196,7 @@ impl<'a> TyLoweringContext<'a> {
196 bounds.iter().flat_map(|b| ctx.lower_type_bound(b, self_ty.clone(), false)), 196 bounds.iter().flat_map(|b| ctx.lower_type_bound(b, self_ty.clone(), false)),
197 ) 197 )
198 }); 198 });
199 let bounds = Binders::new(1, bounds); 199 let bounds = crate::make_only_type_binders(1, bounds);
200 TyKind::Dyn(DynTy { bounds }).intern(&Interner) 200 TyKind::Dyn(DynTy { bounds }).intern(&Interner)
201 } 201 }
202 TypeRef::ImplTrait(bounds) => { 202 TypeRef::ImplTrait(bounds) => {
@@ -209,9 +209,9 @@ impl<'a> TyLoweringContext<'a> {
209 // this dance is to make sure the data is in the right 209 // this dance is to make sure the data is in the right
210 // place even if we encounter more opaque types while 210 // place even if we encounter more opaque types while
211 // lowering the bounds 211 // lowering the bounds
212 self.opaque_type_data 212 self.opaque_type_data.borrow_mut().push(ReturnTypeImplTrait {
213 .borrow_mut() 213 bounds: crate::make_only_type_binders(1, Vec::new()),
214 .push(ReturnTypeImplTrait { bounds: Binders::new(1, Vec::new()) }); 214 });
215 // We don't want to lower the bounds inside the binders 215 // We don't want to lower the bounds inside the binders
216 // we're currently in, because they don't end up inside 216 // we're currently in, because they don't end up inside
217 // those binders. E.g. when we have `impl Trait<impl 217 // those binders. E.g. when we have `impl Trait<impl
@@ -380,7 +380,7 @@ impl<'a> TyLoweringContext<'a> {
380 TyKind::Error.intern(&Interner) 380 TyKind::Error.intern(&Interner)
381 } else { 381 } else {
382 let dyn_ty = DynTy { 382 let dyn_ty = DynTy {
383 bounds: Binders::new( 383 bounds: crate::make_only_type_binders(
384 1, 384 1,
385 QuantifiedWhereClauses::from_iter( 385 QuantifiedWhereClauses::from_iter(
386 &Interner, 386 &Interner,
@@ -787,7 +787,7 @@ impl<'a> TyLoweringContext<'a> {
787 let predicates = self.with_shifted_in(DebruijnIndex::ONE, |ctx| { 787 let predicates = self.with_shifted_in(DebruijnIndex::ONE, |ctx| {
788 bounds.iter().flat_map(|b| ctx.lower_type_bound(b, self_ty.clone(), false)).collect() 788 bounds.iter().flat_map(|b| ctx.lower_type_bound(b, self_ty.clone(), false)).collect()
789 }); 789 });
790 ReturnTypeImplTrait { bounds: Binders::new(1, predicates) } 790 ReturnTypeImplTrait { bounds: crate::make_only_type_binders(1, predicates) }
791 } 791 }
792} 792}
793 793
@@ -884,7 +884,7 @@ pub(crate) fn field_types_query(
884 let ctx = 884 let ctx =
885 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 885 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
886 for (field_id, field_data) in var_data.fields().iter() { 886 for (field_id, field_data) in var_data.fields().iter() {
887 res.insert(field_id, Binders::new(generics.len(), ctx.lower_ty(&field_data.type_ref))) 887 res.insert(field_id, make_binders(&generics, ctx.lower_ty(&field_data.type_ref)))
888 } 888 }
889 Arc::new(res) 889 Arc::new(res)
890} 890}
@@ -918,9 +918,7 @@ pub(crate) fn generic_predicates_for_param_query(
918 }, 918 },
919 WherePredicate::Lifetime { .. } => false, 919 WherePredicate::Lifetime { .. } => false,
920 }) 920 })
921 .flat_map(|pred| { 921 .flat_map(|pred| ctx.lower_where_predicate(pred, true).map(|p| make_binders(&generics, p)))
922 ctx.lower_where_predicate(pred, true).map(|p| Binders::new(generics.len(), p))
923 })
924 .collect() 922 .collect()
925} 923}
926 924
@@ -991,9 +989,7 @@ pub(crate) fn generic_predicates_query(
991 let generics = generics(db.upcast(), def); 989 let generics = generics(db.upcast(), def);
992 resolver 990 resolver
993 .where_predicates_in_scope() 991 .where_predicates_in_scope()
994 .flat_map(|pred| { 992 .flat_map(|pred| ctx.lower_where_predicate(pred, false).map(|p| make_binders(&generics, p)))
995 ctx.lower_where_predicate(pred, false).map(|p| Binders::new(generics.len(), p))
996 })
997 .collect() 993 .collect()
998} 994}
999 995
@@ -1030,7 +1026,7 @@ pub(crate) fn generic_defaults_query(
1030 DebruijnIndex::INNERMOST, 1026 DebruijnIndex::INNERMOST,
1031 ); 1027 );
1032 1028
1033 Binders::new(idx, ty) 1029 crate::make_only_type_binders(idx, ty)
1034 }) 1030 })
1035 .collect(); 1031 .collect();
1036 1032
@@ -1043,14 +1039,13 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
1043 let ctx_params = TyLoweringContext::new(db, &resolver) 1039 let ctx_params = TyLoweringContext::new(db, &resolver)
1044 .with_impl_trait_mode(ImplTraitLoweringMode::Variable) 1040 .with_impl_trait_mode(ImplTraitLoweringMode::Variable)
1045 .with_type_param_mode(TypeParamLoweringMode::Variable); 1041 .with_type_param_mode(TypeParamLoweringMode::Variable);
1046 let params = data.params.iter().map(|tr| (&ctx_params).lower_ty(tr)).collect::<Vec<_>>(); 1042 let params = data.params.iter().map(|tr| ctx_params.lower_ty(tr)).collect::<Vec<_>>();
1047 let ctx_ret = TyLoweringContext::new(db, &resolver) 1043 let ctx_ret = TyLoweringContext::new(db, &resolver)
1048 .with_impl_trait_mode(ImplTraitLoweringMode::Opaque) 1044 .with_impl_trait_mode(ImplTraitLoweringMode::Opaque)
1049 .with_type_param_mode(TypeParamLoweringMode::Variable); 1045 .with_type_param_mode(TypeParamLoweringMode::Variable);
1050 let ret = (&ctx_ret).lower_ty(&data.ret_type); 1046 let ret = ctx_ret.lower_ty(&data.ret_type);
1051 let generics = generics(db.upcast(), def.into()); 1047 let generics = generics(db.upcast(), def.into());
1052 let num_binders = generics.len(); 1048 make_binders(&generics, CallableSig::from_params_and_return(params, ret, data.is_varargs()))
1053 Binders::new(num_binders, CallableSig::from_params_and_return(params, ret, data.is_varargs()))
1054} 1049}
1055 1050
1056/// Build the declared type of a function. This should not need to look at the 1051/// Build the declared type of a function. This should not need to look at the
@@ -1058,8 +1053,8 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig {
1058fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { 1053fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> {
1059 let generics = generics(db.upcast(), def.into()); 1054 let generics = generics(db.upcast(), def.into());
1060 let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST); 1055 let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST);
1061 Binders::new( 1056 make_binders(
1062 substs.len(&Interner), 1057 &generics,
1063 TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner), 1058 TyKind::FnDef(CallableDefId::FunctionId(def).to_chalk(db), substs).intern(&Interner),
1064 ) 1059 )
1065} 1060}
@@ -1072,7 +1067,7 @@ fn type_for_const(db: &dyn HirDatabase, def: ConstId) -> Binders<Ty> {
1072 let ctx = 1067 let ctx =
1073 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 1068 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
1074 1069
1075 Binders::new(generics.len(), ctx.lower_ty(&data.type_ref)) 1070 make_binders(&generics, ctx.lower_ty(&data.type_ref))
1076} 1071}
1077 1072
1078/// Build the declared type of a static. 1073/// Build the declared type of a static.
@@ -1081,7 +1076,7 @@ fn type_for_static(db: &dyn HirDatabase, def: StaticId) -> Binders<Ty> {
1081 let resolver = def.resolver(db.upcast()); 1076 let resolver = def.resolver(db.upcast());
1082 let ctx = TyLoweringContext::new(db, &resolver); 1077 let ctx = TyLoweringContext::new(db, &resolver);
1083 1078
1084 Binders::new(0, ctx.lower_ty(&data.type_ref)) 1079 Binders::empty(&Interner, ctx.lower_ty(&data.type_ref))
1085} 1080}
1086 1081
1087fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnSig { 1082fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnSig {
@@ -1103,8 +1098,8 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<T
1103 } 1098 }
1104 let generics = generics(db.upcast(), def.into()); 1099 let generics = generics(db.upcast(), def.into());
1105 let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST); 1100 let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST);
1106 Binders::new( 1101 make_binders(
1107 substs.len(&Interner), 1102 &generics,
1108 TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(&Interner), 1103 TyKind::FnDef(CallableDefId::StructId(def).to_chalk(db), substs).intern(&Interner),
1109 ) 1104 )
1110} 1105}
@@ -1130,17 +1125,17 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -
1130 } 1125 }
1131 let generics = generics(db.upcast(), def.parent.into()); 1126 let generics = generics(db.upcast(), def.parent.into());
1132 let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST); 1127 let substs = generics.bound_vars_subst(DebruijnIndex::INNERMOST);
1133 Binders::new( 1128 make_binders(
1134 substs.len(&Interner), 1129 &generics,
1135 TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs).intern(&Interner), 1130 TyKind::FnDef(CallableDefId::EnumVariantId(def).to_chalk(db), substs).intern(&Interner),
1136 ) 1131 )
1137} 1132}
1138 1133
1139fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> { 1134fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> {
1135 let generics = generics(db.upcast(), adt.into());
1140 let b = TyBuilder::adt(db, adt); 1136 let b = TyBuilder::adt(db, adt);
1141 let num_binders = b.remaining();
1142 let ty = b.fill_with_bound_vars(DebruijnIndex::INNERMOST, 0).build(); 1137 let ty = b.fill_with_bound_vars(DebruijnIndex::INNERMOST, 0).build();
1143 Binders::new(num_binders, ty) 1138 make_binders(&generics, ty)
1144} 1139}
1145 1140
1146fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { 1141fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> {
@@ -1149,11 +1144,11 @@ fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> {
1149 let ctx = 1144 let ctx =
1150 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 1145 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
1151 if db.type_alias_data(t).is_extern { 1146 if db.type_alias_data(t).is_extern {
1152 Binders::new(0, TyKind::Foreign(crate::to_foreign_def_id(t)).intern(&Interner)) 1147 Binders::empty(&Interner, TyKind::Foreign(crate::to_foreign_def_id(t)).intern(&Interner))
1153 } else { 1148 } else {
1154 let type_ref = &db.type_alias_data(t).type_ref; 1149 let type_ref = &db.type_alias_data(t).type_ref;
1155 let inner = ctx.lower_ty(type_ref.as_deref().unwrap_or(&TypeRef::Error)); 1150 let inner = ctx.lower_ty(type_ref.as_deref().unwrap_or(&TypeRef::Error));
1156 Binders::new(generics.len(), inner) 1151 make_binders(&generics, inner)
1157 } 1152 }
1158} 1153}
1159 1154
@@ -1212,19 +1207,21 @@ impl_from!(FunctionId, StructId, UnionId, EnumVariantId, ConstId, StaticId for V
1212/// namespace. 1207/// namespace.
1213pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> { 1208pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> {
1214 match def { 1209 match def {
1215 TyDefId::BuiltinType(it) => Binders::new(0, TyBuilder::builtin(it)), 1210 TyDefId::BuiltinType(it) => Binders::empty(&Interner, TyBuilder::builtin(it)),
1216 TyDefId::AdtId(it) => type_for_adt(db, it), 1211 TyDefId::AdtId(it) => type_for_adt(db, it),
1217 TyDefId::TypeAliasId(it) => type_for_type_alias(db, it), 1212 TyDefId::TypeAliasId(it) => type_for_type_alias(db, it),
1218 } 1213 }
1219} 1214}
1220 1215
1221pub(crate) fn ty_recover(db: &dyn HirDatabase, _cycle: &[String], def: &TyDefId) -> Binders<Ty> { 1216pub(crate) fn ty_recover(db: &dyn HirDatabase, _cycle: &[String], def: &TyDefId) -> Binders<Ty> {
1222 let num_binders = match *def { 1217 let generics = match *def {
1223 TyDefId::BuiltinType(_) => 0, 1218 TyDefId::BuiltinType(_) => {
1224 TyDefId::AdtId(it) => generics(db.upcast(), it.into()).len(), 1219 return Binders::empty(&Interner, TyKind::Error.intern(&Interner))
1225 TyDefId::TypeAliasId(it) => generics(db.upcast(), it.into()).len(), 1220 }
1221 TyDefId::AdtId(it) => generics(db.upcast(), it.into()),
1222 TyDefId::TypeAliasId(it) => generics(db.upcast(), it.into()),
1226 }; 1223 };
1227 Binders::new(num_binders, TyKind::Error.intern(&Interner)) 1224 make_binders(&generics, TyKind::Error.intern(&Interner))
1228} 1225}
1229 1226
1230pub(crate) fn value_ty_query(db: &dyn HirDatabase, def: ValueTyDefId) -> Binders<Ty> { 1227pub(crate) fn value_ty_query(db: &dyn HirDatabase, def: ValueTyDefId) -> Binders<Ty> {
@@ -1244,7 +1241,7 @@ pub(crate) fn impl_self_ty_query(db: &dyn HirDatabase, impl_id: ImplId) -> Binde
1244 let generics = generics(db.upcast(), impl_id.into()); 1241 let generics = generics(db.upcast(), impl_id.into());
1245 let ctx = 1242 let ctx =
1246 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); 1243 TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable);
1247 Binders::new(generics.len(), ctx.lower_ty(&impl_data.self_ty)) 1244 make_binders(&generics, ctx.lower_ty(&impl_data.self_ty))
1248} 1245}
1249 1246
1250pub(crate) fn const_param_ty_query(db: &dyn HirDatabase, def: ConstParamId) -> Ty { 1247pub(crate) fn const_param_ty_query(db: &dyn HirDatabase, def: ConstParamId) -> Ty {
@@ -1262,7 +1259,7 @@ pub(crate) fn impl_self_ty_recover(
1262 impl_id: &ImplId, 1259 impl_id: &ImplId,
1263) -> Binders<Ty> { 1260) -> Binders<Ty> {
1264 let generics = generics(db.upcast(), (*impl_id).into()); 1261 let generics = generics(db.upcast(), (*impl_id).into());
1265 Binders::new(generics.len(), TyKind::Error.intern(&Interner)) 1262 make_binders(&generics, TyKind::Error.intern(&Interner))
1266} 1263}
1267 1264
1268pub(crate) fn impl_trait_query(db: &dyn HirDatabase, impl_id: ImplId) -> Option<Binders<TraitRef>> { 1265pub(crate) fn impl_trait_query(db: &dyn HirDatabase, impl_id: ImplId) -> Option<Binders<TraitRef>> {
@@ -1287,13 +1284,12 @@ pub(crate) fn return_type_impl_traits(
1287 .with_type_param_mode(TypeParamLoweringMode::Variable); 1284 .with_type_param_mode(TypeParamLoweringMode::Variable);
1288 let _ret = (&ctx_ret).lower_ty(&data.ret_type); 1285 let _ret = (&ctx_ret).lower_ty(&data.ret_type);
1289 let generics = generics(db.upcast(), def.into()); 1286 let generics = generics(db.upcast(), def.into());
1290 let num_binders = generics.len();
1291 let return_type_impl_traits = 1287 let return_type_impl_traits =
1292 ReturnTypeImplTraits { impl_traits: ctx_ret.opaque_type_data.into_inner() }; 1288 ReturnTypeImplTraits { impl_traits: ctx_ret.opaque_type_data.into_inner() };
1293 if return_type_impl_traits.impl_traits.is_empty() { 1289 if return_type_impl_traits.impl_traits.is_empty() {
1294 None 1290 None
1295 } else { 1291 } else {
1296 Some(Arc::new(Binders::new(num_binders, return_type_impl_traits))) 1292 Some(Arc::new(make_binders(&generics, return_type_impl_traits)))
1297 } 1293 }
1298} 1294}
1299 1295
@@ -1303,3 +1299,7 @@ pub(crate) fn lower_to_chalk_mutability(m: hir_def::type_ref::Mutability) -> Mut
1303 hir_def::type_ref::Mutability::Mut => Mutability::Mut, 1299 hir_def::type_ref::Mutability::Mut => Mutability::Mut,
1304 } 1300 }
1305} 1301}
1302
1303fn make_binders<T>(generics: &Generics, value: T) -> Binders<T> {
1304 crate::make_only_type_binders(generics.len(), value)
1305}