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.rs89
1 files changed, 10 insertions, 79 deletions
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index a3addc8e9..f74b22b58 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -31,7 +31,6 @@ mod test_db;
31use std::sync::Arc; 31use std::sync::Arc;
32 32
33use itertools::Itertools; 33use itertools::Itertools;
34use smallvec::SmallVec;
35 34
36use base_db::salsa; 35use base_db::salsa;
37use hir_def::{ 36use hir_def::{
@@ -43,13 +42,13 @@ use crate::{db::HirDatabase, display::HirDisplay, utils::generics};
43 42
44pub use autoderef::autoderef; 43pub use autoderef::autoderef;
45pub use builder::TyBuilder; 44pub use builder::TyBuilder;
46pub use chalk_ext::TyExt; 45pub use chalk_ext::{ProjectionTyExt, TyExt};
47pub use infer::{could_unify, InferenceResult, InferenceVar}; 46pub use infer::{could_unify, InferenceResult, InferenceVar};
48pub use lower::{ 47pub use lower::{
49 associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode, 48 associated_type_shorthand_candidates, callable_item_sig, CallableDefId, ImplTraitLoweringMode,
50 TyDefId, TyLoweringContext, ValueTyDefId, 49 TyDefId, TyLoweringContext, ValueTyDefId,
51}; 50};
52pub use traits::TraitEnvironment; 51pub use traits::{chalk::Interner, TraitEnvironment};
53pub use types::*; 52pub use types::*;
54pub use walk::TypeWalk; 53pub use walk::TypeWalk;
55 54
@@ -57,8 +56,6 @@ pub use chalk_ir::{
57 cast::Cast, AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind, 56 cast::Cast, AdtId, BoundVar, DebruijnIndex, Mutability, Safety, Scalar, TyVariableKind,
58}; 57};
59 58
60pub use crate::traits::chalk::Interner;
61
62pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>; 59pub type ForeignDefId = chalk_ir::ForeignDefId<Interner>;
63pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>; 60pub type AssocTypeId = chalk_ir::AssocTypeId<Interner>;
64pub type FnDefId = chalk_ir::FnDefId<Interner>; 61pub type FnDefId = chalk_ir::FnDefId<Interner>;
@@ -76,46 +73,11 @@ pub type LifetimeOutlives = chalk_ir::LifetimeOutlives<Interner>;
76 73
77pub type ChalkTraitId = chalk_ir::TraitId<Interner>; 74pub type ChalkTraitId = chalk_ir::TraitId<Interner>;
78 75
79impl ProjectionTy {
80 pub fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef {
81 TraitRef {
82 trait_id: to_chalk_trait_id(self.trait_(db)),
83 substitution: self.substitution.clone(),
84 }
85 }
86
87 pub fn self_type_parameter(&self, interner: &Interner) -> &Ty {
88 &self.substitution.interned()[0].assert_ty_ref(interner)
89 }
90
91 fn trait_(&self, db: &dyn HirDatabase) -> TraitId {
92 match from_assoc_type_id(self.associated_ty_id).lookup(db.upcast()).container {
93 AssocContainerId::TraitId(it) => it,
94 _ => panic!("projection ty without parent trait"),
95 }
96 }
97}
98
99pub type FnSig = chalk_ir::FnSig<Interner>; 76pub type FnSig = chalk_ir::FnSig<Interner>;
100 77
101impl Substitution { 78// FIXME: get rid of this
102 pub fn single(ty: Ty) -> Substitution { 79pub fn subst_prefix(s: &Substitution, n: usize) -> Substitution {
103 Substitution::intern({ 80 Substitution::intern(s.interned()[..std::cmp::min(s.len(&Interner), n)].into())
104 let mut v = SmallVec::new();
105 v.push(ty.cast(&Interner));
106 v
107 })
108 }
109
110 pub fn prefix(&self, n: usize) -> Substitution {
111 Substitution::intern(self.interned()[..std::cmp::min(self.len(&Interner), n)].into())
112 }
113
114 pub fn suffix(&self, n: usize) -> Substitution {
115 Substitution::intern(
116 self.interned()[self.len(&Interner) - std::cmp::min(self.len(&Interner), n)..].into(),
117 )
118 }
119} 81}
120 82
121/// Return an index of a parameter in the generic type parameter list by it's id. 83/// Return an index of a parameter in the generic type parameter list by it's id.
@@ -123,22 +85,11 @@ pub fn param_idx(db: &dyn HirDatabase, id: TypeParamId) -> Option<usize> {
123 generics(db.upcast(), id.parent).param_idx(id) 85 generics(db.upcast(), id.parent).param_idx(id)
124} 86}
125 87
126impl<T> Binders<T> { 88pub fn wrap_empty_binders<T>(value: T) -> Binders<T>
127 pub fn wrap_empty(value: T) -> Self 89where
128 where 90 T: TypeWalk,
129 T: TypeWalk, 91{
130 { 92 Binders::empty(&Interner, value.shifted_in_from(DebruijnIndex::ONE))
131 Binders::empty(&Interner, value.shifted_in_from(DebruijnIndex::ONE))
132 }
133}
134
135impl<T: TypeWalk> Binders<T> {
136 /// Substitutes all variables.
137 pub fn substitute(self, interner: &Interner, subst: &Substitution) -> T {
138 let (value, binders) = self.into_value_and_skipped_binders();
139 assert_eq!(subst.len(interner), binders.len(interner));
140 value.subst_bound_vars(subst)
141 }
142} 93}
143 94
144pub fn make_only_type_binders<T>(num_vars: usize, value: T) -> Binders<T> { 95pub fn make_only_type_binders<T>(num_vars: usize, value: T) -> Binders<T> {
@@ -153,31 +104,11 @@ pub fn make_only_type_binders<T>(num_vars: usize, value: T) -> Binders<T> {
153} 104}
154 105
155impl TraitRef { 106impl TraitRef {
156 pub fn self_type_parameter(&self, interner: &Interner) -> &Ty {
157 &self.substitution.at(interner, 0).assert_ty_ref(interner)
158 }
159
160 pub fn hir_trait_id(&self) -> TraitId { 107 pub fn hir_trait_id(&self) -> TraitId {
161 from_chalk_trait_id(self.trait_id) 108 from_chalk_trait_id(self.trait_id)
162 } 109 }
163} 110}
164 111
165impl WhereClause {
166 pub fn is_implemented(&self) -> bool {
167 matches!(self, WhereClause::Implemented(_))
168 }
169
170 pub fn trait_ref(&self, db: &dyn HirDatabase) -> Option<TraitRef> {
171 match self {
172 WhereClause::Implemented(tr) => Some(tr.clone()),
173 WhereClause::AliasEq(AliasEq { alias: AliasTy::Projection(proj), .. }) => {
174 Some(proj.trait_ref(db))
175 }
176 WhereClause::AliasEq(_) => None,
177 }
178 }
179}
180
181impl<T> Canonical<T> { 112impl<T> Canonical<T> {
182 pub fn new(value: T, kinds: impl IntoIterator<Item = TyVariableKind>) -> Self { 113 pub fn new(value: T, kinds: impl IntoIterator<Item = TyVariableKind>) -> Self {
183 let kinds = kinds.into_iter().map(|tk| { 114 let kinds = kinds.into_iter().map(|tk| {