diff options
Diffstat (limited to 'crates/hir_ty/src/lib.rs')
-rw-r--r-- | crates/hir_ty/src/lib.rs | 94 |
1 files changed, 47 insertions, 47 deletions
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 850385280..52b498ff7 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs | |||
@@ -67,7 +67,7 @@ pub enum Lifetime { | |||
67 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 67 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
68 | pub struct OpaqueTy { | 68 | pub struct OpaqueTy { |
69 | pub opaque_ty_id: OpaqueTyId, | 69 | pub opaque_ty_id: OpaqueTyId, |
70 | pub substitution: Substs, | 70 | pub substitution: Substitution, |
71 | } | 71 | } |
72 | 72 | ||
73 | /// A "projection" type corresponds to an (unnormalized) | 73 | /// A "projection" type corresponds to an (unnormalized) |
@@ -76,7 +76,7 @@ pub struct OpaqueTy { | |||
76 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 76 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
77 | pub struct ProjectionTy { | 77 | pub struct ProjectionTy { |
78 | pub associated_ty_id: AssocTypeId, | 78 | pub associated_ty_id: AssocTypeId, |
79 | pub substitution: Substs, | 79 | pub substitution: Substitution, |
80 | } | 80 | } |
81 | 81 | ||
82 | impl ProjectionTy { | 82 | impl ProjectionTy { |
@@ -112,7 +112,7 @@ pub type FnSig = chalk_ir::FnSig<Interner>; | |||
112 | pub struct FnPointer { | 112 | pub struct FnPointer { |
113 | pub num_args: usize, | 113 | pub num_args: usize, |
114 | pub sig: FnSig, | 114 | pub sig: FnSig, |
115 | pub substs: Substs, | 115 | pub substs: Substitution, |
116 | } | 116 | } |
117 | 117 | ||
118 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 118 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
@@ -137,19 +137,19 @@ pub enum AliasTy { | |||
137 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 137 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
138 | pub enum TyKind { | 138 | pub enum TyKind { |
139 | /// Structures, enumerations and unions. | 139 | /// Structures, enumerations and unions. |
140 | Adt(AdtId<Interner>, Substs), | 140 | Adt(AdtId<Interner>, Substitution), |
141 | 141 | ||
142 | /// Represents an associated item like `Iterator::Item`. This is used | 142 | /// Represents an associated item like `Iterator::Item`. This is used |
143 | /// when we have tried to normalize a projection like `T::Item` but | 143 | /// when we have tried to normalize a projection like `T::Item` but |
144 | /// couldn't find a better representation. In that case, we generate | 144 | /// couldn't find a better representation. In that case, we generate |
145 | /// an **application type** like `(Iterator::Item)<T>`. | 145 | /// an **application type** like `(Iterator::Item)<T>`. |
146 | AssociatedType(AssocTypeId, Substs), | 146 | AssociatedType(AssocTypeId, Substitution), |
147 | 147 | ||
148 | /// a scalar type like `bool` or `u32` | 148 | /// a scalar type like `bool` or `u32` |
149 | Scalar(Scalar), | 149 | Scalar(Scalar), |
150 | 150 | ||
151 | /// A tuple type. For example, `(i32, bool)`. | 151 | /// A tuple type. For example, `(i32, bool)`. |
152 | Tuple(usize, Substs), | 152 | Tuple(usize, Substitution), |
153 | 153 | ||
154 | /// An array with the given length. Written as `[T; n]`. | 154 | /// An array with the given length. Written as `[T; n]`. |
155 | Array(Ty), | 155 | Array(Ty), |
@@ -169,7 +169,7 @@ pub enum TyKind { | |||
169 | /// analogous to the `AssociatedType` type constructor. | 169 | /// analogous to the `AssociatedType` type constructor. |
170 | /// It is also used as the type of async block, with one type parameter | 170 | /// It is also used as the type of async block, with one type parameter |
171 | /// representing the Future::Output type. | 171 | /// representing the Future::Output type. |
172 | OpaqueType(OpaqueTyId, Substs), | 172 | OpaqueType(OpaqueTyId, Substitution), |
173 | 173 | ||
174 | /// The anonymous type of a function declaration/definition. Each | 174 | /// The anonymous type of a function declaration/definition. Each |
175 | /// function has a unique type, which is output (for a function | 175 | /// function has a unique type, which is output (for a function |
@@ -183,7 +183,7 @@ pub enum TyKind { | |||
183 | /// fn foo() -> i32 { 1 } | 183 | /// fn foo() -> i32 { 1 } |
184 | /// let bar = foo; // bar: fn() -> i32 {foo} | 184 | /// let bar = foo; // bar: fn() -> i32 {foo} |
185 | /// ``` | 185 | /// ``` |
186 | FnDef(FnDefId, Substs), | 186 | FnDef(FnDefId, Substitution), |
187 | 187 | ||
188 | /// The pointee of a string slice. Written as `str`. | 188 | /// The pointee of a string slice. Written as `str`. |
189 | Str, | 189 | Str, |
@@ -195,7 +195,7 @@ pub enum TyKind { | |||
195 | /// | 195 | /// |
196 | /// The closure signature is stored in a `FnPtr` type in the first type | 196 | /// The closure signature is stored in a `FnPtr` type in the first type |
197 | /// parameter. | 197 | /// parameter. |
198 | Closure(ClosureId, Substs), | 198 | Closure(ClosureId, Substitution), |
199 | 199 | ||
200 | /// Represents a foreign type declared in external blocks. | 200 | /// Represents a foreign type declared in external blocks. |
201 | ForeignType(ForeignDefId), | 201 | ForeignType(ForeignDefId), |
@@ -273,9 +273,9 @@ impl Ty { | |||
273 | 273 | ||
274 | /// A list of substitutions for generic parameters. | 274 | /// A list of substitutions for generic parameters. |
275 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 275 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
276 | pub struct Substs(SmallVec<[Ty; 2]>); | 276 | pub struct Substitution(SmallVec<[Ty; 2]>); |
277 | 277 | ||
278 | impl TypeWalk for Substs { | 278 | impl TypeWalk for Substitution { |
279 | fn walk(&self, f: &mut impl FnMut(&Ty)) { | 279 | fn walk(&self, f: &mut impl FnMut(&Ty)) { |
280 | for t in self.0.iter() { | 280 | for t in self.0.iter() { |
281 | t.walk(f); | 281 | t.walk(f); |
@@ -293,29 +293,29 @@ impl TypeWalk for Substs { | |||
293 | } | 293 | } |
294 | } | 294 | } |
295 | 295 | ||
296 | impl Substs { | 296 | impl Substitution { |
297 | pub fn interned(&self, _: &Interner) -> &[Ty] { | 297 | pub fn interned(&self, _: &Interner) -> &[Ty] { |
298 | &self.0 | 298 | &self.0 |
299 | } | 299 | } |
300 | 300 | ||
301 | pub fn empty() -> Substs { | 301 | pub fn empty() -> Substitution { |
302 | Substs(SmallVec::new()) | 302 | Substitution(SmallVec::new()) |
303 | } | 303 | } |
304 | 304 | ||
305 | pub fn single(ty: Ty) -> Substs { | 305 | pub fn single(ty: Ty) -> Substitution { |
306 | Substs({ | 306 | Substitution({ |
307 | let mut v = SmallVec::new(); | 307 | let mut v = SmallVec::new(); |
308 | v.push(ty); | 308 | v.push(ty); |
309 | v | 309 | v |
310 | }) | 310 | }) |
311 | } | 311 | } |
312 | 312 | ||
313 | pub fn prefix(&self, n: usize) -> Substs { | 313 | pub fn prefix(&self, n: usize) -> Substitution { |
314 | Substs(self.0[..std::cmp::min(self.0.len(), n)].into()) | 314 | Substitution(self.0[..std::cmp::min(self.0.len(), n)].into()) |
315 | } | 315 | } |
316 | 316 | ||
317 | pub fn suffix(&self, n: usize) -> Substs { | 317 | pub fn suffix(&self, n: usize) -> Substitution { |
318 | Substs(self.0[self.0.len() - std::cmp::min(self.0.len(), n)..].into()) | 318 | Substitution(self.0[self.0.len() - std::cmp::min(self.0.len(), n)..].into()) |
319 | } | 319 | } |
320 | 320 | ||
321 | pub fn as_single(&self) -> &Ty { | 321 | pub fn as_single(&self) -> &Ty { |
@@ -326,15 +326,15 @@ impl Substs { | |||
326 | } | 326 | } |
327 | 327 | ||
328 | pub fn from_iter(_interner: &Interner, elements: impl IntoIterator<Item = Ty>) -> Self { | 328 | pub fn from_iter(_interner: &Interner, elements: impl IntoIterator<Item = Ty>) -> Self { |
329 | Substs(elements.into_iter().collect()) | 329 | Substitution(elements.into_iter().collect()) |
330 | } | 330 | } |
331 | 331 | ||
332 | /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). | 332 | /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). |
333 | pub(crate) fn type_params_for_generics( | 333 | pub(crate) fn type_params_for_generics( |
334 | db: &dyn HirDatabase, | 334 | db: &dyn HirDatabase, |
335 | generic_params: &Generics, | 335 | generic_params: &Generics, |
336 | ) -> Substs { | 336 | ) -> Substitution { |
337 | Substs( | 337 | Substitution( |
338 | generic_params | 338 | generic_params |
339 | .iter() | 339 | .iter() |
340 | .map(|(id, _)| TyKind::Placeholder(to_placeholder_idx(db, id)).intern(&Interner)) | 340 | .map(|(id, _)| TyKind::Placeholder(to_placeholder_idx(db, id)).intern(&Interner)) |
@@ -343,14 +343,14 @@ impl Substs { | |||
343 | } | 343 | } |
344 | 344 | ||
345 | /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). | 345 | /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). |
346 | pub fn type_params(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> Substs { | 346 | pub fn type_params(db: &dyn HirDatabase, def: impl Into<GenericDefId>) -> Substitution { |
347 | let params = generics(db.upcast(), def.into()); | 347 | let params = generics(db.upcast(), def.into()); |
348 | Substs::type_params_for_generics(db, ¶ms) | 348 | Substitution::type_params_for_generics(db, ¶ms) |
349 | } | 349 | } |
350 | 350 | ||
351 | /// Return Substs that replace each parameter by a bound variable. | 351 | /// Return Substs that replace each parameter by a bound variable. |
352 | pub(crate) fn bound_vars(generic_params: &Generics, debruijn: DebruijnIndex) -> Substs { | 352 | pub(crate) fn bound_vars(generic_params: &Generics, debruijn: DebruijnIndex) -> Substitution { |
353 | Substs( | 353 | Substitution( |
354 | generic_params | 354 | generic_params |
355 | .iter() | 355 | .iter() |
356 | .enumerate() | 356 | .enumerate() |
@@ -363,11 +363,11 @@ impl Substs { | |||
363 | let def = def.into(); | 363 | let def = def.into(); |
364 | let params = generics(db.upcast(), def); | 364 | let params = generics(db.upcast(), def); |
365 | let param_count = params.len(); | 365 | let param_count = params.len(); |
366 | Substs::builder(param_count) | 366 | Substitution::builder(param_count) |
367 | } | 367 | } |
368 | 368 | ||
369 | pub(crate) fn build_for_generics(generic_params: &Generics) -> SubstsBuilder { | 369 | pub(crate) fn build_for_generics(generic_params: &Generics) -> SubstsBuilder { |
370 | Substs::builder(generic_params.len()) | 370 | Substitution::builder(generic_params.len()) |
371 | } | 371 | } |
372 | 372 | ||
373 | fn builder(param_count: usize) -> SubstsBuilder { | 373 | fn builder(param_count: usize) -> SubstsBuilder { |
@@ -387,9 +387,9 @@ pub struct SubstsBuilder { | |||
387 | } | 387 | } |
388 | 388 | ||
389 | impl SubstsBuilder { | 389 | impl SubstsBuilder { |
390 | pub fn build(self) -> Substs { | 390 | pub fn build(self) -> Substitution { |
391 | assert_eq!(self.vec.len(), self.param_count); | 391 | assert_eq!(self.vec.len(), self.param_count); |
392 | Substs(self.vec.into()) | 392 | Substitution(self.vec.into()) |
393 | } | 393 | } |
394 | 394 | ||
395 | pub fn push(mut self, ty: Ty) -> Self { | 395 | pub fn push(mut self, ty: Ty) -> Self { |
@@ -418,7 +418,7 @@ impl SubstsBuilder { | |||
418 | self | 418 | self |
419 | } | 419 | } |
420 | 420 | ||
421 | pub fn use_parent_substs(mut self, parent_substs: &Substs) -> Self { | 421 | pub fn use_parent_substs(mut self, parent_substs: &Substitution) -> Self { |
422 | assert!(self.vec.is_empty()); | 422 | assert!(self.vec.is_empty()); |
423 | assert!(parent_substs.len() <= self.param_count); | 423 | assert!(parent_substs.len() <= self.param_count); |
424 | self.vec.extend(parent_substs.iter().cloned()); | 424 | self.vec.extend(parent_substs.iter().cloned()); |
@@ -426,7 +426,7 @@ impl SubstsBuilder { | |||
426 | } | 426 | } |
427 | } | 427 | } |
428 | 428 | ||
429 | impl Deref for Substs { | 429 | impl Deref for Substitution { |
430 | type Target = [Ty]; | 430 | type Target = [Ty]; |
431 | 431 | ||
432 | fn deref(&self) -> &[Ty] { | 432 | fn deref(&self) -> &[Ty] { |
@@ -466,13 +466,13 @@ impl<T: Clone> Binders<&T> { | |||
466 | 466 | ||
467 | impl<T: TypeWalk> Binders<T> { | 467 | impl<T: TypeWalk> Binders<T> { |
468 | /// Substitutes all variables. | 468 | /// Substitutes all variables. |
469 | pub fn subst(self, subst: &Substs) -> T { | 469 | pub fn subst(self, subst: &Substitution) -> T { |
470 | assert_eq!(subst.len(), self.num_binders); | 470 | assert_eq!(subst.len(), self.num_binders); |
471 | self.value.subst_bound_vars(subst) | 471 | self.value.subst_bound_vars(subst) |
472 | } | 472 | } |
473 | 473 | ||
474 | /// Substitutes just a prefix of the variables (shifting the rest). | 474 | /// Substitutes just a prefix of the variables (shifting the rest). |
475 | pub fn subst_prefix(self, subst: &Substs) -> Binders<T> { | 475 | pub fn subst_prefix(self, subst: &Substitution) -> Binders<T> { |
476 | assert!(subst.len() < self.num_binders); | 476 | assert!(subst.len() < self.num_binders); |
477 | Binders::new(self.num_binders - subst.len(), self.value.subst_bound_vars(subst)) | 477 | Binders::new(self.num_binders - subst.len(), self.value.subst_bound_vars(subst)) |
478 | } | 478 | } |
@@ -498,7 +498,7 @@ impl<T: TypeWalk> TypeWalk for Binders<T> { | |||
498 | pub struct TraitRef { | 498 | pub struct TraitRef { |
499 | /// FIXME name? | 499 | /// FIXME name? |
500 | pub trait_: TraitId, | 500 | pub trait_: TraitId, |
501 | pub substs: Substs, | 501 | pub substs: Substitution, |
502 | } | 502 | } |
503 | 503 | ||
504 | impl TraitRef { | 504 | impl TraitRef { |
@@ -618,7 +618,7 @@ impl CallableSig { | |||
618 | } | 618 | } |
619 | } | 619 | } |
620 | 620 | ||
621 | pub fn from_substs(substs: &Substs) -> CallableSig { | 621 | pub fn from_substs(substs: &Substitution) -> CallableSig { |
622 | CallableSig { params_and_return: substs.iter().cloned().collect(), is_varargs: false } | 622 | CallableSig { params_and_return: substs.iter().cloned().collect(), is_varargs: false } |
623 | } | 623 | } |
624 | 624 | ||
@@ -651,10 +651,10 @@ impl TypeWalk for CallableSig { | |||
651 | 651 | ||
652 | impl Ty { | 652 | impl Ty { |
653 | pub fn unit() -> Self { | 653 | pub fn unit() -> Self { |
654 | TyKind::Tuple(0, Substs::empty()).intern(&Interner) | 654 | TyKind::Tuple(0, Substitution::empty()).intern(&Interner) |
655 | } | 655 | } |
656 | 656 | ||
657 | pub fn adt_ty(adt: hir_def::AdtId, substs: Substs) -> Ty { | 657 | pub fn adt_ty(adt: hir_def::AdtId, substs: Substitution) -> Ty { |
658 | TyKind::Adt(AdtId(adt), substs).intern(&Interner) | 658 | TyKind::Adt(AdtId(adt), substs).intern(&Interner) |
659 | } | 659 | } |
660 | 660 | ||
@@ -662,7 +662,7 @@ impl Ty { | |||
662 | TyKind::Function(FnPointer { | 662 | TyKind::Function(FnPointer { |
663 | num_args: sig.params().len(), | 663 | num_args: sig.params().len(), |
664 | sig: FnSig { abi: (), safety: Safety::Safe, variadic: sig.is_varargs }, | 664 | sig: FnSig { abi: (), safety: Safety::Safe, variadic: sig.is_varargs }, |
665 | substs: Substs::from_iter(&Interner, sig.params_and_return.iter().cloned()), | 665 | substs: Substitution::from_iter(&Interner, sig.params_and_return.iter().cloned()), |
666 | }) | 666 | }) |
667 | .intern(&Interner) | 667 | .intern(&Interner) |
668 | } | 668 | } |
@@ -709,14 +709,14 @@ impl Ty { | |||
709 | t | 709 | t |
710 | } | 710 | } |
711 | 711 | ||
712 | pub fn as_adt(&self) -> Option<(hir_def::AdtId, &Substs)> { | 712 | pub fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)> { |
713 | match self.interned(&Interner) { | 713 | match self.interned(&Interner) { |
714 | TyKind::Adt(AdtId(adt), parameters) => Some((*adt, parameters)), | 714 | TyKind::Adt(AdtId(adt), parameters) => Some((*adt, parameters)), |
715 | _ => None, | 715 | _ => None, |
716 | } | 716 | } |
717 | } | 717 | } |
718 | 718 | ||
719 | pub fn as_tuple(&self) -> Option<&Substs> { | 719 | pub fn as_tuple(&self) -> Option<&Substitution> { |
720 | match self.interned(&Interner) { | 720 | match self.interned(&Interner) { |
721 | TyKind::Tuple(_, substs) => Some(substs), | 721 | TyKind::Tuple(_, substs) => Some(substs), |
722 | _ => None, | 722 | _ => None, |
@@ -828,7 +828,7 @@ impl Ty { | |||
828 | 828 | ||
829 | /// Returns the type parameters of this type if it has some (i.e. is an ADT | 829 | /// Returns the type parameters of this type if it has some (i.e. is an ADT |
830 | /// or function); so if `self` is `Option<u32>`, this returns the `u32`. | 830 | /// or function); so if `self` is `Option<u32>`, this returns the `u32`. |
831 | pub fn substs(&self) -> Option<&Substs> { | 831 | pub fn substs(&self) -> Option<&Substitution> { |
832 | match self.interned(&Interner) { | 832 | match self.interned(&Interner) { |
833 | TyKind::Adt(_, substs) | 833 | TyKind::Adt(_, substs) |
834 | | TyKind::FnDef(_, substs) | 834 | | TyKind::FnDef(_, substs) |
@@ -841,7 +841,7 @@ impl Ty { | |||
841 | } | 841 | } |
842 | } | 842 | } |
843 | 843 | ||
844 | fn substs_mut(&mut self) -> Option<&mut Substs> { | 844 | fn substs_mut(&mut self) -> Option<&mut Substitution> { |
845 | match self.interned_mut() { | 845 | match self.interned_mut() { |
846 | TyKind::Adt(_, substs) | 846 | TyKind::Adt(_, substs) |
847 | | TyKind::FnDef(_, substs) | 847 | | TyKind::FnDef(_, substs) |
@@ -869,7 +869,7 @@ impl Ty { | |||
869 | // So just provide the Future trait. | 869 | // So just provide the Future trait. |
870 | let impl_bound = GenericPredicate::Implemented(TraitRef { | 870 | let impl_bound = GenericPredicate::Implemented(TraitRef { |
871 | trait_: future_trait, | 871 | trait_: future_trait, |
872 | substs: Substs::empty(), | 872 | substs: Substitution::empty(), |
873 | }); | 873 | }); |
874 | Some(vec![impl_bound]) | 874 | Some(vec![impl_bound]) |
875 | } else { | 875 | } else { |
@@ -992,7 +992,7 @@ pub trait TypeWalk { | |||
992 | } | 992 | } |
993 | 993 | ||
994 | /// Substitutes `TyKind::Bound` vars with the given substitution. | 994 | /// Substitutes `TyKind::Bound` vars with the given substitution. |
995 | fn subst_bound_vars(self, substs: &Substs) -> Self | 995 | fn subst_bound_vars(self, substs: &Substitution) -> Self |
996 | where | 996 | where |
997 | Self: Sized, | 997 | Self: Sized, |
998 | { | 998 | { |
@@ -1000,7 +1000,7 @@ pub trait TypeWalk { | |||
1000 | } | 1000 | } |
1001 | 1001 | ||
1002 | /// Substitutes `TyKind::Bound` vars with the given substitution. | 1002 | /// Substitutes `TyKind::Bound` vars with the given substitution. |
1003 | fn subst_bound_vars_at_depth(mut self, substs: &Substs, depth: DebruijnIndex) -> Self | 1003 | fn subst_bound_vars_at_depth(mut self, substs: &Substitution, depth: DebruijnIndex) -> Self |
1004 | where | 1004 | where |
1005 | Self: Sized, | 1005 | Self: Sized, |
1006 | { | 1006 | { |