Correctly support SelfType when searching for usages

This commit is contained in:
Lukas Wirth 2021-05-08 22:34:55 +02:00
parent 96c5df9b17
commit 41f470fea8
6 changed files with 214 additions and 80 deletions

View file

@ -1,8 +1,10 @@
//! Various extensions traits for Chalk types.
use chalk_ir::Mutability;
use chalk_ir::{FloatTy, IntTy, Mutability, Scalar, UintTy};
use hir_def::{
type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule, Lookup, TraitId,
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType, BuiltinUint},
type_ref::Rawness,
AssocContainerId, FunctionId, GenericDefId, HasModule, Lookup, TraitId,
};
use crate::{
@ -18,6 +20,7 @@ pub trait TyExt {
fn is_unknown(&self) -> bool;
fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)>;
fn as_builtin(&self) -> Option<BuiltinType>;
fn as_tuple(&self) -> Option<&Substitution>;
fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId>;
fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)>;
@ -59,6 +62,35 @@ impl TyExt for Ty {
}
}
fn as_builtin(&self) -> Option<BuiltinType> {
match self.kind(&Interner) {
TyKind::Str => Some(BuiltinType::Str),
TyKind::Scalar(Scalar::Bool) => Some(BuiltinType::Bool),
TyKind::Scalar(Scalar::Char) => Some(BuiltinType::Char),
TyKind::Scalar(Scalar::Float(fty)) => Some(BuiltinType::Float(match fty {
FloatTy::F64 => BuiltinFloat::F64,
FloatTy::F32 => BuiltinFloat::F32,
})),
TyKind::Scalar(Scalar::Int(ity)) => Some(BuiltinType::Int(match ity {
IntTy::Isize => BuiltinInt::Isize,
IntTy::I8 => BuiltinInt::I8,
IntTy::I16 => BuiltinInt::I16,
IntTy::I32 => BuiltinInt::I32,
IntTy::I64 => BuiltinInt::I64,
IntTy::I128 => BuiltinInt::I128,
})),
TyKind::Scalar(Scalar::Uint(ity)) => Some(BuiltinType::Uint(match ity {
UintTy::Usize => BuiltinUint::Usize,
UintTy::U8 => BuiltinUint::U8,
UintTy::U16 => BuiltinUint::U16,
UintTy::U32 => BuiltinUint::U32,
UintTy::U64 => BuiltinUint::U64,
UintTy::U128 => BuiltinUint::U128,
})),
_ => None,
}
}
fn as_tuple(&self) -> Option<&Substitution> {
match self.kind(&Interner) {
TyKind::Tuple(_, substs) => Some(substs),