diff options
Diffstat (limited to 'crates/hir_ty/src/lower.rs')
-rw-r--r-- | crates/hir_ty/src/lower.rs | 138 |
1 files changed, 66 insertions, 72 deletions
diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index 7a734c8b9..1b5843d48 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs | |||
@@ -8,6 +8,7 @@ | |||
8 | use std::{iter, sync::Arc}; | 8 | use std::{iter, sync::Arc}; |
9 | 9 | ||
10 | use base_db::CrateId; | 10 | use base_db::CrateId; |
11 | use chalk_ir::Mutability; | ||
11 | use hir_def::{ | 12 | use hir_def::{ |
12 | adt::StructKind, | 13 | adt::StructKind, |
13 | builtin_type::BuiltinType, | 14 | builtin_type::BuiltinType, |
@@ -27,14 +28,13 @@ use test_utils::mark; | |||
27 | 28 | ||
28 | use crate::{ | 29 | use crate::{ |
29 | db::HirDatabase, | 30 | db::HirDatabase, |
30 | primitive::{FloatTy, IntTy}, | ||
31 | utils::{ | 31 | utils::{ |
32 | all_super_trait_refs, associated_type_by_name_including_super_traits, generics, | 32 | all_super_trait_refs, associated_type_by_name_including_super_traits, generics, |
33 | make_mut_slice, variant_data, | 33 | make_mut_slice, variant_data, |
34 | }, | 34 | }, |
35 | Binders, BoundVar, DebruijnIndex, FnSig, GenericPredicate, OpaqueTy, OpaqueTyId, PolyFnSig, | 35 | AliasTy, Binders, BoundVar, CallableSig, DebruijnIndex, FnPointer, FnSig, GenericPredicate, |
36 | ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait, ReturnTypeImplTraits, Substs, | 36 | OpaqueTy, OpaqueTyId, PolyFnSig, ProjectionPredicate, ProjectionTy, ReturnTypeImplTrait, |
37 | TraitEnvironment, TraitRef, Ty, TypeCtor, TypeWalk, | 37 | ReturnTypeImplTraits, Substs, TraitEnvironment, TraitRef, Ty, TypeWalk, |
38 | }; | 38 | }; |
39 | 39 | ||
40 | #[derive(Debug)] | 40 | #[derive(Debug)] |
@@ -146,13 +146,10 @@ impl Ty { | |||
146 | pub fn from_hir_ext(ctx: &TyLoweringContext<'_>, type_ref: &TypeRef) -> (Self, Option<TypeNs>) { | 146 | pub fn from_hir_ext(ctx: &TyLoweringContext<'_>, type_ref: &TypeRef) -> (Self, Option<TypeNs>) { |
147 | let mut res = None; | 147 | let mut res = None; |
148 | let ty = match type_ref { | 148 | let ty = match type_ref { |
149 | TypeRef::Never => Ty::simple(TypeCtor::Never), | 149 | TypeRef::Never => Ty::Never, |
150 | TypeRef::Tuple(inner) => { | 150 | TypeRef::Tuple(inner) => { |
151 | let inner_tys: Arc<[Ty]> = inner.iter().map(|tr| Ty::from_hir(ctx, tr)).collect(); | 151 | let inner_tys: Arc<[Ty]> = inner.iter().map(|tr| Ty::from_hir(ctx, tr)).collect(); |
152 | Ty::apply( | 152 | Ty::Tuple(inner_tys.len(), Substs(inner_tys)) |
153 | TypeCtor::Tuple { cardinality: inner_tys.len() as u16 }, | ||
154 | Substs(inner_tys), | ||
155 | ) | ||
156 | } | 153 | } |
157 | TypeRef::Path(path) => { | 154 | TypeRef::Path(path) => { |
158 | let (ty, res_) = Ty::from_hir_path(ctx, path); | 155 | let (ty, res_) = Ty::from_hir_path(ctx, path); |
@@ -161,30 +158,31 @@ impl Ty { | |||
161 | } | 158 | } |
162 | TypeRef::RawPtr(inner, mutability) => { | 159 | TypeRef::RawPtr(inner, mutability) => { |
163 | let inner_ty = Ty::from_hir(ctx, inner); | 160 | let inner_ty = Ty::from_hir(ctx, inner); |
164 | Ty::apply_one(TypeCtor::RawPtr(*mutability), inner_ty) | 161 | Ty::Raw(lower_to_chalk_mutability(*mutability), Substs::single(inner_ty)) |
165 | } | 162 | } |
166 | TypeRef::Array(inner) => { | 163 | TypeRef::Array(inner) => { |
167 | let inner_ty = Ty::from_hir(ctx, inner); | 164 | let inner_ty = Ty::from_hir(ctx, inner); |
168 | Ty::apply_one(TypeCtor::Array, inner_ty) | 165 | Ty::Array(Substs::single(inner_ty)) |
169 | } | 166 | } |
170 | TypeRef::Slice(inner) => { | 167 | TypeRef::Slice(inner) => { |
171 | let inner_ty = Ty::from_hir(ctx, inner); | 168 | let inner_ty = Ty::from_hir(ctx, inner); |
172 | Ty::apply_one(TypeCtor::Slice, inner_ty) | 169 | Ty::Slice(Substs::single(inner_ty)) |
173 | } | 170 | } |
174 | TypeRef::Reference(inner, _, mutability) => { | 171 | TypeRef::Reference(inner, _, mutability) => { |
175 | let inner_ty = Ty::from_hir(ctx, inner); | 172 | let inner_ty = Ty::from_hir(ctx, inner); |
176 | Ty::apply_one(TypeCtor::Ref(*mutability), inner_ty) | 173 | Ty::Ref(lower_to_chalk_mutability(*mutability), Substs::single(inner_ty)) |
177 | } | 174 | } |
178 | TypeRef::Placeholder => Ty::Unknown, | 175 | TypeRef::Placeholder => Ty::Unknown, |
179 | TypeRef::Fn(params, is_varargs) => { | 176 | TypeRef::Fn(params, is_varargs) => { |
180 | let sig = Substs(params.iter().map(|tr| Ty::from_hir(ctx, tr)).collect()); | 177 | let substs = Substs(params.iter().map(|tr| Ty::from_hir(ctx, tr)).collect()); |
181 | Ty::apply( | 178 | Ty::Function(FnPointer { |
182 | TypeCtor::FnPtr { num_args: sig.len() as u16 - 1, is_varargs: *is_varargs }, | 179 | num_args: substs.len() - 1, |
183 | sig, | 180 | sig: FnSig { variadic: *is_varargs }, |
184 | ) | 181 | substs, |
182 | }) | ||
185 | } | 183 | } |
186 | TypeRef::DynTrait(bounds) => { | 184 | TypeRef::DynTrait(bounds) => { |
187 | let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); | 185 | let self_ty = Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)); |
188 | let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| { | 186 | let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| { |
189 | bounds | 187 | bounds |
190 | .iter() | 188 | .iter() |
@@ -228,7 +226,10 @@ impl Ty { | |||
228 | let impl_trait_id = OpaqueTyId::ReturnTypeImplTrait(func, idx); | 226 | let impl_trait_id = OpaqueTyId::ReturnTypeImplTrait(func, idx); |
229 | let generics = generics(ctx.db.upcast(), func.into()); | 227 | let generics = generics(ctx.db.upcast(), func.into()); |
230 | let parameters = Substs::bound_vars(&generics, ctx.in_binders); | 228 | let parameters = Substs::bound_vars(&generics, ctx.in_binders); |
231 | Ty::Opaque(OpaqueTy { opaque_ty_id: impl_trait_id, parameters }) | 229 | Ty::Alias(AliasTy::Opaque(OpaqueTy { |
230 | opaque_ty_id: impl_trait_id, | ||
231 | parameters, | ||
232 | })) | ||
232 | } | 233 | } |
233 | ImplTraitLoweringMode::Param => { | 234 | ImplTraitLoweringMode::Param => { |
234 | let idx = ctx.impl_trait_counter.get(); | 235 | let idx = ctx.impl_trait_counter.get(); |
@@ -259,7 +260,7 @@ impl Ty { | |||
259 | } else { | 260 | } else { |
260 | (0, 0, 0, 0) | 261 | (0, 0, 0, 0) |
261 | }; | 262 | }; |
262 | Ty::Bound(BoundVar::new( | 263 | Ty::BoundVar(BoundVar::new( |
263 | ctx.in_binders, | 264 | ctx.in_binders, |
264 | idx as usize + parent_params + self_params + list_params, | 265 | idx as usize + parent_params + self_params + list_params, |
265 | )) | 266 | )) |
@@ -331,7 +332,7 @@ impl Ty { | |||
331 | TypeNs::TraitId(trait_) => { | 332 | TypeNs::TraitId(trait_) => { |
332 | // if this is a bare dyn Trait, we'll directly put the required ^0 for the self type in there | 333 | // if this is a bare dyn Trait, we'll directly put the required ^0 for the self type in there |
333 | let self_ty = if remaining_segments.len() == 0 { | 334 | let self_ty = if remaining_segments.len() == 0 { |
334 | Some(Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0))) | 335 | Some(Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0))) |
335 | } else { | 336 | } else { |
336 | None | 337 | None |
337 | }; | 338 | }; |
@@ -347,10 +348,10 @@ impl Ty { | |||
347 | match found { | 348 | match found { |
348 | Some((super_trait_ref, associated_ty)) => { | 349 | Some((super_trait_ref, associated_ty)) => { |
349 | // FIXME handle type parameters on the segment | 350 | // FIXME handle type parameters on the segment |
350 | Ty::Projection(ProjectionTy { | 351 | Ty::Alias(AliasTy::Projection(ProjectionTy { |
351 | associated_ty, | 352 | associated_ty, |
352 | parameters: super_trait_ref.substs, | 353 | parameters: super_trait_ref.substs, |
353 | }) | 354 | })) |
354 | } | 355 | } |
355 | None => { | 356 | None => { |
356 | // FIXME: report error (associated type not found) | 357 | // FIXME: report error (associated type not found) |
@@ -374,7 +375,7 @@ impl Ty { | |||
374 | TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), | 375 | TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), |
375 | TypeParamLoweringMode::Variable => { | 376 | TypeParamLoweringMode::Variable => { |
376 | let idx = generics.param_idx(param_id).expect("matching generics"); | 377 | let idx = generics.param_idx(param_id).expect("matching generics"); |
377 | Ty::Bound(BoundVar::new(ctx.in_binders, idx)) | 378 | Ty::BoundVar(BoundVar::new(ctx.in_binders, idx)) |
378 | } | 379 | } |
379 | } | 380 | } |
380 | } | 381 | } |
@@ -415,7 +416,6 @@ impl Ty { | |||
415 | // FIXME: report error | 416 | // FIXME: report error |
416 | TypeNs::EnumVariantId(_) => return (Ty::Unknown, None), | 417 | TypeNs::EnumVariantId(_) => return (Ty::Unknown, None), |
417 | }; | 418 | }; |
418 | |||
419 | Ty::from_type_relative_path(ctx, ty, Some(resolution), remaining_segments) | 419 | Ty::from_type_relative_path(ctx, ty, Some(resolution), remaining_segments) |
420 | } | 420 | } |
421 | 421 | ||
@@ -473,10 +473,10 @@ impl Ty { | |||
473 | // associated_type_shorthand_candidates does not do that | 473 | // associated_type_shorthand_candidates does not do that |
474 | let substs = substs.shift_bound_vars(ctx.in_binders); | 474 | let substs = substs.shift_bound_vars(ctx.in_binders); |
475 | // FIXME handle type parameters on the segment | 475 | // FIXME handle type parameters on the segment |
476 | return Some(Ty::Projection(ProjectionTy { | 476 | return Some(Ty::Alias(AliasTy::Projection(ProjectionTy { |
477 | associated_ty, | 477 | associated_ty, |
478 | parameters: substs, | 478 | parameters: substs, |
479 | })); | 479 | }))); |
480 | } | 480 | } |
481 | 481 | ||
482 | None | 482 | None |
@@ -656,17 +656,6 @@ impl TraitRef { | |||
656 | ) -> Substs { | 656 | ) -> Substs { |
657 | substs_from_path_segment(ctx, segment, Some(resolved.into()), false) | 657 | substs_from_path_segment(ctx, segment, Some(resolved.into()), false) |
658 | } | 658 | } |
659 | |||
660 | pub(crate) fn from_type_bound( | ||
661 | ctx: &TyLoweringContext<'_>, | ||
662 | bound: &TypeBound, | ||
663 | self_ty: Ty, | ||
664 | ) -> Option<TraitRef> { | ||
665 | match bound { | ||
666 | TypeBound::Path(path) => TraitRef::from_path(ctx, path, Some(self_ty)), | ||
667 | TypeBound::Lifetime(_) | TypeBound::Error => None, | ||
668 | } | ||
669 | } | ||
670 | } | 659 | } |
671 | 660 | ||
672 | impl GenericPredicate { | 661 | impl GenericPredicate { |
@@ -688,7 +677,7 @@ impl GenericPredicate { | |||
688 | TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), | 677 | TypeParamLoweringMode::Placeholder => Ty::Placeholder(param_id), |
689 | TypeParamLoweringMode::Variable => { | 678 | TypeParamLoweringMode::Variable => { |
690 | let idx = generics.param_idx(param_id).expect("matching generics"); | 679 | let idx = generics.param_idx(param_id).expect("matching generics"); |
691 | Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, idx)) | 680 | Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, idx)) |
692 | } | 681 | } |
693 | } | 682 | } |
694 | } | 683 | } |
@@ -706,13 +695,22 @@ impl GenericPredicate { | |||
706 | bound: &'a TypeBound, | 695 | bound: &'a TypeBound, |
707 | self_ty: Ty, | 696 | self_ty: Ty, |
708 | ) -> impl Iterator<Item = GenericPredicate> + 'a { | 697 | ) -> impl Iterator<Item = GenericPredicate> + 'a { |
709 | let trait_ref = TraitRef::from_type_bound(ctx, bound, self_ty); | 698 | let mut bindings = None; |
710 | iter::once(trait_ref.clone().map_or(GenericPredicate::Error, GenericPredicate::Implemented)) | 699 | let trait_ref = match bound { |
711 | .chain( | 700 | TypeBound::Path(path) => { |
712 | trait_ref | 701 | bindings = TraitRef::from_path(ctx, path, Some(self_ty)); |
713 | .into_iter() | 702 | Some( |
714 | .flat_map(move |tr| assoc_type_bindings_from_type_bound(ctx, bound, tr)), | 703 | bindings.clone().map_or(GenericPredicate::Error, GenericPredicate::Implemented), |
715 | ) | 704 | ) |
705 | } | ||
706 | TypeBound::Lifetime(_) => None, | ||
707 | TypeBound::Error => Some(GenericPredicate::Error), | ||
708 | }; | ||
709 | trait_ref.into_iter().chain( | ||
710 | bindings | ||
711 | .into_iter() | ||
712 | .flat_map(move |tr| assoc_type_bindings_from_type_bound(ctx, bound, tr)), | ||
713 | ) | ||
716 | } | 714 | } |
717 | } | 715 | } |
718 | 716 | ||
@@ -753,7 +751,7 @@ fn assoc_type_bindings_from_type_bound<'a>( | |||
753 | preds.extend(GenericPredicate::from_type_bound( | 751 | preds.extend(GenericPredicate::from_type_bound( |
754 | ctx, | 752 | ctx, |
755 | bound, | 753 | bound, |
756 | Ty::Projection(projection_ty.clone()), | 754 | Ty::Alias(AliasTy::Projection(projection_ty.clone())), |
757 | )); | 755 | )); |
758 | } | 756 | } |
759 | preds | 757 | preds |
@@ -763,7 +761,7 @@ fn assoc_type_bindings_from_type_bound<'a>( | |||
763 | impl ReturnTypeImplTrait { | 761 | impl ReturnTypeImplTrait { |
764 | fn from_hir(ctx: &TyLoweringContext, bounds: &[TypeBound]) -> Self { | 762 | fn from_hir(ctx: &TyLoweringContext, bounds: &[TypeBound]) -> Self { |
765 | mark::hit!(lower_rpit); | 763 | mark::hit!(lower_rpit); |
766 | let self_ty = Ty::Bound(BoundVar::new(DebruijnIndex::INNERMOST, 0)); | 764 | let self_ty = Ty::BoundVar(BoundVar::new(DebruijnIndex::INNERMOST, 0)); |
767 | let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| { | 765 | let predicates = ctx.with_shifted_in(DebruijnIndex::ONE, |ctx| { |
768 | bounds | 766 | bounds |
769 | .iter() | 767 | .iter() |
@@ -987,7 +985,7 @@ pub(crate) fn generic_defaults_query( | |||
987 | // Each default can only refer to previous parameters. | 985 | // Each default can only refer to previous parameters. |
988 | ty.walk_mut_binders( | 986 | ty.walk_mut_binders( |
989 | &mut |ty, binders| match ty { | 987 | &mut |ty, binders| match ty { |
990 | Ty::Bound(BoundVar { debruijn, index }) if *debruijn == binders => { | 988 | Ty::BoundVar(BoundVar { debruijn, index }) if *debruijn == binders => { |
991 | if *index >= idx { | 989 | if *index >= idx { |
992 | // type variable default referring to parameter coming | 990 | // type variable default referring to parameter coming |
993 | // after it. This is forbidden (FIXME: report | 991 | // after it. This is forbidden (FIXME: report |
@@ -1020,7 +1018,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig { | |||
1020 | let ret = Ty::from_hir(&ctx_ret, &data.ret_type); | 1018 | let ret = Ty::from_hir(&ctx_ret, &data.ret_type); |
1021 | let generics = generics(db.upcast(), def.into()); | 1019 | let generics = generics(db.upcast(), def.into()); |
1022 | let num_binders = generics.len(); | 1020 | let num_binders = generics.len(); |
1023 | Binders::new(num_binders, FnSig::from_params_and_return(params, ret, data.is_varargs)) | 1021 | Binders::new(num_binders, CallableSig::from_params_and_return(params, ret, data.is_varargs)) |
1024 | } | 1022 | } |
1025 | 1023 | ||
1026 | /// Build the declared type of a function. This should not need to look at the | 1024 | /// Build the declared type of a function. This should not need to look at the |
@@ -1028,7 +1026,7 @@ fn fn_sig_for_fn(db: &dyn HirDatabase, def: FunctionId) -> PolyFnSig { | |||
1028 | fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { | 1026 | fn type_for_fn(db: &dyn HirDatabase, def: FunctionId) -> Binders<Ty> { |
1029 | let generics = generics(db.upcast(), def.into()); | 1027 | let generics = generics(db.upcast(), def.into()); |
1030 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1028 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); |
1031 | Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) | 1029 | Binders::new(substs.len(), Ty::FnDef(def.into(), substs)) |
1032 | } | 1030 | } |
1033 | 1031 | ||
1034 | /// Build the declared type of a const. | 1032 | /// Build the declared type of a const. |
@@ -1051,17 +1049,6 @@ fn type_for_static(db: &dyn HirDatabase, def: StaticId) -> Binders<Ty> { | |||
1051 | Binders::new(0, Ty::from_hir(&ctx, &data.type_ref)) | 1049 | Binders::new(0, Ty::from_hir(&ctx, &data.type_ref)) |
1052 | } | 1050 | } |
1053 | 1051 | ||
1054 | /// Build the declared type of a static. | ||
1055 | fn type_for_builtin(def: BuiltinType) -> Ty { | ||
1056 | Ty::simple(match def { | ||
1057 | BuiltinType::Char => TypeCtor::Char, | ||
1058 | BuiltinType::Bool => TypeCtor::Bool, | ||
1059 | BuiltinType::Str => TypeCtor::Str, | ||
1060 | BuiltinType::Int(t) => TypeCtor::Int(IntTy::from(t).into()), | ||
1061 | BuiltinType::Float(t) => TypeCtor::Float(FloatTy::from(t).into()), | ||
1062 | }) | ||
1063 | } | ||
1064 | |||
1065 | fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnSig { | 1052 | fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnSig { |
1066 | let struct_data = db.struct_data(def); | 1053 | let struct_data = db.struct_data(def); |
1067 | let fields = struct_data.variant_data.fields(); | 1054 | let fields = struct_data.variant_data.fields(); |
@@ -1071,7 +1058,7 @@ fn fn_sig_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> PolyFnS | |||
1071 | let params = | 1058 | let params = |
1072 | fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::<Vec<_>>(); | 1059 | fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::<Vec<_>>(); |
1073 | let ret = type_for_adt(db, def.into()); | 1060 | let ret = type_for_adt(db, def.into()); |
1074 | Binders::new(ret.num_binders, FnSig::from_params_and_return(params, ret.value, false)) | 1061 | Binders::new(ret.num_binders, CallableSig::from_params_and_return(params, ret.value, false)) |
1075 | } | 1062 | } |
1076 | 1063 | ||
1077 | /// Build the type of a tuple struct constructor. | 1064 | /// Build the type of a tuple struct constructor. |
@@ -1082,7 +1069,7 @@ fn type_for_struct_constructor(db: &dyn HirDatabase, def: StructId) -> Binders<T | |||
1082 | } | 1069 | } |
1083 | let generics = generics(db.upcast(), def.into()); | 1070 | let generics = generics(db.upcast(), def.into()); |
1084 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1071 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); |
1085 | Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) | 1072 | Binders::new(substs.len(), Ty::FnDef(def.into(), substs)) |
1086 | } | 1073 | } |
1087 | 1074 | ||
1088 | fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig { | 1075 | fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) -> PolyFnSig { |
@@ -1095,7 +1082,7 @@ fn fn_sig_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) | |||
1095 | let params = | 1082 | let params = |
1096 | fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::<Vec<_>>(); | 1083 | fields.iter().map(|(_, field)| Ty::from_hir(&ctx, &field.type_ref)).collect::<Vec<_>>(); |
1097 | let ret = type_for_adt(db, def.parent.into()); | 1084 | let ret = type_for_adt(db, def.parent.into()); |
1098 | Binders::new(ret.num_binders, FnSig::from_params_and_return(params, ret.value, false)) | 1085 | Binders::new(ret.num_binders, CallableSig::from_params_and_return(params, ret.value, false)) |
1099 | } | 1086 | } |
1100 | 1087 | ||
1101 | /// Build the type of a tuple enum variant constructor. | 1088 | /// Build the type of a tuple enum variant constructor. |
@@ -1107,13 +1094,13 @@ fn type_for_enum_variant_constructor(db: &dyn HirDatabase, def: EnumVariantId) - | |||
1107 | } | 1094 | } |
1108 | let generics = generics(db.upcast(), def.parent.into()); | 1095 | let generics = generics(db.upcast(), def.parent.into()); |
1109 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1096 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); |
1110 | Binders::new(substs.len(), Ty::apply(TypeCtor::FnDef(def.into()), substs)) | 1097 | Binders::new(substs.len(), Ty::FnDef(def.into(), substs)) |
1111 | } | 1098 | } |
1112 | 1099 | ||
1113 | fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> { | 1100 | fn type_for_adt(db: &dyn HirDatabase, adt: AdtId) -> Binders<Ty> { |
1114 | let generics = generics(db.upcast(), adt.into()); | 1101 | let generics = generics(db.upcast(), adt.into()); |
1115 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | 1102 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); |
1116 | Binders::new(substs.len(), Ty::apply(TypeCtor::Adt(adt), substs)) | 1103 | Binders::new(substs.len(), Ty::Adt(adt, substs)) |
1117 | } | 1104 | } |
1118 | 1105 | ||
1119 | fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { | 1106 | fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { |
@@ -1121,10 +1108,10 @@ fn type_for_type_alias(db: &dyn HirDatabase, t: TypeAliasId) -> Binders<Ty> { | |||
1121 | let resolver = t.resolver(db.upcast()); | 1108 | let resolver = t.resolver(db.upcast()); |
1122 | let ctx = | 1109 | let ctx = |
1123 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); | 1110 | TyLoweringContext::new(db, &resolver).with_type_param_mode(TypeParamLoweringMode::Variable); |
1124 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | ||
1125 | if db.type_alias_data(t).is_extern { | 1111 | if db.type_alias_data(t).is_extern { |
1126 | Binders::new(substs.len(), Ty::apply(TypeCtor::ForeignType(t), substs)) | 1112 | Binders::new(0, Ty::ForeignType(t)) |
1127 | } else { | 1113 | } else { |
1114 | let substs = Substs::bound_vars(&generics, DebruijnIndex::INNERMOST); | ||
1128 | let type_ref = &db.type_alias_data(t).type_ref; | 1115 | let type_ref = &db.type_alias_data(t).type_ref; |
1129 | let inner = Ty::from_hir(&ctx, type_ref.as_ref().unwrap_or(&TypeRef::Error)); | 1116 | let inner = Ty::from_hir(&ctx, type_ref.as_ref().unwrap_or(&TypeRef::Error)); |
1130 | Binders::new(substs.len(), inner) | 1117 | Binders::new(substs.len(), inner) |
@@ -1147,7 +1134,7 @@ impl CallableDefId { | |||
1147 | CallableDefId::StructId(s) => s.lookup(db).container.module(db), | 1134 | CallableDefId::StructId(s) => s.lookup(db).container.module(db), |
1148 | CallableDefId::EnumVariantId(e) => e.parent.lookup(db).container.module(db), | 1135 | CallableDefId::EnumVariantId(e) => e.parent.lookup(db).container.module(db), |
1149 | } | 1136 | } |
1150 | .krate | 1137 | .krate() |
1151 | } | 1138 | } |
1152 | } | 1139 | } |
1153 | 1140 | ||
@@ -1186,7 +1173,7 @@ impl_from!(FunctionId, StructId, UnionId, EnumVariantId, ConstId, StaticId for V | |||
1186 | /// namespace. | 1173 | /// namespace. |
1187 | pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> { | 1174 | pub(crate) fn ty_query(db: &dyn HirDatabase, def: TyDefId) -> Binders<Ty> { |
1188 | match def { | 1175 | match def { |
1189 | TyDefId::BuiltinType(it) => Binders::new(0, type_for_builtin(it)), | 1176 | TyDefId::BuiltinType(it) => Binders::new(0, Ty::builtin(it)), |
1190 | TyDefId::AdtId(it) => type_for_adt(db, it), | 1177 | TyDefId::AdtId(it) => type_for_adt(db, it), |
1191 | TyDefId::TypeAliasId(it) => type_for_type_alias(db, it), | 1178 | TyDefId::TypeAliasId(it) => type_for_type_alias(db, it), |
1192 | } | 1179 | } |
@@ -1273,3 +1260,10 @@ pub(crate) fn return_type_impl_traits( | |||
1273 | Some(Arc::new(Binders::new(num_binders, return_type_impl_traits))) | 1260 | Some(Arc::new(Binders::new(num_binders, return_type_impl_traits))) |
1274 | } | 1261 | } |
1275 | } | 1262 | } |
1263 | |||
1264 | pub(crate) fn lower_to_chalk_mutability(m: hir_def::type_ref::Mutability) -> Mutability { | ||
1265 | match m { | ||
1266 | hir_def::type_ref::Mutability::Shared => Mutability::Not, | ||
1267 | hir_def::type_ref::Mutability::Mut => Mutability::Mut, | ||
1268 | } | ||
1269 | } | ||