aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/lib.rs')
-rw-r--r--crates/hir_ty/src/lib.rs76
1 files changed, 41 insertions, 35 deletions
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index 1131eaf92..9bcaf6fa7 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -111,6 +111,19 @@ pub struct FnPointer {
111 pub substs: Substs, 111 pub substs: Substs,
112} 112}
113 113
114#[derive(Clone, PartialEq, Eq, Debug, Hash)]
115pub enum AliasTy {
116 /// A "projection" type corresponds to an (unnormalized)
117 /// projection like `<P0 as Trait<P1..Pn>>::Foo`. Note that the
118 /// trait and all its parameters are fully known.
119 Projection(ProjectionTy),
120 /// An opaque type (`impl Trait`).
121 ///
122 /// This is currently only used for return type impl trait; each instance of
123 /// `impl Trait` in a return type gets its own ID.
124 Opaque(OpaqueTy),
125}
126
114/// A type. 127/// A type.
115/// 128///
116/// See also the `TyKind` enum in rustc (librustc/ty/sty.rs), which represents 129/// See also the `TyKind` enum in rustc (librustc/ty/sty.rs), which represents
@@ -141,7 +154,7 @@ pub enum Ty {
141 Slice(Substs), 154 Slice(Substs),
142 155
143 /// A raw pointer. Written as `*mut T` or `*const T` 156 /// A raw pointer. Written as `*mut T` or `*const T`
144 RawPtr(Mutability, Substs), 157 Raw(Mutability, Substs),
145 158
146 /// A reference; a pointer with an associated lifetime. Written as 159 /// A reference; a pointer with an associated lifetime. Written as
147 /// `&'a mut T` or `&'a T`. 160 /// `&'a mut T` or `&'a T`.
@@ -193,16 +206,11 @@ pub enum Ty {
193 /// ``` 206 /// ```
194 Function(FnPointer), 207 Function(FnPointer),
195 208
196 /// A "projection" type corresponds to an (unnormalized) 209 /// An "alias" type represents some form of type alias, such as:
197 /// projection like `<P0 as Trait<P1..Pn>>::Foo`. Note that the 210 /// - An associated type projection like `<T as Iterator>::Item`
198 /// trait and all its parameters are fully known. 211 /// - `impl Trait` types
199 Projection(ProjectionTy), 212 /// - Named type aliases like `type Foo<X> = Vec<X>`
200 213 Alias(AliasTy),
201 /// An opaque type (`impl Trait`).
202 ///
203 /// This is currently only used for return type impl trait; each instance of
204 /// `impl Trait` in a return type gets its own ID.
205 Opaque(OpaqueTy),
206 214
207 /// A placeholder for a type parameter; for example, `T` in `fn f<T>(x: T) 215 /// A placeholder for a type parameter; for example, `T` in `fn f<T>(x: T)
208 /// {}` when we're type-checking the body of that function. In this 216 /// {}` when we're type-checking the body of that function. In this
@@ -215,7 +223,7 @@ pub enum Ty {
215 /// parameters get turned into variables; during trait resolution, inference 223 /// parameters get turned into variables; during trait resolution, inference
216 /// variables get turned into bound variables and back; and in `Dyn` the 224 /// variables get turned into bound variables and back; and in `Dyn` the
217 /// `Self` type is represented with a bound variable as well. 225 /// `Self` type is represented with a bound variable as well.
218 Bound(BoundVar), 226 BoundVar(BoundVar),
219 227
220 /// A type variable used during type checking. 228 /// A type variable used during type checking.
221 InferenceVar(InferenceVar, TyVariableKind), 229 InferenceVar(InferenceVar, TyVariableKind),
@@ -299,7 +307,7 @@ impl Substs {
299 generic_params 307 generic_params
300 .iter() 308 .iter()
301 .enumerate() 309 .enumerate()
302 .map(|(idx, _)| Ty::Bound(BoundVar::new(debruijn, idx))) 310 .map(|(idx, _)| Ty::BoundVar(BoundVar::new(debruijn, idx)))
303 .collect(), 311 .collect(),
304 ) 312 )
305 } 313 }
@@ -347,7 +355,7 @@ impl SubstsBuilder {
347 } 355 }
348 356
349 pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self { 357 pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self {
350 self.fill((starting_from..).map(|idx| Ty::Bound(BoundVar::new(debruijn, idx)))) 358 self.fill((starting_from..).map(|idx| Ty::BoundVar(BoundVar::new(debruijn, idx))))
351 } 359 }
352 360
353 pub fn fill_with_unknown(self) -> Self { 361 pub fn fill_with_unknown(self) -> Self {
@@ -627,7 +635,7 @@ impl Ty {
627 Ty::Ref(mutability, parameters) => { 635 Ty::Ref(mutability, parameters) => {
628 Some((parameters.as_single(), Rawness::Ref, *mutability)) 636 Some((parameters.as_single(), Rawness::Ref, *mutability))
629 } 637 }
630 Ty::RawPtr(mutability, parameters) => { 638 Ty::Raw(mutability, parameters) => {
631 Some((parameters.as_single(), Rawness::RawPtr, *mutability)) 639 Some((parameters.as_single(), Rawness::RawPtr, *mutability))
632 } 640 }
633 _ => None, 641 _ => None,
@@ -688,9 +696,7 @@ impl Ty {
688 expr == expr2 && def == def2 696 expr == expr2 && def == def2
689 } 697 }
690 (Ty::Ref(mutability, ..), Ty::Ref(mutability2, ..)) 698 (Ty::Ref(mutability, ..), Ty::Ref(mutability2, ..))
691 | (Ty::RawPtr(mutability, ..), Ty::RawPtr(mutability2, ..)) => { 699 | (Ty::Raw(mutability, ..), Ty::Raw(mutability2, ..)) => mutability == mutability2,
692 mutability == mutability2
693 }
694 ( 700 (
695 Ty::Function(FnPointer { num_args, sig, .. }), 701 Ty::Function(FnPointer { num_args, sig, .. }),
696 Ty::Function(FnPointer { num_args: num_args2, sig: sig2, .. }), 702 Ty::Function(FnPointer { num_args: num_args2, sig: sig2, .. }),
@@ -721,7 +727,7 @@ impl Ty {
721 fn builtin_deref(&self) -> Option<Ty> { 727 fn builtin_deref(&self) -> Option<Ty> {
722 match self { 728 match self {
723 Ty::Ref(.., parameters) => Some(Ty::clone(parameters.as_single())), 729 Ty::Ref(.., parameters) => Some(Ty::clone(parameters.as_single())),
724 Ty::RawPtr(.., parameters) => Some(Ty::clone(parameters.as_single())), 730 Ty::Raw(.., parameters) => Some(Ty::clone(parameters.as_single())),
725 _ => None, 731 _ => None,
726 } 732 }
727 } 733 }
@@ -757,7 +763,7 @@ impl Ty {
757 Ty::Adt(_, substs) 763 Ty::Adt(_, substs)
758 | Ty::Slice(substs) 764 | Ty::Slice(substs)
759 | Ty::Array(substs) 765 | Ty::Array(substs)
760 | Ty::RawPtr(_, substs) 766 | Ty::Raw(_, substs)
761 | Ty::Ref(_, substs) 767 | Ty::Ref(_, substs)
762 | Ty::FnDef(_, substs) 768 | Ty::FnDef(_, substs)
763 | Ty::Function(FnPointer { substs, .. }) 769 | Ty::Function(FnPointer { substs, .. })
@@ -780,7 +786,7 @@ impl Ty {
780 Ty::Adt(_, substs) 786 Ty::Adt(_, substs)
781 | Ty::Slice(substs) 787 | Ty::Slice(substs)
782 | Ty::Array(substs) 788 | Ty::Array(substs)
783 | Ty::RawPtr(_, substs) 789 | Ty::Raw(_, substs)
784 | Ty::Ref(_, substs) 790 | Ty::Ref(_, substs)
785 | Ty::FnDef(_, substs) 791 | Ty::FnDef(_, substs)
786 | Ty::Function(FnPointer { substs, .. }) 792 | Ty::Function(FnPointer { substs, .. })
@@ -797,7 +803,7 @@ impl Ty {
797 Ty::Adt(_, substs) 803 Ty::Adt(_, substs)
798 | Ty::Slice(substs) 804 | Ty::Slice(substs)
799 | Ty::Array(substs) 805 | Ty::Array(substs)
800 | Ty::RawPtr(_, substs) 806 | Ty::Raw(_, substs)
801 | Ty::Ref(_, substs) 807 | Ty::Ref(_, substs)
802 | Ty::FnDef(_, substs) 808 | Ty::FnDef(_, substs)
803 | Ty::Function(FnPointer { substs, .. }) 809 | Ty::Function(FnPointer { substs, .. })
@@ -834,7 +840,7 @@ impl Ty {
834 OpaqueTyId::ReturnTypeImplTrait(..) => None, 840 OpaqueTyId::ReturnTypeImplTrait(..) => None,
835 } 841 }
836 } 842 }
837 Ty::Opaque(opaque_ty) => { 843 Ty::Alias(AliasTy::Opaque(opaque_ty)) => {
838 let predicates = match opaque_ty.opaque_ty_id { 844 let predicates = match opaque_ty.opaque_ty_id {
839 OpaqueTyId::ReturnTypeImplTrait(func, idx) => { 845 OpaqueTyId::ReturnTypeImplTrait(func, idx) => {
840 db.return_type_impl_traits(func).map(|it| { 846 db.return_type_impl_traits(func).map(|it| {
@@ -878,7 +884,7 @@ impl Ty {
878 _ => None, 884 _ => None,
879 } 885 }
880 } 886 }
881 Ty::Projection(projection_ty) => { 887 Ty::Alias(AliasTy::Projection(projection_ty)) => {
882 match projection_ty.associated_ty.lookup(db.upcast()).container { 888 match projection_ty.associated_ty.lookup(db.upcast()).container {
883 AssocContainerId::TraitId(trait_id) => Some(trait_id), 889 AssocContainerId::TraitId(trait_id) => Some(trait_id),
884 _ => None, 890 _ => None,
@@ -956,7 +962,7 @@ pub trait TypeWalk {
956 { 962 {
957 self.walk_mut_binders( 963 self.walk_mut_binders(
958 &mut |ty, binders| { 964 &mut |ty, binders| {
959 if let &mut Ty::Bound(bound) = ty { 965 if let &mut Ty::BoundVar(bound) = ty {
960 if bound.debruijn >= binders { 966 if bound.debruijn >= binders {
961 *ty = substs.0[bound.index].clone().shift_bound_vars(binders); 967 *ty = substs.0[bound.index].clone().shift_bound_vars(binders);
962 } 968 }
@@ -974,8 +980,8 @@ pub trait TypeWalk {
974 { 980 {
975 self.fold_binders( 981 self.fold_binders(
976 &mut |ty, binders| match ty { 982 &mut |ty, binders| match ty {
977 Ty::Bound(bound) if bound.debruijn >= binders => { 983 Ty::BoundVar(bound) if bound.debruijn >= binders => {
978 Ty::Bound(bound.shifted_in_from(n)) 984 Ty::BoundVar(bound.shifted_in_from(n))
979 } 985 }
980 ty => ty, 986 ty => ty,
981 }, 987 },
@@ -987,21 +993,21 @@ pub trait TypeWalk {
987impl TypeWalk for Ty { 993impl TypeWalk for Ty {
988 fn walk(&self, f: &mut impl FnMut(&Ty)) { 994 fn walk(&self, f: &mut impl FnMut(&Ty)) {
989 match self { 995 match self {
990 Ty::Projection(p_ty) => { 996 Ty::Alias(AliasTy::Projection(p_ty)) => {
991 for t in p_ty.parameters.iter() { 997 for t in p_ty.parameters.iter() {
992 t.walk(f); 998 t.walk(f);
993 } 999 }
994 } 1000 }
1001 Ty::Alias(AliasTy::Opaque(o_ty)) => {
1002 for t in o_ty.parameters.iter() {
1003 t.walk(f);
1004 }
1005 }
995 Ty::Dyn(predicates) => { 1006 Ty::Dyn(predicates) => {
996 for p in predicates.iter() { 1007 for p in predicates.iter() {
997 p.walk(f); 1008 p.walk(f);
998 } 1009 }
999 } 1010 }
1000 Ty::Opaque(o_ty) => {
1001 for t in o_ty.parameters.iter() {
1002 t.walk(f);
1003 }
1004 }
1005 _ => { 1011 _ => {
1006 if let Some(substs) = self.substs() { 1012 if let Some(substs) = self.substs() {
1007 for t in substs.iter() { 1013 for t in substs.iter() {
@@ -1019,7 +1025,7 @@ impl TypeWalk for Ty {
1019 binders: DebruijnIndex, 1025 binders: DebruijnIndex,
1020 ) { 1026 ) {
1021 match self { 1027 match self {
1022 Ty::Projection(p_ty) => { 1028 Ty::Alias(AliasTy::Projection(p_ty)) => {
1023 p_ty.parameters.walk_mut_binders(f, binders); 1029 p_ty.parameters.walk_mut_binders(f, binders);
1024 } 1030 }
1025 Ty::Dyn(predicates) => { 1031 Ty::Dyn(predicates) => {
@@ -1027,7 +1033,7 @@ impl TypeWalk for Ty {
1027 p.walk_mut_binders(f, binders.shifted_in()); 1033 p.walk_mut_binders(f, binders.shifted_in());
1028 } 1034 }
1029 } 1035 }
1030 Ty::Opaque(o_ty) => { 1036 Ty::Alias(AliasTy::Opaque(o_ty)) => {
1031 o_ty.parameters.walk_mut_binders(f, binders); 1037 o_ty.parameters.walk_mut_binders(f, binders);
1032 } 1038 }
1033 _ => { 1039 _ => {