]> git.lizzy.rs Git - rust.git/blob - crates/hir_ty/src/chalk_ext.rs
Merge #8853
[rust.git] / crates / hir_ty / src / chalk_ext.rs
1 //! Various extensions traits for Chalk types.
2
3 use chalk_ir::{FloatTy, IntTy, Mutability, Scalar, UintTy};
4 use hir_def::{
5     builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType, BuiltinUint},
6     type_ref::Rawness,
7     AssocContainerId, FunctionId, GenericDefId, HasModule, Lookup, TraitId,
8 };
9
10 use crate::{
11     db::HirDatabase, from_assoc_type_id, from_chalk_trait_id, from_foreign_def_id,
12     from_placeholder_idx, to_chalk_trait_id, AdtId, AliasEq, AliasTy, Binders, CallableDefId,
13     CallableSig, FnPointer, ImplTraitId, Interner, Lifetime, ProjectionTy, QuantifiedWhereClause,
14     Substitution, TraitRef, Ty, TyBuilder, TyKind, WhereClause,
15 };
16
17 pub trait TyExt {
18     fn is_unit(&self) -> bool;
19     fn is_never(&self) -> bool;
20     fn is_unknown(&self) -> bool;
21
22     fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)>;
23     fn as_builtin(&self) -> Option<BuiltinType>;
24     fn as_tuple(&self) -> Option<&Substitution>;
25     fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId>;
26     fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)>;
27     fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)>;
28     fn as_generic_def(&self, db: &dyn HirDatabase) -> Option<GenericDefId>;
29
30     fn callable_def(&self, db: &dyn HirDatabase) -> Option<CallableDefId>;
31     fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig>;
32
33     fn strip_references(&self) -> &Ty;
34
35     /// If this is a `dyn Trait`, returns that trait.
36     fn dyn_trait(&self) -> Option<TraitId>;
37
38     fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>>;
39     fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId>;
40
41     /// FIXME: Get rid of this, it's not a good abstraction
42     fn equals_ctor(&self, other: &Ty) -> bool;
43 }
44
45 impl TyExt for Ty {
46     fn is_unit(&self) -> bool {
47         matches!(self.kind(&Interner), TyKind::Tuple(0, _))
48     }
49
50     fn is_never(&self) -> bool {
51         matches!(self.kind(&Interner), TyKind::Never)
52     }
53
54     fn is_unknown(&self) -> bool {
55         matches!(self.kind(&Interner), TyKind::Error)
56     }
57
58     fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)> {
59         match self.kind(&Interner) {
60             TyKind::Adt(AdtId(adt), parameters) => Some((*adt, parameters)),
61             _ => None,
62         }
63     }
64
65     fn as_builtin(&self) -> Option<BuiltinType> {
66         match self.kind(&Interner) {
67             TyKind::Str => Some(BuiltinType::Str),
68             TyKind::Scalar(Scalar::Bool) => Some(BuiltinType::Bool),
69             TyKind::Scalar(Scalar::Char) => Some(BuiltinType::Char),
70             TyKind::Scalar(Scalar::Float(fty)) => Some(BuiltinType::Float(match fty {
71                 FloatTy::F64 => BuiltinFloat::F64,
72                 FloatTy::F32 => BuiltinFloat::F32,
73             })),
74             TyKind::Scalar(Scalar::Int(ity)) => Some(BuiltinType::Int(match ity {
75                 IntTy::Isize => BuiltinInt::Isize,
76                 IntTy::I8 => BuiltinInt::I8,
77                 IntTy::I16 => BuiltinInt::I16,
78                 IntTy::I32 => BuiltinInt::I32,
79                 IntTy::I64 => BuiltinInt::I64,
80                 IntTy::I128 => BuiltinInt::I128,
81             })),
82             TyKind::Scalar(Scalar::Uint(ity)) => Some(BuiltinType::Uint(match ity {
83                 UintTy::Usize => BuiltinUint::Usize,
84                 UintTy::U8 => BuiltinUint::U8,
85                 UintTy::U16 => BuiltinUint::U16,
86                 UintTy::U32 => BuiltinUint::U32,
87                 UintTy::U64 => BuiltinUint::U64,
88                 UintTy::U128 => BuiltinUint::U128,
89             })),
90             _ => None,
91         }
92     }
93
94     fn as_tuple(&self) -> Option<&Substitution> {
95         match self.kind(&Interner) {
96             TyKind::Tuple(_, substs) => Some(substs),
97             _ => None,
98         }
99     }
100
101     fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId> {
102         if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) {
103             Some(func)
104         } else {
105             None
106         }
107     }
108     fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> {
109         match self.kind(&Interner) {
110             TyKind::Ref(mutability, lifetime, ty) => Some((ty, lifetime.clone(), *mutability)),
111             _ => None,
112         }
113     }
114
115     fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> {
116         match self.kind(&Interner) {
117             TyKind::Ref(mutability, _, ty) => Some((ty, Rawness::Ref, *mutability)),
118             TyKind::Raw(mutability, ty) => Some((ty, Rawness::RawPtr, *mutability)),
119             _ => None,
120         }
121     }
122
123     fn as_generic_def(&self, db: &dyn HirDatabase) -> Option<GenericDefId> {
124         match *self.kind(&Interner) {
125             TyKind::Adt(AdtId(adt), ..) => Some(adt.into()),
126             TyKind::FnDef(callable, ..) => {
127                 Some(db.lookup_intern_callable_def(callable.into()).into())
128             }
129             TyKind::AssociatedType(type_alias, ..) => Some(from_assoc_type_id(type_alias).into()),
130             TyKind::Foreign(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()),
131             _ => None,
132         }
133     }
134
135     fn callable_def(&self, db: &dyn HirDatabase) -> Option<CallableDefId> {
136         match self.kind(&Interner) {
137             &TyKind::FnDef(def, ..) => Some(db.lookup_intern_callable_def(def.into())),
138             _ => None,
139         }
140     }
141
142     fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig> {
143         match self.kind(&Interner) {
144             TyKind::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)),
145             TyKind::FnDef(def, parameters) => {
146                 let callable_def = db.lookup_intern_callable_def((*def).into());
147                 let sig = db.callable_item_signature(callable_def);
148                 Some(sig.substitute(&Interner, &parameters))
149             }
150             TyKind::Closure(.., substs) => {
151                 let sig_param = substs.at(&Interner, 0).assert_ty_ref(&Interner);
152                 sig_param.callable_sig(db)
153             }
154             _ => None,
155         }
156     }
157
158     fn dyn_trait(&self) -> Option<TraitId> {
159         let trait_ref = match self.kind(&Interner) {
160             TyKind::Dyn(dyn_ty) => dyn_ty.bounds.skip_binders().interned().get(0).and_then(|b| {
161                 match b.skip_binders() {
162                     WhereClause::Implemented(trait_ref) => Some(trait_ref),
163                     _ => None,
164                 }
165             }),
166             _ => None,
167         }?;
168         Some(from_chalk_trait_id(trait_ref.trait_id))
169     }
170
171     fn strip_references(&self) -> &Ty {
172         let mut t: &Ty = self;
173         while let TyKind::Ref(_mutability, _lifetime, ty) = t.kind(&Interner) {
174             t = ty;
175         }
176         t
177     }
178
179     fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>> {
180         match self.kind(&Interner) {
181             TyKind::OpaqueType(opaque_ty_id, ..) => {
182                 match db.lookup_intern_impl_trait_id((*opaque_ty_id).into()) {
183                     ImplTraitId::AsyncBlockTypeImplTrait(def, _expr) => {
184                         let krate = def.module(db.upcast()).krate();
185                         if let Some(future_trait) = db
186                             .lang_item(krate, "future_trait".into())
187                             .and_then(|item| item.as_trait())
188                         {
189                             // This is only used by type walking.
190                             // Parameters will be walked outside, and projection predicate is not used.
191                             // So just provide the Future trait.
192                             let impl_bound = Binders::empty(
193                                 &Interner,
194                                 WhereClause::Implemented(TraitRef {
195                                     trait_id: to_chalk_trait_id(future_trait),
196                                     substitution: Substitution::empty(&Interner),
197                                 }),
198                             );
199                             Some(vec![impl_bound])
200                         } else {
201                             None
202                         }
203                     }
204                     ImplTraitId::ReturnTypeImplTrait(..) => None,
205                 }
206             }
207             TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
208                 let predicates = match db.lookup_intern_impl_trait_id(opaque_ty.opaque_ty_id.into())
209                 {
210                     ImplTraitId::ReturnTypeImplTrait(func, idx) => {
211                         db.return_type_impl_traits(func).map(|it| {
212                             let data = (*it)
213                                 .as_ref()
214                                 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
215                             data.substitute(&Interner, &opaque_ty.substitution)
216                         })
217                     }
218                     // It always has an parameter for Future::Output type.
219                     ImplTraitId::AsyncBlockTypeImplTrait(..) => unreachable!(),
220                 };
221
222                 predicates.map(|it| it.into_value_and_skipped_binders().0)
223             }
224             TyKind::Placeholder(idx) => {
225                 let id = from_placeholder_idx(db, *idx);
226                 let generic_params = db.generic_params(id.parent);
227                 let param_data = &generic_params.types[id.local_id];
228                 match param_data.provenance {
229                     hir_def::generics::TypeParamProvenance::ArgumentImplTrait => {
230                         let substs = TyBuilder::type_params_subst(db, id.parent);
231                         let predicates = db
232                             .generic_predicates(id.parent)
233                             .into_iter()
234                             .map(|pred| pred.clone().substitute(&Interner, &substs))
235                             .filter(|wc| match &wc.skip_binders() {
236                                 WhereClause::Implemented(tr) => {
237                                     &tr.self_type_parameter(&Interner) == self
238                                 }
239                                 WhereClause::AliasEq(AliasEq {
240                                     alias: AliasTy::Projection(proj),
241                                     ty: _,
242                                 }) => &proj.self_type_parameter(&Interner) == self,
243                                 _ => false,
244                             })
245                             .collect::<Vec<_>>();
246
247                         Some(predicates)
248                     }
249                     _ => None,
250                 }
251             }
252             _ => None,
253         }
254     }
255
256     fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> {
257         match self.kind(&Interner) {
258             TyKind::AssociatedType(id, ..) => {
259                 match from_assoc_type_id(*id).lookup(db.upcast()).container {
260                     AssocContainerId::TraitId(trait_id) => Some(trait_id),
261                     _ => None,
262                 }
263             }
264             TyKind::Alias(AliasTy::Projection(projection_ty)) => {
265                 match from_assoc_type_id(projection_ty.associated_ty_id)
266                     .lookup(db.upcast())
267                     .container
268                 {
269                     AssocContainerId::TraitId(trait_id) => Some(trait_id),
270                     _ => None,
271                 }
272             }
273             _ => None,
274         }
275     }
276
277     fn equals_ctor(&self, other: &Ty) -> bool {
278         match (self.kind(&Interner), other.kind(&Interner)) {
279             (TyKind::Adt(adt, ..), TyKind::Adt(adt2, ..)) => adt == adt2,
280             (TyKind::Slice(_), TyKind::Slice(_)) | (TyKind::Array(_, _), TyKind::Array(_, _)) => {
281                 true
282             }
283             (TyKind::FnDef(def_id, ..), TyKind::FnDef(def_id2, ..)) => def_id == def_id2,
284             (TyKind::OpaqueType(ty_id, ..), TyKind::OpaqueType(ty_id2, ..)) => ty_id == ty_id2,
285             (TyKind::AssociatedType(ty_id, ..), TyKind::AssociatedType(ty_id2, ..)) => {
286                 ty_id == ty_id2
287             }
288             (TyKind::Foreign(ty_id, ..), TyKind::Foreign(ty_id2, ..)) => ty_id == ty_id2,
289             (TyKind::Closure(id1, _), TyKind::Closure(id2, _)) => id1 == id2,
290             (TyKind::Ref(mutability, ..), TyKind::Ref(mutability2, ..))
291             | (TyKind::Raw(mutability, ..), TyKind::Raw(mutability2, ..)) => {
292                 mutability == mutability2
293             }
294             (
295                 TyKind::Function(FnPointer { num_binders, sig, .. }),
296                 TyKind::Function(FnPointer { num_binders: num_binders2, sig: sig2, .. }),
297             ) => num_binders == num_binders2 && sig == sig2,
298             (TyKind::Tuple(cardinality, _), TyKind::Tuple(cardinality2, _)) => {
299                 cardinality == cardinality2
300             }
301             (TyKind::Str, TyKind::Str) | (TyKind::Never, TyKind::Never) => true,
302             (TyKind::Scalar(scalar), TyKind::Scalar(scalar2)) => scalar == scalar2,
303             _ => false,
304         }
305     }
306 }
307
308 pub trait ProjectionTyExt {
309     fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef;
310     fn trait_(&self, db: &dyn HirDatabase) -> TraitId;
311 }
312
313 impl ProjectionTyExt for ProjectionTy {
314     fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef {
315         TraitRef {
316             trait_id: to_chalk_trait_id(self.trait_(db)),
317             substitution: self.substitution.clone(),
318         }
319     }
320
321     fn trait_(&self, db: &dyn HirDatabase) -> TraitId {
322         match from_assoc_type_id(self.associated_ty_id).lookup(db.upcast()).container {
323             AssocContainerId::TraitId(it) => it,
324             _ => panic!("projection ty without parent trait"),
325         }
326     }
327 }
328
329 pub trait TraitRefExt {
330     fn hir_trait_id(&self) -> TraitId;
331 }
332
333 impl TraitRefExt for TraitRef {
334     fn hir_trait_id(&self) -> TraitId {
335         from_chalk_trait_id(self.trait_id)
336     }
337 }