diff options
Diffstat (limited to 'crates/hir_ty/src/lib.rs')
-rw-r--r-- | crates/hir_ty/src/lib.rs | 276 |
1 files changed, 155 insertions, 121 deletions
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index e77f24e4e..2309db492 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs | |||
@@ -49,7 +49,7 @@ pub use traits::{InEnvironment, Obligation, ProjectionPredicate, TraitEnvironmen | |||
49 | 49 | ||
50 | pub use chalk_ir::{AdtId, BoundVar, DebruijnIndex, Mutability, Scalar, TyVariableKind}; | 50 | pub use chalk_ir::{AdtId, BoundVar, DebruijnIndex, Mutability, Scalar, TyVariableKind}; |
51 | 51 | ||
52 | pub(crate) use crate::traits::chalk::Interner; | 52 | pub use crate::traits::chalk::Interner; |
53 | 53 | ||
54 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 54 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
55 | pub enum Lifetime { | 55 | pub enum Lifetime { |
@@ -131,7 +131,7 @@ pub enum AliasTy { | |||
131 | /// | 131 | /// |
132 | /// This should be cheap to clone. | 132 | /// This should be cheap to clone. |
133 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 133 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
134 | pub enum Ty { | 134 | pub enum TyKind { |
135 | /// Structures, enumerations and unions. | 135 | /// Structures, enumerations and unions. |
136 | Adt(AdtId<Interner>, Substs), | 136 | Adt(AdtId<Interner>, Substs), |
137 | 137 | ||
@@ -244,6 +244,21 @@ pub enum Ty { | |||
244 | Unknown, | 244 | Unknown, |
245 | } | 245 | } |
246 | 246 | ||
247 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | ||
248 | pub struct Ty(TyKind); | ||
249 | |||
250 | impl TyKind { | ||
251 | pub fn intern(self, _interner: &Interner) -> Ty { | ||
252 | Ty(self) | ||
253 | } | ||
254 | } | ||
255 | |||
256 | impl Ty { | ||
257 | pub fn interned(&self, _interner: &Interner) -> &TyKind { | ||
258 | &self.0 | ||
259 | } | ||
260 | } | ||
261 | |||
247 | /// A list of substitutions for generic parameters. | 262 | /// A list of substitutions for generic parameters. |
248 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] | 263 | #[derive(Clone, PartialEq, Eq, Debug, Hash)] |
249 | pub struct Substs(Arc<[Ty]>); | 264 | pub struct Substs(Arc<[Ty]>); |
@@ -292,7 +307,12 @@ impl Substs { | |||
292 | 307 | ||
293 | /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). | 308 | /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). |
294 | pub(crate) fn type_params_for_generics(generic_params: &Generics) -> Substs { | 309 | pub(crate) fn type_params_for_generics(generic_params: &Generics) -> Substs { |
295 | Substs(generic_params.iter().map(|(id, _)| Ty::Placeholder(id)).collect()) | 310 | Substs( |
311 | generic_params | ||
312 | .iter() | ||
313 | .map(|(id, _)| TyKind::Placeholder(id).intern(&Interner)) | ||
314 | .collect(), | ||
315 | ) | ||
296 | } | 316 | } |
297 | 317 | ||
298 | /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). | 318 | /// Return Substs that replace each parameter by itself (i.e. `Ty::Param`). |
@@ -307,7 +327,7 @@ impl Substs { | |||
307 | generic_params | 327 | generic_params |
308 | .iter() | 328 | .iter() |
309 | .enumerate() | 329 | .enumerate() |
310 | .map(|(idx, _)| Ty::BoundVar(BoundVar::new(debruijn, idx))) | 330 | .map(|(idx, _)| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)) |
311 | .collect(), | 331 | .collect(), |
312 | ) | 332 | ) |
313 | } | 333 | } |
@@ -355,11 +375,14 @@ impl SubstsBuilder { | |||
355 | } | 375 | } |
356 | 376 | ||
357 | pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self { | 377 | pub fn fill_with_bound_vars(self, debruijn: DebruijnIndex, starting_from: usize) -> Self { |
358 | self.fill((starting_from..).map(|idx| Ty::BoundVar(BoundVar::new(debruijn, idx)))) | 378 | self.fill( |
379 | (starting_from..) | ||
380 | .map(|idx| TyKind::BoundVar(BoundVar::new(debruijn, idx)).intern(&Interner)), | ||
381 | ) | ||
359 | } | 382 | } |
360 | 383 | ||
361 | pub fn fill_with_unknown(self) -> Self { | 384 | pub fn fill_with_unknown(self) -> Self { |
362 | self.fill(iter::repeat(Ty::Unknown)) | 385 | self.fill(iter::repeat(TyKind::Unknown.intern(&Interner))) |
363 | } | 386 | } |
364 | 387 | ||
365 | pub fn fill(mut self, filler: impl Iterator<Item = Ty>) -> Self { | 388 | pub fn fill(mut self, filler: impl Iterator<Item = Ty>) -> Self { |
@@ -601,45 +624,52 @@ impl TypeWalk for CallableSig { | |||
601 | 624 | ||
602 | impl Ty { | 625 | impl Ty { |
603 | pub fn unit() -> Self { | 626 | pub fn unit() -> Self { |
604 | Ty::Tuple(0, Substs::empty()) | 627 | TyKind::Tuple(0, Substs::empty()).intern(&Interner) |
605 | } | 628 | } |
606 | 629 | ||
607 | pub fn adt_ty(adt: hir_def::AdtId, substs: Substs) -> Ty { | 630 | pub fn adt_ty(adt: hir_def::AdtId, substs: Substs) -> Ty { |
608 | Ty::Adt(AdtId(adt), substs) | 631 | TyKind::Adt(AdtId(adt), substs).intern(&Interner) |
609 | } | 632 | } |
610 | 633 | ||
611 | pub fn fn_ptr(sig: CallableSig) -> Self { | 634 | pub fn fn_ptr(sig: CallableSig) -> Self { |
612 | Ty::Function(FnPointer { | 635 | TyKind::Function(FnPointer { |
613 | num_args: sig.params().len(), | 636 | num_args: sig.params().len(), |
614 | sig: FnSig { variadic: sig.is_varargs }, | 637 | sig: FnSig { variadic: sig.is_varargs }, |
615 | substs: Substs(sig.params_and_return), | 638 | substs: Substs(sig.params_and_return), |
616 | }) | 639 | }) |
640 | .intern(&Interner) | ||
617 | } | 641 | } |
618 | 642 | ||
619 | pub fn builtin(builtin: BuiltinType) -> Self { | 643 | pub fn builtin(builtin: BuiltinType) -> Self { |
620 | match builtin { | 644 | match builtin { |
621 | BuiltinType::Char => Ty::Scalar(Scalar::Char), | 645 | BuiltinType::Char => TyKind::Scalar(Scalar::Char).intern(&Interner), |
622 | BuiltinType::Bool => Ty::Scalar(Scalar::Bool), | 646 | BuiltinType::Bool => TyKind::Scalar(Scalar::Bool).intern(&Interner), |
623 | BuiltinType::Str => Ty::Str, | 647 | BuiltinType::Str => TyKind::Str.intern(&Interner), |
624 | BuiltinType::Int(t) => Ty::Scalar(Scalar::Int(primitive::int_ty_from_builtin(t))), | 648 | BuiltinType::Int(t) => { |
625 | BuiltinType::Uint(t) => Ty::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(t))), | 649 | TyKind::Scalar(Scalar::Int(primitive::int_ty_from_builtin(t))).intern(&Interner) |
626 | BuiltinType::Float(t) => Ty::Scalar(Scalar::Float(primitive::float_ty_from_builtin(t))), | 650 | } |
651 | BuiltinType::Uint(t) => { | ||
652 | TyKind::Scalar(Scalar::Uint(primitive::uint_ty_from_builtin(t))).intern(&Interner) | ||
653 | } | ||
654 | BuiltinType::Float(t) => { | ||
655 | TyKind::Scalar(Scalar::Float(primitive::float_ty_from_builtin(t))).intern(&Interner) | ||
656 | } | ||
627 | } | 657 | } |
628 | } | 658 | } |
629 | 659 | ||
630 | pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { | 660 | pub fn as_reference(&self) -> Option<(&Ty, Mutability)> { |
631 | match self { | 661 | match self.interned(&Interner) { |
632 | Ty::Ref(mutability, parameters) => Some((parameters.as_single(), *mutability)), | 662 | TyKind::Ref(mutability, parameters) => Some((parameters.as_single(), *mutability)), |
633 | _ => None, | 663 | _ => None, |
634 | } | 664 | } |
635 | } | 665 | } |
636 | 666 | ||
637 | pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> { | 667 | pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> { |
638 | match self { | 668 | match self.interned(&Interner) { |
639 | Ty::Ref(mutability, parameters) => { | 669 | TyKind::Ref(mutability, parameters) => { |
640 | Some((parameters.as_single(), Rawness::Ref, *mutability)) | 670 | Some((parameters.as_single(), Rawness::Ref, *mutability)) |
641 | } | 671 | } |
642 | Ty::Raw(mutability, parameters) => { | 672 | TyKind::Raw(mutability, parameters) => { |
643 | Some((parameters.as_single(), Rawness::RawPtr, *mutability)) | 673 | Some((parameters.as_single(), Rawness::RawPtr, *mutability)) |
644 | } | 674 | } |
645 | _ => None, | 675 | _ => None, |
@@ -649,7 +679,7 @@ impl Ty { | |||
649 | pub fn strip_references(&self) -> &Ty { | 679 | pub fn strip_references(&self) -> &Ty { |
650 | let mut t: &Ty = self; | 680 | let mut t: &Ty = self; |
651 | 681 | ||
652 | while let Ty::Ref(_mutability, parameters) = t { | 682 | while let TyKind::Ref(_mutability, parameters) = t.interned(&Interner) { |
653 | t = parameters.as_single(); | 683 | t = parameters.as_single(); |
654 | } | 684 | } |
655 | 685 | ||
@@ -657,65 +687,69 @@ impl Ty { | |||
657 | } | 687 | } |
658 | 688 | ||
659 | pub fn as_adt(&self) -> Option<(hir_def::AdtId, &Substs)> { | 689 | pub fn as_adt(&self) -> Option<(hir_def::AdtId, &Substs)> { |
660 | match self { | 690 | match self.interned(&Interner) { |
661 | Ty::Adt(AdtId(adt), parameters) => Some((*adt, parameters)), | 691 | TyKind::Adt(AdtId(adt), parameters) => Some((*adt, parameters)), |
662 | _ => None, | 692 | _ => None, |
663 | } | 693 | } |
664 | } | 694 | } |
665 | 695 | ||
666 | pub fn as_tuple(&self) -> Option<&Substs> { | 696 | pub fn as_tuple(&self) -> Option<&Substs> { |
667 | match self { | 697 | match self.interned(&Interner) { |
668 | Ty::Tuple(_, substs) => Some(substs), | 698 | TyKind::Tuple(_, substs) => Some(substs), |
669 | _ => None, | 699 | _ => None, |
670 | } | 700 | } |
671 | } | 701 | } |
672 | 702 | ||
673 | pub fn as_generic_def(&self) -> Option<GenericDefId> { | 703 | pub fn as_generic_def(&self) -> Option<GenericDefId> { |
674 | match *self { | 704 | match *self.interned(&Interner) { |
675 | Ty::Adt(AdtId(adt), ..) => Some(adt.into()), | 705 | TyKind::Adt(AdtId(adt), ..) => Some(adt.into()), |
676 | Ty::FnDef(callable, ..) => Some(callable.into()), | 706 | TyKind::FnDef(callable, ..) => Some(callable.into()), |
677 | Ty::AssociatedType(type_alias, ..) => Some(type_alias.into()), | 707 | TyKind::AssociatedType(type_alias, ..) => Some(type_alias.into()), |
678 | Ty::ForeignType(type_alias, ..) => Some(type_alias.into()), | 708 | TyKind::ForeignType(type_alias, ..) => Some(type_alias.into()), |
679 | _ => None, | 709 | _ => None, |
680 | } | 710 | } |
681 | } | 711 | } |
682 | 712 | ||
683 | pub fn is_never(&self) -> bool { | 713 | pub fn is_never(&self) -> bool { |
684 | matches!(self, Ty::Never) | 714 | matches!(self.interned(&Interner), TyKind::Never) |
685 | } | 715 | } |
686 | 716 | ||
687 | pub fn is_unknown(&self) -> bool { | 717 | pub fn is_unknown(&self) -> bool { |
688 | matches!(self, Ty::Unknown) | 718 | matches!(self.interned(&Interner), TyKind::Unknown) |
689 | } | 719 | } |
690 | 720 | ||
691 | pub fn equals_ctor(&self, other: &Ty) -> bool { | 721 | pub fn equals_ctor(&self, other: &Ty) -> bool { |
692 | match (self, other) { | 722 | match (self.interned(&Interner), other.interned(&Interner)) { |
693 | (Ty::Adt(adt, ..), Ty::Adt(adt2, ..)) => adt == adt2, | 723 | (TyKind::Adt(adt, ..), TyKind::Adt(adt2, ..)) => adt == adt2, |
694 | (Ty::Slice(_), Ty::Slice(_)) | (Ty::Array(_), Ty::Array(_)) => true, | 724 | (TyKind::Slice(_), TyKind::Slice(_)) | (TyKind::Array(_), TyKind::Array(_)) => true, |
695 | (Ty::FnDef(def_id, ..), Ty::FnDef(def_id2, ..)) => def_id == def_id2, | 725 | (TyKind::FnDef(def_id, ..), TyKind::FnDef(def_id2, ..)) => def_id == def_id2, |
696 | (Ty::OpaqueType(ty_id, ..), Ty::OpaqueType(ty_id2, ..)) => ty_id == ty_id2, | 726 | (TyKind::OpaqueType(ty_id, ..), TyKind::OpaqueType(ty_id2, ..)) => ty_id == ty_id2, |
697 | (Ty::AssociatedType(ty_id, ..), Ty::AssociatedType(ty_id2, ..)) | 727 | (TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..)) |
698 | | (Ty::ForeignType(ty_id, ..), Ty::ForeignType(ty_id2, ..)) => ty_id == ty_id2, | 728 | | (TyKind::ForeignType(ty_id, ..), TyKind::ForeignType(ty_id2, ..)) => ty_id == ty_id2, |
699 | (Ty::Closure(def, expr, _), Ty::Closure(def2, expr2, _)) => { | 729 | (TyKind::Closure(def, expr, _), TyKind::Closure(def2, expr2, _)) => { |
700 | expr == expr2 && def == def2 | 730 | expr == expr2 && def == def2 |
701 | } | 731 | } |
702 | (Ty::Ref(mutability, ..), Ty::Ref(mutability2, ..)) | 732 | (TyKind::Ref(mutability, ..), TyKind::Ref(mutability2, ..)) |
703 | | (Ty::Raw(mutability, ..), Ty::Raw(mutability2, ..)) => mutability == mutability2, | 733 | | (TyKind::Raw(mutability, ..), TyKind::Raw(mutability2, ..)) => { |
734 | mutability == mutability2 | ||
735 | } | ||
704 | ( | 736 | ( |
705 | Ty::Function(FnPointer { num_args, sig, .. }), | 737 | TyKind::Function(FnPointer { num_args, sig, .. }), |
706 | Ty::Function(FnPointer { num_args: num_args2, sig: sig2, .. }), | 738 | TyKind::Function(FnPointer { num_args: num_args2, sig: sig2, .. }), |
707 | ) => num_args == num_args2 && sig == sig2, | 739 | ) => num_args == num_args2 && sig == sig2, |
708 | (Ty::Tuple(cardinality, _), Ty::Tuple(cardinality2, _)) => cardinality == cardinality2, | 740 | (TyKind::Tuple(cardinality, _), TyKind::Tuple(cardinality2, _)) => { |
709 | (Ty::Str, Ty::Str) | (Ty::Never, Ty::Never) => true, | 741 | cardinality == cardinality2 |
710 | (Ty::Scalar(scalar), Ty::Scalar(scalar2)) => scalar == scalar2, | 742 | } |
743 | (TyKind::Str, TyKind::Str) | (TyKind::Never, TyKind::Never) => true, | ||
744 | (TyKind::Scalar(scalar), TyKind::Scalar(scalar2)) => scalar == scalar2, | ||
711 | _ => false, | 745 | _ => false, |
712 | } | 746 | } |
713 | } | 747 | } |
714 | 748 | ||
715 | /// If this is a `dyn Trait` type, this returns the `Trait` part. | 749 | /// If this is a `dyn Trait` type, this returns the `Trait` part. |
716 | pub fn dyn_trait_ref(&self) -> Option<&TraitRef> { | 750 | pub fn dyn_trait_ref(&self) -> Option<&TraitRef> { |
717 | match self { | 751 | match self.interned(&Interner) { |
718 | Ty::Dyn(bounds) => bounds.get(0).and_then(|b| match b { | 752 | TyKind::Dyn(bounds) => bounds.get(0).and_then(|b| match b { |
719 | GenericPredicate::Implemented(trait_ref) => Some(trait_ref), | 753 | GenericPredicate::Implemented(trait_ref) => Some(trait_ref), |
720 | _ => None, | 754 | _ => None, |
721 | }), | 755 | }), |
@@ -729,28 +763,28 @@ impl Ty { | |||
729 | } | 763 | } |
730 | 764 | ||
731 | fn builtin_deref(&self) -> Option<Ty> { | 765 | fn builtin_deref(&self) -> Option<Ty> { |
732 | match self { | 766 | match self.interned(&Interner) { |
733 | Ty::Ref(.., parameters) => Some(Ty::clone(parameters.as_single())), | 767 | TyKind::Ref(.., parameters) => Some(Ty::clone(parameters.as_single())), |
734 | Ty::Raw(.., parameters) => Some(Ty::clone(parameters.as_single())), | 768 | TyKind::Raw(.., parameters) => Some(Ty::clone(parameters.as_single())), |
735 | _ => None, | 769 | _ => None, |
736 | } | 770 | } |
737 | } | 771 | } |
738 | 772 | ||
739 | pub fn as_fn_def(&self) -> Option<FunctionId> { | 773 | pub fn as_fn_def(&self) -> Option<FunctionId> { |
740 | match self { | 774 | match self.interned(&Interner) { |
741 | &Ty::FnDef(CallableDefId::FunctionId(func), ..) => Some(func), | 775 | &TyKind::FnDef(CallableDefId::FunctionId(func), ..) => Some(func), |
742 | _ => None, | 776 | _ => None, |
743 | } | 777 | } |
744 | } | 778 | } |
745 | 779 | ||
746 | pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig> { | 780 | pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig> { |
747 | match self { | 781 | match self.interned(&Interner) { |
748 | Ty::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)), | 782 | TyKind::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)), |
749 | Ty::FnDef(def, parameters) => { | 783 | TyKind::FnDef(def, parameters) => { |
750 | let sig = db.callable_item_signature(*def); | 784 | let sig = db.callable_item_signature(*def); |
751 | Some(sig.subst(¶meters)) | 785 | Some(sig.subst(¶meters)) |
752 | } | 786 | } |
753 | Ty::Closure(.., substs) => { | 787 | TyKind::Closure(.., substs) => { |
754 | let sig_param = &substs[0]; | 788 | let sig_param = &substs[0]; |
755 | sig_param.callable_sig(db) | 789 | sig_param.callable_sig(db) |
756 | } | 790 | } |
@@ -763,18 +797,18 @@ impl Ty { | |||
763 | /// `self` is `Option<_>` and the substs contain `u32`, we'll have | 797 | /// `self` is `Option<_>` and the substs contain `u32`, we'll have |
764 | /// `Option<u32>` afterwards.) | 798 | /// `Option<u32>` afterwards.) |
765 | pub fn apply_substs(mut self, new_substs: Substs) -> Ty { | 799 | pub fn apply_substs(mut self, new_substs: Substs) -> Ty { |
766 | match &mut self { | 800 | match &mut self.0 { |
767 | Ty::Adt(_, substs) | 801 | TyKind::Adt(_, substs) |
768 | | Ty::Slice(substs) | 802 | | TyKind::Slice(substs) |
769 | | Ty::Array(substs) | 803 | | TyKind::Array(substs) |
770 | | Ty::Raw(_, substs) | 804 | | TyKind::Raw(_, substs) |
771 | | Ty::Ref(_, substs) | 805 | | TyKind::Ref(_, substs) |
772 | | Ty::FnDef(_, substs) | 806 | | TyKind::FnDef(_, substs) |
773 | | Ty::Function(FnPointer { substs, .. }) | 807 | | TyKind::Function(FnPointer { substs, .. }) |
774 | | Ty::Tuple(_, substs) | 808 | | TyKind::Tuple(_, substs) |
775 | | Ty::OpaqueType(_, substs) | 809 | | TyKind::OpaqueType(_, substs) |
776 | | Ty::AssociatedType(_, substs) | 810 | | TyKind::AssociatedType(_, substs) |
777 | | Ty::Closure(.., substs) => { | 811 | | TyKind::Closure(.., substs) => { |
778 | assert_eq!(substs.len(), new_substs.len()); | 812 | assert_eq!(substs.len(), new_substs.len()); |
779 | *substs = new_substs; | 813 | *substs = new_substs; |
780 | } | 814 | } |
@@ -786,42 +820,42 @@ impl Ty { | |||
786 | /// Returns the type parameters of this type if it has some (i.e. is an ADT | 820 | /// Returns the type parameters of this type if it has some (i.e. is an ADT |
787 | /// or function); so if `self` is `Option<u32>`, this returns the `u32`. | 821 | /// or function); so if `self` is `Option<u32>`, this returns the `u32`. |
788 | pub fn substs(&self) -> Option<&Substs> { | 822 | pub fn substs(&self) -> Option<&Substs> { |
789 | match self { | 823 | match self.interned(&Interner) { |
790 | Ty::Adt(_, substs) | 824 | TyKind::Adt(_, substs) |
791 | | Ty::Slice(substs) | 825 | | TyKind::Slice(substs) |
792 | | Ty::Array(substs) | 826 | | TyKind::Array(substs) |
793 | | Ty::Raw(_, substs) | 827 | | TyKind::Raw(_, substs) |
794 | | Ty::Ref(_, substs) | 828 | | TyKind::Ref(_, substs) |
795 | | Ty::FnDef(_, substs) | 829 | | TyKind::FnDef(_, substs) |
796 | | Ty::Function(FnPointer { substs, .. }) | 830 | | TyKind::Function(FnPointer { substs, .. }) |
797 | | Ty::Tuple(_, substs) | 831 | | TyKind::Tuple(_, substs) |
798 | | Ty::OpaqueType(_, substs) | 832 | | TyKind::OpaqueType(_, substs) |
799 | | Ty::AssociatedType(_, substs) | 833 | | TyKind::AssociatedType(_, substs) |
800 | | Ty::Closure(.., substs) => Some(substs), | 834 | | TyKind::Closure(.., substs) => Some(substs), |
801 | _ => None, | 835 | _ => None, |
802 | } | 836 | } |
803 | } | 837 | } |
804 | 838 | ||
805 | pub fn substs_mut(&mut self) -> Option<&mut Substs> { | 839 | pub fn substs_mut(&mut self) -> Option<&mut Substs> { |
806 | match self { | 840 | match &mut self.0 { |
807 | Ty::Adt(_, substs) | 841 | TyKind::Adt(_, substs) |
808 | | Ty::Slice(substs) | 842 | | TyKind::Slice(substs) |
809 | | Ty::Array(substs) | 843 | | TyKind::Array(substs) |
810 | | Ty::Raw(_, substs) | 844 | | TyKind::Raw(_, substs) |
811 | | Ty::Ref(_, substs) | 845 | | TyKind::Ref(_, substs) |
812 | | Ty::FnDef(_, substs) | 846 | | TyKind::FnDef(_, substs) |
813 | | Ty::Function(FnPointer { substs, .. }) | 847 | | TyKind::Function(FnPointer { substs, .. }) |
814 | | Ty::Tuple(_, substs) | 848 | | TyKind::Tuple(_, substs) |
815 | | Ty::OpaqueType(_, substs) | 849 | | TyKind::OpaqueType(_, substs) |
816 | | Ty::AssociatedType(_, substs) | 850 | | TyKind::AssociatedType(_, substs) |
817 | | Ty::Closure(.., substs) => Some(substs), | 851 | | TyKind::Closure(.., substs) => Some(substs), |
818 | _ => None, | 852 | _ => None, |
819 | } | 853 | } |
820 | } | 854 | } |
821 | 855 | ||
822 | pub fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<GenericPredicate>> { | 856 | pub fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<GenericPredicate>> { |
823 | match self { | 857 | match self.interned(&Interner) { |
824 | Ty::OpaqueType(opaque_ty_id, ..) => { | 858 | TyKind::OpaqueType(opaque_ty_id, ..) => { |
825 | match opaque_ty_id { | 859 | match opaque_ty_id { |
826 | OpaqueTyId::AsyncBlockTypeImplTrait(def, _expr) => { | 860 | OpaqueTyId::AsyncBlockTypeImplTrait(def, _expr) => { |
827 | let krate = def.module(db.upcast()).krate(); | 861 | let krate = def.module(db.upcast()).krate(); |
@@ -844,7 +878,7 @@ impl Ty { | |||
844 | OpaqueTyId::ReturnTypeImplTrait(..) => None, | 878 | OpaqueTyId::ReturnTypeImplTrait(..) => None, |
845 | } | 879 | } |
846 | } | 880 | } |
847 | Ty::Alias(AliasTy::Opaque(opaque_ty)) => { | 881 | TyKind::Alias(AliasTy::Opaque(opaque_ty)) => { |
848 | let predicates = match opaque_ty.opaque_ty_id { | 882 | let predicates = match opaque_ty.opaque_ty_id { |
849 | OpaqueTyId::ReturnTypeImplTrait(func, idx) => { | 883 | OpaqueTyId::ReturnTypeImplTrait(func, idx) => { |
850 | db.return_type_impl_traits(func).map(|it| { | 884 | db.return_type_impl_traits(func).map(|it| { |
@@ -860,7 +894,7 @@ impl Ty { | |||
860 | 894 | ||
861 | predicates.map(|it| it.value) | 895 | predicates.map(|it| it.value) |
862 | } | 896 | } |
863 | Ty::Placeholder(id) => { | 897 | TyKind::Placeholder(id) => { |
864 | let generic_params = db.generic_params(id.parent); | 898 | let generic_params = db.generic_params(id.parent); |
865 | let param_data = &generic_params.types[id.local_id]; | 899 | let param_data = &generic_params.types[id.local_id]; |
866 | match param_data.provenance { | 900 | match param_data.provenance { |
@@ -881,14 +915,14 @@ impl Ty { | |||
881 | } | 915 | } |
882 | 916 | ||
883 | pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> { | 917 | pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> { |
884 | match self { | 918 | match self.interned(&Interner) { |
885 | Ty::AssociatedType(type_alias_id, ..) => { | 919 | TyKind::AssociatedType(type_alias_id, ..) => { |
886 | match type_alias_id.lookup(db.upcast()).container { | 920 | match type_alias_id.lookup(db.upcast()).container { |
887 | AssocContainerId::TraitId(trait_id) => Some(trait_id), | 921 | AssocContainerId::TraitId(trait_id) => Some(trait_id), |
888 | _ => None, | 922 | _ => None, |
889 | } | 923 | } |
890 | } | 924 | } |
891 | Ty::Alias(AliasTy::Projection(projection_ty)) => { | 925 | TyKind::Alias(AliasTy::Projection(projection_ty)) => { |
892 | match projection_ty.associated_ty.lookup(db.upcast()).container { | 926 | match projection_ty.associated_ty.lookup(db.upcast()).container { |
893 | AssocContainerId::TraitId(trait_id) => Some(trait_id), | 927 | AssocContainerId::TraitId(trait_id) => Some(trait_id), |
894 | _ => None, | 928 | _ => None, |
@@ -908,13 +942,13 @@ pub trait TypeWalk { | |||
908 | } | 942 | } |
909 | /// Walk the type, counting entered binders. | 943 | /// Walk the type, counting entered binders. |
910 | /// | 944 | /// |
911 | /// `Ty::Bound` variables use DeBruijn indexing, which means that 0 refers | 945 | /// `TyKind::Bound` variables use DeBruijn indexing, which means that 0 refers |
912 | /// to the innermost binder, 1 to the next, etc.. So when we want to | 946 | /// to the innermost binder, 1 to the next, etc.. So when we want to |
913 | /// substitute a certain bound variable, we can't just walk the whole type | 947 | /// substitute a certain bound variable, we can't just walk the whole type |
914 | /// and blindly replace each instance of a certain index; when we 'enter' | 948 | /// and blindly replace each instance of a certain index; when we 'enter' |
915 | /// things that introduce new bound variables, we have to keep track of | 949 | /// things that introduce new bound variables, we have to keep track of |
916 | /// that. Currently, the only thing that introduces bound variables on our | 950 | /// that. Currently, the only thing that introduces bound variables on our |
917 | /// side are `Ty::Dyn` and `Ty::Opaque`, which each introduce a bound | 951 | /// side are `TyKind::Dyn` and `TyKind::Opaque`, which each introduce a bound |
918 | /// variable for the self type. | 952 | /// variable for the self type. |
919 | fn walk_mut_binders( | 953 | fn walk_mut_binders( |
920 | &mut self, | 954 | &mut self, |
@@ -932,7 +966,7 @@ pub trait TypeWalk { | |||
932 | { | 966 | { |
933 | self.walk_mut_binders( | 967 | self.walk_mut_binders( |
934 | &mut |ty_mut, binders| { | 968 | &mut |ty_mut, binders| { |
935 | let ty = mem::replace(ty_mut, Ty::Unknown); | 969 | let ty = mem::replace(ty_mut, Ty(TyKind::Unknown)); |
936 | *ty_mut = f(ty, binders); | 970 | *ty_mut = f(ty, binders); |
937 | }, | 971 | }, |
938 | binders, | 972 | binders, |
@@ -945,13 +979,13 @@ pub trait TypeWalk { | |||
945 | Self: Sized, | 979 | Self: Sized, |
946 | { | 980 | { |
947 | self.walk_mut(&mut |ty_mut| { | 981 | self.walk_mut(&mut |ty_mut| { |
948 | let ty = mem::replace(ty_mut, Ty::Unknown); | 982 | let ty = mem::replace(ty_mut, Ty(TyKind::Unknown)); |
949 | *ty_mut = f(ty); | 983 | *ty_mut = f(ty); |
950 | }); | 984 | }); |
951 | self | 985 | self |
952 | } | 986 | } |
953 | 987 | ||
954 | /// Substitutes `Ty::Bound` vars with the given substitution. | 988 | /// Substitutes `TyKind::Bound` vars with the given substitution. |
955 | fn subst_bound_vars(self, substs: &Substs) -> Self | 989 | fn subst_bound_vars(self, substs: &Substs) -> Self |
956 | where | 990 | where |
957 | Self: Sized, | 991 | Self: Sized, |
@@ -959,14 +993,14 @@ pub trait TypeWalk { | |||
959 | self.subst_bound_vars_at_depth(substs, DebruijnIndex::INNERMOST) | 993 | self.subst_bound_vars_at_depth(substs, DebruijnIndex::INNERMOST) |
960 | } | 994 | } |
961 | 995 | ||
962 | /// Substitutes `Ty::Bound` vars with the given substitution. | 996 | /// Substitutes `TyKind::Bound` vars with the given substitution. |
963 | fn subst_bound_vars_at_depth(mut self, substs: &Substs, depth: DebruijnIndex) -> Self | 997 | fn subst_bound_vars_at_depth(mut self, substs: &Substs, depth: DebruijnIndex) -> Self |
964 | where | 998 | where |
965 | Self: Sized, | 999 | Self: Sized, |
966 | { | 1000 | { |
967 | self.walk_mut_binders( | 1001 | self.walk_mut_binders( |
968 | &mut |ty, binders| { | 1002 | &mut |ty, binders| { |
969 | if let &mut Ty::BoundVar(bound) = ty { | 1003 | if let &mut TyKind::BoundVar(bound) = &mut ty.0 { |
970 | if bound.debruijn >= binders { | 1004 | if bound.debruijn >= binders { |
971 | *ty = substs.0[bound.index].clone().shift_bound_vars(binders); | 1005 | *ty = substs.0[bound.index].clone().shift_bound_vars(binders); |
972 | } | 1006 | } |
@@ -977,17 +1011,17 @@ pub trait TypeWalk { | |||
977 | self | 1011 | self |
978 | } | 1012 | } |
979 | 1013 | ||
980 | /// Shifts up debruijn indices of `Ty::Bound` vars by `n`. | 1014 | /// Shifts up debruijn indices of `TyKind::Bound` vars by `n`. |
981 | fn shift_bound_vars(self, n: DebruijnIndex) -> Self | 1015 | fn shift_bound_vars(self, n: DebruijnIndex) -> Self |
982 | where | 1016 | where |
983 | Self: Sized, | 1017 | Self: Sized, |
984 | { | 1018 | { |
985 | self.fold_binders( | 1019 | self.fold_binders( |
986 | &mut |ty, binders| match ty { | 1020 | &mut |ty, binders| match &ty.0 { |
987 | Ty::BoundVar(bound) if bound.debruijn >= binders => { | 1021 | TyKind::BoundVar(bound) if bound.debruijn >= binders => { |
988 | Ty::BoundVar(bound.shifted_in_from(n)) | 1022 | TyKind::BoundVar(bound.shifted_in_from(n)).intern(&Interner) |
989 | } | 1023 | } |
990 | ty => ty, | 1024 | _ => ty, |
991 | }, | 1025 | }, |
992 | DebruijnIndex::INNERMOST, | 1026 | DebruijnIndex::INNERMOST, |
993 | ) | 1027 | ) |
@@ -996,18 +1030,18 @@ pub trait TypeWalk { | |||
996 | 1030 | ||
997 | impl TypeWalk for Ty { | 1031 | impl TypeWalk for Ty { |
998 | fn walk(&self, f: &mut impl FnMut(&Ty)) { | 1032 | fn walk(&self, f: &mut impl FnMut(&Ty)) { |
999 | match self { | 1033 | match self.interned(&Interner) { |
1000 | Ty::Alias(AliasTy::Projection(p_ty)) => { | 1034 | TyKind::Alias(AliasTy::Projection(p_ty)) => { |
1001 | for t in p_ty.parameters.iter() { | 1035 | for t in p_ty.parameters.iter() { |
1002 | t.walk(f); | 1036 | t.walk(f); |
1003 | } | 1037 | } |
1004 | } | 1038 | } |
1005 | Ty::Alias(AliasTy::Opaque(o_ty)) => { | 1039 | TyKind::Alias(AliasTy::Opaque(o_ty)) => { |
1006 | for t in o_ty.parameters.iter() { | 1040 | for t in o_ty.parameters.iter() { |
1007 | t.walk(f); | 1041 | t.walk(f); |
1008 | } | 1042 | } |
1009 | } | 1043 | } |
1010 | Ty::Dyn(predicates) => { | 1044 | TyKind::Dyn(predicates) => { |
1011 | for p in predicates.iter() { | 1045 | for p in predicates.iter() { |
1012 | p.walk(f); | 1046 | p.walk(f); |
1013 | } | 1047 | } |
@@ -1028,16 +1062,16 @@ impl TypeWalk for Ty { | |||
1028 | f: &mut impl FnMut(&mut Ty, DebruijnIndex), | 1062 | f: &mut impl FnMut(&mut Ty, DebruijnIndex), |
1029 | binders: DebruijnIndex, | 1063 | binders: DebruijnIndex, |
1030 | ) { | 1064 | ) { |
1031 | match self { | 1065 | match &mut self.0 { |
1032 | Ty::Alias(AliasTy::Projection(p_ty)) => { | 1066 | TyKind::Alias(AliasTy::Projection(p_ty)) => { |
1033 | p_ty.parameters.walk_mut_binders(f, binders); | 1067 | p_ty.parameters.walk_mut_binders(f, binders); |
1034 | } | 1068 | } |
1035 | Ty::Dyn(predicates) => { | 1069 | TyKind::Dyn(predicates) => { |
1036 | for p in make_mut_slice(predicates) { | 1070 | for p in make_mut_slice(predicates) { |
1037 | p.walk_mut_binders(f, binders.shifted_in()); | 1071 | p.walk_mut_binders(f, binders.shifted_in()); |
1038 | } | 1072 | } |
1039 | } | 1073 | } |
1040 | Ty::Alias(AliasTy::Opaque(o_ty)) => { | 1074 | TyKind::Alias(AliasTy::Opaque(o_ty)) => { |
1041 | o_ty.parameters.walk_mut_binders(f, binders); | 1075 | o_ty.parameters.walk_mut_binders(f, binders); |
1042 | } | 1076 | } |
1043 | _ => { | 1077 | _ => { |