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