diff options
Diffstat (limited to 'crates/hir_ty/src/lower.rs')
-rw-r--r-- | crates/hir_ty/src/lower.rs | 51 |
1 files changed, 24 insertions, 27 deletions
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 99b0ecf3b..44bd95a9a 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs | |||
@@ -31,9 +31,9 @@ use crate::{ | |||
31 | all_super_trait_refs, associated_type_by_name_including_super_traits, generics, | 31 | all_super_trait_refs, associated_type_by_name_including_super_traits, generics, |
32 | make_mut_slice, variant_data, | 32 | make_mut_slice, variant_data, |
33 | }, | 33 | }, |
34 | Binders, BoundVar, DebruijnIndex, FnSig, GenericPredicate, OpaqueTy, OpaqueTyId, PolyFnSig, | 34 | Binders, BoundVar, CallableSig, DebruijnIndex, FnPointer, FnSig, GenericPredicate, OpaqueTy, |
35 | ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait, ReturnTypeImplTraits, Substs, | 35 | OpaqueTyId, PolyFnSig, ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait, |
36 | TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk, | 36 | ReturnTypeImplTraits, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk, |
37 | }; | 37 | }; |
38 | 38 | ||
39 | #[derive(Debug)] | 39 | #[derive(Debug)] |
@@ -145,13 +145,10 @@ impl Ty { | |||
145 | pub fn from_hir_ext(ctx: &TyLoweringContext<'_>, type_ref: &TypeRef) -> (Self, Option<TypeNs>) { | 145 | pub fn from_hir_ext(ctx: &TyLoweringContext<'_>, type_ref: &TypeRef) -> (Self, Option<TypeNs>) { |
146 | let mut res = None; | 146 | let mut res = None; |
147 | let ty = match type_ref { | 147 | let ty = match type_ref { |
148 | TypeRef::Never => Ty::simple(TypeCtor::Never), | 148 | TypeRef::Never => Ty::Never, |
149 | TypeRef::Tuple(inner) => { | 149 | TypeRef::Tuple(inner) => { |
150 | let inner_tys: Arc<[Ty]> = inner.iter().map(|tr| Ty::from_hir(ctx, tr)).collect(); | 150 | let inner_tys: Arc<[Ty]> = inner.iter().map(|tr| Ty::from_hir(ctx, tr)).collect(); |
151 | Ty::apply( | 151 | Ty::Tuple(inner_tys.len(), Substs(inner_tys)) |
152 | TypeCtor::Tuple { cardinality: inner_tys.len() as u16 }, | ||
153 | Substs(inner_tys), | ||
154 | ) | ||
155 | } | 152 | } |
156 | TypeRef::Path(path) => { | 153 | TypeRef::Path(path) => { |
157 | let (ty, res_) = Ty::from_hir_path(ctx, path); | 154 | let (ty, res_) = Ty::from_hir_path(ctx, path); |
@@ -160,27 +157,28 @@ impl Ty { | |||
160 | } | 157 | } |
161 | TypeRef::RawPtr(inner, mutability) => { | 158 | TypeRef::RawPtr(inner, mutability) => { |
162 | let inner_ty = Ty::from_hir(ctx, inner); | 159 | let inner_ty = Ty::from_hir(ctx, inner); |
163 | Ty::apply_one(TypeCtor::RawPtr(*mutability), inner_ty) | 160 | Ty::RawPtr(*mutability, Substs::single(inner_ty)) |
164 | } | 161 | } |
165 | TypeRef::Array(inner) => { | 162 | TypeRef::Array(inner) => { |
166 | let inner_ty = Ty::from_hir(ctx, inner); | 163 | let inner_ty = Ty::from_hir(ctx, inner); |
167 | Ty::apply_one(TypeCtor::Array, inner_ty) | 164 | Ty::Array(Substs::single(inner_ty)) |
168 | } | 165 | } |
169 | TypeRef::Slice(inner) => { | 166 | TypeRef::Slice(inner) => { |
170 | let inner_ty = Ty::from_hir(ctx, inner); | 167 | let inner_ty = Ty::from_hir(ctx, inner); |
171 | Ty::apply_one(TypeCtor::Slice, inner_ty) | 168 | Ty::Slice(Substs::single(inner_ty)) |
172 | } | 169 | } |
173 | TypeRef::Reference(inner, _, mutability) => { | 170 | TypeRef::Reference(inner, _, mutability) => { |
174 | let inner_ty = Ty::from_hir(ctx, inner); | 171 | let inner_ty = Ty::from_hir(ctx, inner); |
175 | Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty) | 172 | Ty::Ref(*mutability, Substs::single(inner_ty)) |
176 | } | 173 | } |
177 | TypeRef::Placeholder => Ty::Unknown, | 174 | TypeRef::Placeholder => Ty::Unknown, |
178 | TypeRef::Fn(params, is_varargs) => { | 175 | TypeRef::Fn(params, is_varargs) => { |
179 | let sig = Substs(params.iter().map(|tr| Ty::from_hir(ctx, tr)).collect()); | 176 | let substs = Substs(params.iter().map(|tr| Ty::from_hir(ctx, tr)).collect()); |
180 | Ty::apply( | 177 | Ty::Function(FnPointer { |
181 | TypeCtor::FnPtr { num_args: sig.len() as u16 - 1, is_varargs: *is_varargs }, | 178 | num_args: substs.len() - 1, |
182 | sig, | 179 | sig: FnSig { variadic: *is_varargs }, |
183 | ) | 180 | substs, |
181 | }) | ||
184 | } | 182 | } |
185 | TypeRef::DynTrait(bounds) => { | 183 | TypeRef::DynTrait(bounds) => { |
186 | let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); | 184 | let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); |
@@ -414,7 +412,6 @@ impl Ty { | |||
414 | // FIXME: report error | 412 | // FIXME: report error |
415 | TypeNs::EnumVariantId(_) => return (Ty::Unknown, None), | 413 | TypeNs::EnumVariantId(_) => return (Ty::Unknown, None), |
416 | }; | 414 | }; |
417 | |||
418 | Ty::from_type_relative_path(ctx, ty, Some(resolution), remaining_segments) | 415 | Ty::from_type_relative_path(ctx, ty, Some(resolution), remaining_segments) |
419 | } | 416 | } |
420 | 417 | ||
@@ -1017,7 +1014,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig { | |||
1017 | let ret = Ty::from_hir(&ctx_ret, &data.ret_type); | 1014 | let ret = Ty::from_hir(&ctx_ret, &data.ret_type); |
1018 | let generics = generics(db.upcast(), def.into()); | 1015 | let generics = generics(db.upcast(), def.into()); |
1019 | let num_binders = generics.len(); | 1016 | let num_binders = generics.len(); |
1020 | Binders::new(num_binders, FnSig::from_params_and_return(params, ret, data.is_varargs)) | 1017 | Binders::new(num_binders, CallableSig::from_params_and_return(params, ret, data.is_varargs)) |
1021 | } | 1018 | } |
1022 | 1019 | ||
1023 | /// Build the declared type of a function. This should not need to look at the | 1020 | /// Build the declared type of a function. This should not need to look at the |
@@ -1025,7 +1022,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig { | |||
1025 | fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { | 1022 | fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { |
1026 | let generics = generics(db.upcast(), def.into()); | 1023 | let generics = generics(db.upcast(), def.into()); |
1027 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1024 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); |
1028 | Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) | 1025 | Binders::new(substs.len(), Ty::FnDef(def.into(), substs)) |
1029 | } | 1026 | } |
1030 | 1027 | ||
1031 | /// Build the declared type of a const. | 1028 | /// Build the declared type of a const. |
@@ -1057,7 +1054,7 @@ fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnS | |||
1057 | let params = | 1054 | let params = |
1058 | fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::<Vec<_>>(); | 1055 | fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::<Vec<_>>(); |
1059 | let ret = type_for_adt(db, def.into()); | 1056 | let ret = type_for_adt(db, def.into()); |
1060 | Binders::new(ret.num_binders, FnSig::from_params_and_return(params, ret.value, false)) | 1057 | Binders::new(ret.num_binders, CallableSig::from_params_and_return(params, ret.value, false)) |
1061 | } | 1058 | } |
1062 | 1059 | ||
1063 | /// Build the type of a tuple struct constructor. | 1060 | /// Build the type of a tuple struct constructor. |
@@ -1068,7 +1065,7 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<T | |||
1068 | } | 1065 | } |
1069 | let generics = generics(db.upcast(), def.into()); | 1066 | let generics = generics(db.upcast(), def.into()); |
1070 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1067 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); |
1071 | Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) | 1068 | Binders::new(substs.len(), Ty::FnDef(def.into(), substs)) |
1072 | } | 1069 | } |
1073 | 1070 | ||
1074 | fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig { | 1071 | fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig { |
@@ -1081,7 +1078,7 @@ fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) | |||
1081 | let params = | 1078 | let params = |
1082 | fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::<Vec<_>>(); | 1079 | fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::<Vec<_>>(); |
1083 | let ret = type_for_adt(db, def.parent.into()); | 1080 | let ret = type_for_adt(db, def.parent.into()); |
1084 | Binders::new(ret.num_binders, FnSig::from_params_and_return(params, ret.value, false)) | 1081 | Binders::new(ret.num_binders, CallableSig::from_params_and_return(params, ret.value, false)) |
1085 | } | 1082 | } |
1086 | 1083 | ||
1087 | /// Build the type of a tuple enum variant constructor. | 1084 | /// Build the type of a tuple enum variant constructor. |
@@ -1093,13 +1090,13 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) - | |||
1093 | } | 1090 | } |
1094 | let generics = generics(db.upcast(), def.parent.into()); | 1091 | let generics = generics(db.upcast(), def.parent.into()); |
1095 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1092 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); |
1096 | Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) | 1093 | Binders::new(substs.len(), Ty::FnDef(def.into(), substs)) |
1097 | } | 1094 | } |
1098 | 1095 | ||
1099 | fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> { | 1096 | fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> { |
1100 | let generics = generics(db.upcast(), adt.into()); | 1097 | let generics = generics(db.upcast(), adt.into()); |
1101 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1098 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); |
1102 | Binders::new(substs.len(), Ty::apply(TypeCtor::Adt(adt), substs)) | 1099 | Binders::new(substs.len(), Ty::Adt(adt, substs)) |
1103 | } | 1100 | } |
1104 | 1101 | ||
1105 | fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { | 1102 | fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { |
@@ -1107,10 +1104,10 @@ fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { | |||
1107 | let resolver = t.resolver(db.upcast()); | 1104 | let resolver = t.resolver(db.upcast()); |
1108 | let ctx = | 1105 | let ctx = |
1109 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); | 1106 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); |
1110 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | ||
1111 | if db.type_alias_data(t).is_extern { | 1107 | if db.type_alias_data(t).is_extern { |
1112 | Binders::new(substs.len(), Ty::apply(TypeCtor::ForeignType(t), substs)) | 1108 | Binders::new(0, Ty::ForeignType(t)) |
1113 | } else { | 1109 | } else { |
1110 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | ||
1114 | let type_ref = &db.type_alias_data(t).type_ref; | 1111 | let type_ref = &db.type_alias_data(t).type_ref; |
1115 | let inner = Ty::from_hir(&ctx, type_ref.as_ref().unwrap_or(&TypeRef::Error)); | 1112 | let inner = Ty::from_hir(&ctx, type_ref.as_ref().unwrap_or(&TypeRef::Error)); |
1116 | Binders::new(substs.len(), inner) | 1113 | Binders::new(substs.len(), inner) |