aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/lib.rs
diff options
context:
space:
mode:
authorLukas Wirth <[email protected]>2021-04-06 11:36:12 +0100
committerLukas Wirth <[email protected]>2021-04-06 12:58:48 +0100
commit9b4699a9be2c45ec96647c2079a32706ec65f222 (patch)
tree22a59fac0d60d102aae5e8f9266b161c17ff8696 /crates/hir_ty/src/lib.rs
parent002e72a28de3df818992442ad49bb60d3d0b1d0b (diff)
Move Ty accessors to TyExt
Diffstat (limited to 'crates/hir_ty/src/lib.rs')
-rw-r--r--crates/hir_ty/src/lib.rs214
1 files changed, 3 insertions, 211 deletions
diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs
index a2a5bcc07..f5b658cba 100644
--- a/crates/hir_ty/src/lib.rs
+++ b/crates/hir_ty/src/lib.rs
@@ -30,13 +30,11 @@ mod test_db;
30 30
31use std::sync::Arc; 31use std::sync::Arc;
32 32
33use chalk_ir::UintTy;
34use itertools::Itertools;
35
36use base_db::salsa; 33use base_db::salsa;
34use chalk_ir::UintTy;
37use hir_def::{ 35use hir_def::{
38 expr::ExprId, type_ref::Rawness, AssocContainerId, ConstParamId, FunctionId, GenericDefId, 36 expr::ExprId, type_ref::Rawness, ConstParamId, LifetimeParamId, TraitId, TypeAliasId,
39 HasModule, LifetimeParamId, Lookup, TraitId, TypeAliasId, TypeParamId, 37 TypeParamId,
40}; 38};
41 39
42use crate::{db::HirDatabase, display::HirDisplay, utils::generics}; 40use crate::{db::HirDatabase, display::HirDisplay, utils::generics};
@@ -171,65 +169,6 @@ impl CallableSig {
171} 169}
172 170
173impl Ty { 171impl Ty {
174 pub fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)> {
175 match self.kind(&Interner) {
176 TyKind::Ref(mutability, lifetime, ty) => Some((ty, *lifetime, *mutability)),
177 _ => None,
178 }
179 }
180
181 pub fn as_reference_or_ptr(&self) -> Option<(&Ty, Rawness, Mutability)> {
182 match self.kind(&Interner) {
183 TyKind::Ref(mutability, _, ty) => Some((ty, Rawness::Ref, *mutability)),
184 TyKind::Raw(mutability, ty) => Some((ty, Rawness::RawPtr, *mutability)),
185 _ => None,
186 }
187 }
188
189 pub fn strip_references(&self) -> &Ty {
190 let mut t: &Ty = self;
191
192 while let TyKind::Ref(_mutability, _lifetime, ty) = t.kind(&Interner) {
193 t = ty;
194 }
195
196 t
197 }
198
199 pub fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)> {
200 match self.kind(&Interner) {
201 TyKind::Adt(AdtId(adt), parameters) => Some((*adt, parameters)),
202 _ => None,
203 }
204 }
205
206 pub fn as_tuple(&self) -> Option<&Substitution> {
207 match self.kind(&Interner) {
208 TyKind::Tuple(_, substs) => Some(substs),
209 _ => None,
210 }
211 }
212
213 pub fn as_generic_def(&self, db: &dyn HirDatabase) -> Option<GenericDefId> {
214 match *self.kind(&Interner) {
215 TyKind::Adt(AdtId(adt), ..) => Some(adt.into()),
216 TyKind::FnDef(callable, ..) => {
217 Some(db.lookup_intern_callable_def(callable.into()).into())
218 }
219 TyKind::AssociatedType(type_alias, ..) => Some(from_assoc_type_id(type_alias).into()),
220 TyKind::Foreign(type_alias, ..) => Some(from_foreign_def_id(type_alias).into()),
221 _ => None,
222 }
223 }
224
225 pub fn is_never(&self) -> bool {
226 matches!(self.kind(&Interner), TyKind::Never)
227 }
228
229 pub fn is_unknown(&self) -> bool {
230 matches!(self.kind(&Interner), TyKind::Error)
231 }
232
233 pub fn equals_ctor(&self, other: &Ty) -> bool { 172 pub fn equals_ctor(&self, other: &Ty) -> bool {
234 match (self.kind(&Interner), other.kind(&Interner)) { 173 match (self.kind(&Interner), other.kind(&Interner)) {
235 (TyKind::Adt(adt, ..), TyKind::Adt(adt2, ..)) => adt == adt2, 174 (TyKind::Adt(adt, ..), TyKind::Adt(adt2, ..)) => adt == adt2,
@@ -260,24 +199,6 @@ impl Ty {
260 } 199 }
261 } 200 }
262 201
263 /// If this is a `dyn Trait` type, this returns the `Trait` part.
264 fn dyn_trait_ref(&self) -> Option<&TraitRef> {
265 match self.kind(&Interner) {
266 TyKind::Dyn(dyn_ty) => dyn_ty.bounds.skip_binders().interned().get(0).and_then(|b| {
267 match b.skip_binders() {
268 WhereClause::Implemented(trait_ref) => Some(trait_ref),
269 _ => None,
270 }
271 }),
272 _ => None,
273 }
274 }
275
276 /// If this is a `dyn Trait`, returns that trait.
277 pub fn dyn_trait(&self) -> Option<TraitId> {
278 self.dyn_trait_ref().map(|it| it.trait_id).map(from_chalk_trait_id)
279 }
280
281 fn builtin_deref(&self) -> Option<Ty> { 202 fn builtin_deref(&self) -> Option<Ty> {
282 match self.kind(&Interner) { 203 match self.kind(&Interner) {
283 TyKind::Ref(.., ty) => Some(ty.clone()), 204 TyKind::Ref(.., ty) => Some(ty.clone()),
@@ -286,37 +207,6 @@ impl Ty {
286 } 207 }
287 } 208 }
288 209
289 pub fn callable_def(&self, db: &dyn HirDatabase) -> Option<CallableDefId> {
290 match self.kind(&Interner) {
291 &TyKind::FnDef(def, ..) => Some(db.lookup_intern_callable_def(def.into())),
292 _ => None,
293 }
294 }
295
296 pub fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId> {
297 if let Some(CallableDefId::FunctionId(func)) = self.callable_def(db) {
298 Some(func)
299 } else {
300 None
301 }
302 }
303
304 pub fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig> {
305 match self.kind(&Interner) {
306 TyKind::Function(fn_ptr) => Some(CallableSig::from_fn_ptr(fn_ptr)),
307 TyKind::FnDef(def, parameters) => {
308 let callable_def = db.lookup_intern_callable_def((*def).into());
309 let sig = db.callable_item_signature(callable_def);
310 Some(sig.substitute(&Interner, &parameters))
311 }
312 TyKind::Closure(.., substs) => {
313 let sig_param = substs.at(&Interner, 0).assert_ty_ref(&Interner);
314 sig_param.callable_sig(db)
315 }
316 _ => None,
317 }
318 }
319
320 /// Returns the type parameters of this type if it has some (i.e. is an ADT 210 /// Returns the type parameters of this type if it has some (i.e. is an ADT
321 /// or function); so if `self` is `Option<u32>`, this returns the `u32`. 211 /// or function); so if `self` is `Option<u32>`, this returns the `u32`.
322 pub fn substs(&self) -> Option<&Substitution> { 212 pub fn substs(&self) -> Option<&Substitution> {
@@ -344,104 +234,6 @@ impl Ty {
344 _ => None, 234 _ => None,
345 } 235 }
346 } 236 }
347
348 pub fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>> {
349 match self.kind(&Interner) {
350 TyKind::OpaqueType(opaque_ty_id, ..) => {
351 match db.lookup_intern_impl_trait_id((*opaque_ty_id).into()) {
352 ImplTraitId::AsyncBlockTypeImplTrait(def, _expr) => {
353 let krate = def.module(db.upcast()).krate();
354 if let Some(future_trait) = db
355 .lang_item(krate, "future_trait".into())
356 .and_then(|item| item.as_trait())
357 {
358 // This is only used by type walking.
359 // Parameters will be walked outside, and projection predicate is not used.
360 // So just provide the Future trait.
361 let impl_bound = Binders::empty(
362 &Interner,
363 WhereClause::Implemented(TraitRef {
364 trait_id: to_chalk_trait_id(future_trait),
365 substitution: Substitution::empty(&Interner),
366 }),
367 );
368 Some(vec![impl_bound])
369 } else {
370 None
371 }
372 }
373 ImplTraitId::ReturnTypeImplTrait(..) => None,
374 }
375 }
376 TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
377 let predicates = match db.lookup_intern_impl_trait_id(opaque_ty.opaque_ty_id.into())
378 {
379 ImplTraitId::ReturnTypeImplTrait(func, idx) => {
380 db.return_type_impl_traits(func).map(|it| {
381 let data = (*it)
382 .as_ref()
383 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
384 data.substitute(&Interner, &opaque_ty.substitution)
385 })
386 }
387 // It always has an parameter for Future::Output type.
388 ImplTraitId::AsyncBlockTypeImplTrait(..) => unreachable!(),
389 };
390
391 predicates.map(|it| it.into_value_and_skipped_binders().0)
392 }
393 TyKind::Placeholder(idx) => {
394 let id = from_placeholder_idx(db, *idx);
395 let generic_params = db.generic_params(id.parent);
396 let param_data = &generic_params.types[id.local_id];
397 match param_data.provenance {
398 hir_def::generics::TypeParamProvenance::ArgumentImplTrait => {
399 let substs = TyBuilder::type_params_subst(db, id.parent);
400 let predicates = db
401 .generic_predicates(id.parent)
402 .into_iter()
403 .map(|pred| pred.clone().substitute(&Interner, &substs))
404 .filter(|wc| match &wc.skip_binders() {
405 WhereClause::Implemented(tr) => {
406 tr.self_type_parameter(&Interner) == self
407 }
408 WhereClause::AliasEq(AliasEq {
409 alias: AliasTy::Projection(proj),
410 ty: _,
411 }) => proj.self_type_parameter(&Interner) == self,
412 _ => false,
413 })
414 .collect_vec();
415
416 Some(predicates)
417 }
418 _ => None,
419 }
420 }
421 _ => None,
422 }
423 }
424
425 pub fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId> {
426 match self.kind(&Interner) {
427 TyKind::AssociatedType(id, ..) => {
428 match from_assoc_type_id(*id).lookup(db.upcast()).container {
429 AssocContainerId::TraitId(trait_id) => Some(trait_id),
430 _ => None,
431 }
432 }
433 TyKind::Alias(AliasTy::Projection(projection_ty)) => {
434 match from_assoc_type_id(projection_ty.associated_ty_id)
435 .lookup(db.upcast())
436 .container
437 {
438 AssocContainerId::TraitId(trait_id) => Some(trait_id),
439 _ => None,
440 }
441 }
442 _ => None,
443 }
444 }
445} 237}
446 238
447#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] 239#[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)]