11//! Various extensions traits for Chalk types.
22
3- use chalk_ir:: Mutability ;
43use hir_def:: { FunctionId , ItemContainerId , Lookup , TraitId } ;
54
65use crate :: {
7- AdtId , Binders , CallableDefId , CallableSig , DynTy , Interner , Lifetime , ProjectionTy ,
8- Substitution , ToChalk , TraitRef , Ty , TyKind , TypeFlags , WhereClause , db:: HirDatabase ,
9- from_assoc_type_id, from_chalk_trait_id, generics:: generics, to_chalk_trait_id,
10- utils:: ClosureSubst ,
6+ Binders , CallableDefId , CallableSig , DynTy , Interner , ProjectionTy , Substitution , ToChalk ,
7+ TraitRef , Ty , TyKind , db:: HirDatabase , from_assoc_type_id, from_chalk_trait_id,
8+ generics:: generics, to_chalk_trait_id, utils:: ClosureSubst ,
119} ;
1210
1311pub ( crate ) trait TyExt {
1412 fn is_unit ( & self ) -> bool ;
1513 fn is_unknown ( & self ) -> bool ;
16- fn contains_unknown ( & self ) -> bool ;
1714
18- fn as_adt ( & self ) -> Option < ( hir_def:: AdtId , & Substitution ) > ;
1915 fn as_tuple ( & self ) -> Option < & Substitution > ;
2016 fn as_fn_def ( & self , db : & dyn HirDatabase ) -> Option < FunctionId > ;
21- fn as_reference ( & self ) -> Option < ( & Ty , Lifetime , Mutability ) > ;
2217
2318 fn callable_def ( & self , db : & dyn HirDatabase ) -> Option < CallableDefId > ;
2419 fn callable_sig ( & self , db : & dyn HirDatabase ) -> Option < CallableSig > ;
25-
26- fn strip_references ( & self ) -> & Ty ;
27-
28- /// If this is a `dyn Trait`, returns that trait.
29- fn dyn_trait ( & self ) -> Option < TraitId > ;
3020}
3121
3222impl TyExt for Ty {
@@ -38,17 +28,6 @@ impl TyExt for Ty {
3828 matches ! ( self . kind( Interner ) , TyKind :: Error )
3929 }
4030
41- fn contains_unknown ( & self ) -> bool {
42- self . data ( Interner ) . flags . contains ( TypeFlags :: HAS_ERROR )
43- }
44-
45- fn as_adt ( & self ) -> Option < ( hir_def:: AdtId , & Substitution ) > {
46- match self . kind ( Interner ) {
47- TyKind :: Adt ( AdtId ( adt) , parameters) => Some ( ( * adt, parameters) ) ,
48- _ => None ,
49- }
50- }
51-
5231 fn as_tuple ( & self ) -> Option < & Substitution > {
5332 match self . kind ( Interner ) {
5433 TyKind :: Tuple ( _, substs) => Some ( substs) ,
@@ -63,13 +42,6 @@ impl TyExt for Ty {
6342 }
6443 }
6544
66- fn as_reference ( & self ) -> Option < ( & Ty , Lifetime , Mutability ) > {
67- match self . kind ( Interner ) {
68- TyKind :: Ref ( mutability, lifetime, ty) => Some ( ( ty, lifetime. clone ( ) , * mutability) ) ,
69- _ => None ,
70- }
71- }
72-
7345 fn callable_def ( & self , db : & dyn HirDatabase ) -> Option < CallableDefId > {
7446 match self . kind ( Interner ) {
7547 & TyKind :: FnDef ( def, ..) => Some ( ToChalk :: from_chalk ( db, def) ) ,
@@ -85,31 +57,6 @@ impl TyExt for Ty {
8557 _ => None ,
8658 }
8759 }
88-
89- fn dyn_trait ( & self ) -> Option < TraitId > {
90- let trait_ref = match self . kind ( Interner ) {
91- // The principal trait bound should be the first element of the bounds. This is an
92- // invariant ensured by `TyLoweringContext::lower_dyn_trait()`.
93- // FIXME: dyn types may not have principal trait and we don't want to return auto trait
94- // here.
95- TyKind :: Dyn ( dyn_ty) => dyn_ty. bounds . skip_binders ( ) . interned ( ) . first ( ) . and_then ( |b| {
96- match b. skip_binders ( ) {
97- WhereClause :: Implemented ( trait_ref) => Some ( trait_ref) ,
98- _ => None ,
99- }
100- } ) ,
101- _ => None ,
102- } ?;
103- Some ( from_chalk_trait_id ( trait_ref. trait_id ) )
104- }
105-
106- fn strip_references ( & self ) -> & Ty {
107- let mut t: & Ty = self ;
108- while let TyKind :: Ref ( _mutability, _lifetime, ty) = t. kind ( Interner ) {
109- t = ty;
110- }
111- t
112- }
11360}
11461
11562pub trait ProjectionTyExt {
0 commit comments