diff options
Diffstat (limited to 'crates/hir_ty/src/lib.rs')
-rw-r--r-- | crates/hir_ty/src/lib.rs | 66 |
1 files changed, 16 insertions, 50 deletions
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 27ebb7b7c..f99b70f2b 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs | |||
@@ -492,10 +492,6 @@ impl Substitution { | |||
492 | .map(|(idx, _)| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)), | 492 | .map(|(idx, _)| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)), |
493 | ) | 493 | ) |
494 | } | 494 | } |
495 | |||
496 | fn builder(param_count: usize) -> SubstsBuilder { | ||
497 | SubstsBuilder { vec: Vec::with_capacity(param_count), param_count } | ||
498 | } | ||
499 | } | 495 | } |
500 | 496 | ||
501 | /// Return an index of a parameter in the generic type parameter list by it's id. | 497 | /// Return an index of a parameter in the generic type parameter list by it's id. |
@@ -503,52 +499,6 @@ pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option<usize> { | |||
503 | generics(db.upcast(), id.parent).param_idx(id) | 499 | generics(db.upcast(), id.parent).param_idx(id) |
504 | } | 500 | } |
505 | 501 | ||
506 | #[derive(Debug, Clone)] | ||
507 | pub struct SubstsBuilder { | ||
508 | vec: Vec<GenericArg>, | ||
509 | param_count: usize, | ||
510 | } | ||
511 | |||
512 | impl SubstsBuilder { | ||
513 | pub fn build(self) -> Substitution { | ||
514 | assert_eq!(self.vec.len(), self.param_count); | ||
515 | Substitution::from_iter(&Interner, self.vec) | ||
516 | } | ||
517 | |||
518 | pub fn push(mut self, ty: impl CastTo<GenericArg>) -> Self { | ||
519 | self.vec.push(ty.cast(&Interner)); | ||
520 | self | ||
521 | } | ||
522 | |||
523 | fn remaining(&self) -> usize { | ||
524 | self.param_count - self.vec.len() | ||
525 | } | ||
526 | |||
527 | pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self { | ||
528 | self.fill( | ||
529 | (starting_from..) | ||
530 | .map(|idx| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)), | ||
531 | ) | ||
532 | } | ||
533 | |||
534 | pub fn fill_with_unknown(self) -> Self { | ||
535 | self.fill(iter::repeat(TyKind::Unknown.intern(&Interner))) | ||
536 | } | ||
537 | |||
538 | pub fn fill(mut self, filler: impl Iterator<Item = impl CastTo<GenericArg>>) -> Self { | ||
539 | self.vec.extend(filler.take(self.remaining()).casted(&Interner)); | ||
540 | assert_eq!(self.remaining(), 0); | ||
541 | self | ||
542 | } | ||
543 | |||
544 | pub fn use_parent_substs(mut self, parent_substs: &Substitution) -> Self { | ||
545 | assert!(self.vec.is_empty()); | ||
546 | assert!(parent_substs.len(&Interner) <= self.param_count); | ||
547 | self.vec.extend(parent_substs.iter(&Interner).cloned()); | ||
548 | self | ||
549 | } | ||
550 | } | ||
551 | |||
552 | #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] | 502 | #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] |
553 | pub struct Binders<T> { | 503 | pub struct Binders<T> { |
554 | pub num_binders: usize, | 504 | pub num_binders: usize, |
@@ -921,6 +871,18 @@ impl TyBuilder<hir_def::AdtId> { | |||
921 | } | 871 | } |
922 | } | 872 | } |
923 | 873 | ||
874 | struct Tuple(usize); | ||
875 | impl TyBuilder<Tuple> { | ||
876 | pub fn tuple(size: usize) -> TyBuilder<Tuple> { | ||
877 | TyBuilder::new(Tuple(size), size) | ||
878 | } | ||
879 | |||
880 | pub fn build(self) -> Ty { | ||
881 | let (Tuple(size), subst) = self.build_internal(); | ||
882 | TyKind::Tuple(size, subst).intern(&Interner) | ||
883 | } | ||
884 | } | ||
885 | |||
924 | impl TyBuilder<TraitId> { | 886 | impl TyBuilder<TraitId> { |
925 | pub fn trait_ref(db: &dyn HirDatabase, trait_id: TraitId) -> TyBuilder<TraitId> { | 887 | pub fn trait_ref(db: &dyn HirDatabase, trait_id: TraitId) -> TyBuilder<TraitId> { |
926 | let generics = generics(db.upcast(), trait_id.into()); | 888 | let generics = generics(db.upcast(), trait_id.into()); |
@@ -970,6 +932,10 @@ impl TyBuilder<Binders<Ty>> { | |||
970 | pub fn impl_self_ty(db: &dyn HirDatabase, def: hir_def::ImplId) -> TyBuilder<Binders<Ty>> { | 932 | pub fn impl_self_ty(db: &dyn HirDatabase, def: hir_def::ImplId) -> TyBuilder<Binders<Ty>> { |
971 | TyBuilder::subst_binders(db.impl_self_ty(def)) | 933 | TyBuilder::subst_binders(db.impl_self_ty(def)) |
972 | } | 934 | } |
935 | |||
936 | pub fn value_ty(db: &dyn HirDatabase, def: ValueTyDefId) -> TyBuilder<Binders<Ty>> { | ||
937 | TyBuilder::subst_binders(db.value_ty(def)) | ||
938 | } | ||
973 | } | 939 | } |
974 | 940 | ||
975 | impl Ty { | 941 | impl Ty { |