1 //! Various extensions traits for Chalk types.
3 use chalk_ir::{FloatTy, IntTy, Mutability, Scalar, UintTy};
5 builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType, BuiltinUint},
7 FunctionId, GenericDefId, HasModule, ItemContainerId, Lookup, TraitId,
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,
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;
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>;
32 fn callable_def(&self, db: &dyn HirDatabase) -> Option<CallableDefId>;
33 fn callable_sig(&self, db: &dyn HirDatabase) -> Option<CallableSig>;
35 fn strip_references(&self) -> &Ty;
37 /// If this is a `dyn Trait`, returns that trait.
38 fn dyn_trait(&self) -> Option<TraitId>;
40 fn impl_trait_bounds(&self, db: &dyn HirDatabase) -> Option<Vec<QuantifiedWhereClause>>;
41 fn associated_type_parent_trait(&self, db: &dyn HirDatabase) -> Option<TraitId>;
43 /// FIXME: Get rid of this, it's not a good abstraction
44 fn equals_ctor(&self, other: &Ty) -> bool;
48 fn is_unit(&self) -> bool {
49 matches!(self.kind(Interner), TyKind::Tuple(0, _))
52 fn is_never(&self) -> bool {
53 matches!(self.kind(Interner), TyKind::Never)
56 fn is_unknown(&self) -> bool {
57 matches!(self.kind(Interner), TyKind::Error)
60 fn is_ty_var(&self) -> bool {
61 matches!(self.kind(Interner), TyKind::InferenceVar(_, _))
64 fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)> {
65 match self.kind(Interner) {
66 TyKind::Adt(AdtId(adt), parameters) => Some((*adt, parameters)),
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,
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,
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,
100 fn as_tuple(&self) -> Option<&Substitution> {
101 match self.kind(Interner) {
102 TyKind::Tuple(_, substs) => Some(substs),
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,
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)),
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)),
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())
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()),
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())),
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, ¶meters))
155 TyKind::Closure(.., substs) => {
156 let sig_param = substs.at(Interner, 0).assert_ty_ref(Interner);
157 sig_param.callable_sig(db)
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),
173 Some(from_chalk_trait_id(trait_ref.trait_id))
176 fn strip_references(&self) -> &Ty {
177 let mut t: &Ty = self;
178 while let TyKind::Ref(_mutability, _lifetime, ty) = t.kind(Interner) {
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())
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(
199 WhereClause::Implemented(TraitRef {
200 trait_id: to_chalk_trait_id(future_trait),
201 substitution: Substitution::empty(Interner),
204 Some(vec![impl_bound])
209 ImplTraitId::ReturnTypeImplTrait(func, idx) => {
210 db.return_type_impl_traits(func).map(|it| {
213 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
214 data.substitute(Interner, &subst).into_value_and_skipped_binders().0
219 TyKind::Alias(AliasTy::Opaque(opaque_ty)) => {
220 let predicates = match db.lookup_intern_impl_trait_id(opaque_ty.opaque_ty_id.into())
222 ImplTraitId::ReturnTypeImplTrait(func, idx) => {
223 db.return_type_impl_traits(func).map(|it| {
226 .map(|rpit| rpit.impl_traits[idx as usize].bounds.clone());
227 data.substitute(Interner, &opaque_ty.substitution)
230 // It always has an parameter for Future::Output type.
231 ImplTraitId::AsyncBlockTypeImplTrait(..) => unreachable!(),
234 predicates.map(|it| it.into_value_and_skipped_binders().0)
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);
244 .generic_predicates(id.parent)
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
251 WhereClause::AliasEq(AliasEq {
252 alias: AliasTy::Projection(proj),
254 }) => &proj.self_type_parameter(Interner) == self,
257 .collect::<Vec<_>>();
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),
276 TyKind::Alias(AliasTy::Projection(projection_ty)) => {
277 match from_assoc_type_id(projection_ty.associated_ty_id)
281 ItemContainerId::TraitId(trait_id) => Some(trait_id),
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(_, _)) => {
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, ..)) => {
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
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
313 (TyKind::Str, TyKind::Str) | (TyKind::Never, TyKind::Never) => true,
314 (TyKind::Scalar(scalar), TyKind::Scalar(scalar2)) => scalar == scalar2,
320 pub trait ProjectionTyExt {
321 fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef;
322 fn trait_(&self, db: &dyn HirDatabase) -> TraitId;
325 impl ProjectionTyExt for ProjectionTy {
326 fn trait_ref(&self, db: &dyn HirDatabase) -> TraitRef {
328 trait_id: to_chalk_trait_id(self.trait_(db)),
329 substitution: self.substitution.clone(),
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"),
341 pub trait TraitRefExt {
342 fn hir_trait_id(&self) -> TraitId;
345 impl TraitRefExt for TraitRef {
346 fn hir_trait_id(&self) -> TraitId {
347 from_chalk_trait_id(self.trait_id)