From 41f470fea84998af65292f3c297c3e2b1d897848 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Sat, 8 May 2021 22:34:55 +0200 Subject: Correctly support SelfType when searching for usages --- crates/hir_ty/src/chalk_ext.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'crates/hir_ty') diff --git a/crates/hir_ty/src/chalk_ext.rs b/crates/hir_ty/src/chalk_ext.rs index 8c4542956..5232a7d80 100644 --- a/crates/hir_ty/src/chalk_ext.rs +++ b/crates/hir_ty/src/chalk_ext.rs @@ -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; fn as_tuple(&self) -> Option<&Substitution>; fn as_fn_def(&self, db: &dyn HirDatabase) -> Option; fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)>; @@ -59,6 +62,35 @@ impl TyExt for Ty { } } + fn as_builtin(&self) -> Option { + 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), -- cgit v1.2.3