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