aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/lib.rs
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2021-03-14 16:40:55 +0000
committerFlorian Diebold <[email protected]>2021-03-14 19:21:05 +0000
commit42217738e0b121a8e5d48a9a55cb51ef6c98975f (patch)
tree63b3fc22a011ae0ee37a91cb19f5dcfe390507f1 /crates/hir_ty/src/lib.rs
parentaf466f8542173002361eb134e66102908c7cd024 (diff)
Don't use Substs for Ref/Raw/Array/Slice
Diffstat (limited to 'crates/hir_ty/src/lib.rs')
-rw-r--r--crates/hir_ty/src/lib.rs67
1 files changed, 18 insertions, 49 deletions
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index 0b2da8971..503910dde 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -151,17 +151,17 @@ pub enum TyKind {
151 Tuple(usize, Substs), 151 Tuple(usize, Substs),
152 152
153 /// An array with the given length. Written as `[T; n]`. 153 /// An array with the given length. Written as `[T; n]`.
154 Array(Substs), 154 Array(Ty),
155 155
156 /// The pointee of an array slice. Written as `[T]`. 156 /// The pointee of an array slice. Written as `[T]`.
157 Slice(Substs), 157 Slice(Ty),
158 158
159 /// A raw pointer. Written as `*mut T` or `*const T` 159 /// A raw pointer. Written as `*mut T` or `*const T`
160 Raw(Mutability, Substs), 160 Raw(Mutability, Ty),
161 161
162 /// A reference; a pointer with an associated lifetime. Written as 162 /// A reference; a pointer with an associated lifetime. Written as
163 /// `&'a mut T` or `&'a T`. 163 /// `&'a mut T` or `&'a T`.
164 Ref(Mutability, Substs), 164 Ref(Mutability, Ty),
165 165
166 /// This represents a placeholder for an opaque type in situations where we 166 /// This represents a placeholder for an opaque type in situations where we
167 /// don't know the hidden type (i.e. currently almost always). This is 167 /// don't know the hidden type (i.e. currently almost always). This is
@@ -673,19 +673,15 @@ impl Ty {
673 673
674 pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { 674 pub fn as_reference(&self) -> Option<(&Ty, Mutability)> {
675 match self.interned(&Interner) { 675 match self.interned(&Interner) {
676 TyKind::Ref(mutability, parameters) => Some((parameters.as_single(), *mutability)), 676 TyKind::Ref(mutability, ty) => Some((ty, *mutability)),
677 _ => None, 677 _ => None,
678 } 678 }
679 } 679 }
680 680
681 pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> { 681 pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> {
682 match self.interned(&Interner) { 682 match self.interned(&Interner) {
683 TyKind::Ref(mutability, parameters) => { 683 TyKind::Ref(mutability, ty) => Some((ty, Rawness::Ref, *mutability)),
684 Some((parameters.as_single(), Rawness::Ref, *mutability)) 684 TyKind::Raw(mutability, ty) => Some((ty, Rawness::RawPtr, *mutability)),
685 }
686 TyKind::Raw(mutability, parameters) => {
687 Some((parameters.as_single(), Rawness::RawPtr, *mutability))
688 }
689 _ => None, 685 _ => None,
690 } 686 }
691 } 687 }
@@ -693,8 +689,8 @@ impl Ty {
693 pub fn strip_references(&self) -> &Ty { 689 pub fn strip_references(&self) -> &Ty {
694 let mut t: &Ty = self; 690 let mut t: &Ty = self;
695 691
696 while let TyKind::Ref(_mutability, parameters) = t.interned(&Interner) { 692 while let TyKind::Ref(_mutability, ty) = t.interned(&Interner) {
697 t = parameters.as_single(); 693 t = ty;
698 } 694 }
699 695
700 t 696 t
@@ -780,8 +776,8 @@ impl Ty {
780 776
781 fn builtin_deref(&self) -> Option<Ty> { 777 fn builtin_deref(&self) -> Option<Ty> {
782 match self.interned(&Interner) { 778 match self.interned(&Interner) {
783 TyKind::Ref(.., parameters) => Some(Ty::clone(parameters.as_single())), 779 TyKind::Ref(.., ty) => Some(ty.clone()),
784 TyKind::Raw(.., parameters) => Some(Ty::clone(parameters.as_single())), 780 TyKind::Raw(.., ty) => Some(ty.clone()),
785 _ => None, 781 _ => None,
786 } 782 }
787 } 783 }
@@ -817,40 +813,11 @@ impl Ty {
817 } 813 }
818 } 814 }
819 815
820 /// If this is a type with type parameters (an ADT or function), replaces
821 /// the `Substs` for these type parameters with the given ones. (So e.g. if
822 /// `self` is `Option<_>` and the substs contain `u32`, we'll have
823 /// `Option<u32>` afterwards.)
824 pub fn apply_substs(mut self, new_substs: Substs) -> Ty {
825 match self.interned_mut() {
826 TyKind::Adt(_, substs)
827 | TyKind::Slice(substs)
828 | TyKind::Array(substs)
829 | TyKind::Raw(_, substs)
830 | TyKind::Ref(_, substs)
831 | TyKind::FnDef(_, substs)
832 | TyKind::Function(FnPointer { substs, .. })
833 | TyKind::Tuple(_, substs)
834 | TyKind::OpaqueType(_, substs)
835 | TyKind::AssociatedType(_, substs)
836 | TyKind::Closure(.., substs) => {
837 assert_eq!(substs.len(), new_substs.len());
838 *substs = new_substs;
839 }
840 _ => (),
841 }
842 self
843 }
844
845 /// Returns the type parameters of this type if it has some (i.e. is an ADT 816 /// Returns the type parameters of this type if it has some (i.e. is an ADT
846 /// or function); so if `self` is `Option<u32>`, this returns the `u32`. 817 /// or function); so if `self` is `Option<u32>`, this returns the `u32`.
847 pub fn substs(&self) -> Option<&Substs> { 818 pub fn substs(&self) -> Option<&Substs> {
848 match self.interned(&Interner) { 819 match self.interned(&Interner) {
849 TyKind::Adt(_, substs) 820 TyKind::Adt(_, substs)
850 | TyKind::Slice(substs)
851 | TyKind::Array(substs)
852 | TyKind::Raw(_, substs)
853 | TyKind::Ref(_, substs)
854 | TyKind::FnDef(_, substs) 821 | TyKind::FnDef(_, substs)
855 | TyKind::Function(FnPointer { substs, .. }) 822 | TyKind::Function(FnPointer { substs, .. })
856 | TyKind::Tuple(_, substs) 823 | TyKind::Tuple(_, substs)
@@ -861,13 +828,9 @@ impl Ty {
861 } 828 }
862 } 829 }
863 830
864 pub fn substs_mut(&mut self) -> Option<&mut Substs> { 831 fn substs_mut(&mut self) -> Option<&mut Substs> {
865 match self.interned_mut() { 832 match self.interned_mut() {
866 TyKind::Adt(_, substs) 833 TyKind::Adt(_, substs)
867 | TyKind::Slice(substs)
868 | TyKind::Array(substs)
869 | TyKind::Raw(_, substs)
870 | TyKind::Ref(_, substs)
871 | TyKind::FnDef(_, substs) 834 | TyKind::FnDef(_, substs)
872 | TyKind::Function(FnPointer { substs, .. }) 835 | TyKind::Function(FnPointer { substs, .. })
873 | TyKind::Tuple(_, substs) 836 | TyKind::Tuple(_, substs)
@@ -1076,6 +1039,9 @@ impl TypeWalk for Ty {
1076 p.walk(f); 1039 p.walk(f);
1077 } 1040 }
1078 } 1041 }
1042 TyKind::Slice(ty) | TyKind::Array(ty) | TyKind::Ref(_, ty) | TyKind::Raw(_, ty) => {
1043 ty.walk(f);
1044 }
1079 _ => { 1045 _ => {
1080 if let Some(substs) = self.substs() { 1046 if let Some(substs) = self.substs() {
1081 for t in substs.iter() { 1047 for t in substs.iter() {
@@ -1104,6 +1070,9 @@ impl TypeWalk for Ty {
1104 TyKind::Alias(AliasTy::Opaque(o_ty)) => { 1070 TyKind::Alias(AliasTy::Opaque(o_ty)) => {
1105 o_ty.substitution.walk_mut_binders(f, binders); 1071 o_ty.substitution.walk_mut_binders(f, binders);
1106 } 1072 }
1073 TyKind::Slice(ty) | TyKind::Array(ty) | TyKind::Ref(_, ty) | TyKind::Raw(_, ty) => {
1074 ty.walk_mut_binders(f, binders);
1075 }
1107 _ => { 1076 _ => {
1108 if let Some(substs) = self.substs_mut() { 1077 if let Some(substs) = self.substs_mut() {
1109 substs.walk_mut_binders(f, binders); 1078 substs.walk_mut_binders(f, binders);