From e3a5c15d18492f5b9f0c606f988b914918d29ef5 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 7 Apr 2021 13:06:48 +0200 Subject: Move Ty::builtin_deref --- crates/hir_ty/src/autoderef.rs | 10 +++++++++- crates/hir_ty/src/lib.rs | 8 -------- 2 files changed, 9 insertions(+), 9 deletions(-) (limited to 'crates') diff --git a/crates/hir_ty/src/autoderef.rs b/crates/hir_ty/src/autoderef.rs index 80e192a57..f8e9db9ae 100644 --- a/crates/hir_ty/src/autoderef.rs +++ b/crates/hir_ty/src/autoderef.rs @@ -35,13 +35,21 @@ pub(crate) fn deref( krate: CrateId, ty: InEnvironment<&Canonical>, ) -> Option> { - if let Some(derefed) = ty.goal.value.builtin_deref() { + if let Some(derefed) = builtin_deref(&ty.goal.value) { Some(Canonical { value: derefed, binders: ty.goal.binders.clone() }) } else { deref_by_trait(db, krate, ty) } } +fn builtin_deref(ty: &Ty) -> Option { + match ty.kind(&Interner) { + TyKind::Ref(.., ty) => Some(ty.clone()), + TyKind::Raw(.., ty) => Some(ty.clone()), + _ => None, + } +} + fn deref_by_trait( db: &dyn HirDatabase, krate: CrateId, diff --git a/crates/hir_ty/src/lib.rs b/crates/hir_ty/src/lib.rs index 5c83a508d..ae3987752 100644 --- a/crates/hir_ty/src/lib.rs +++ b/crates/hir_ty/src/lib.rs @@ -199,14 +199,6 @@ impl Ty { } } - fn builtin_deref(&self) -> Option { - match self.kind(&Interner) { - TyKind::Ref(.., ty) => Some(ty.clone()), - TyKind::Raw(.., ty) => Some(ty.clone()), - _ => None, - } - } - /// Returns the type parameters of this type if it has some (i.e. is an ADT /// or function); so if `self` is `Option`, this returns the `u32`. pub fn substs(&self) -> Option<&Substitution> { -- cgit v1.2.3 From c3c8e8225ff0a7a741f24777b5ee7a9c3e91eeb7 Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Wed, 7 Apr 2021 13:09:31 +0200 Subject: Free Ty::def_crates --- crates/hir/src/lib.rs | 6 +- crates/hir_ty/src/method_resolution.rs | 106 ++++++++++++++++----------------- 2 files changed, 55 insertions(+), 57 deletions(-) (limited to 'crates') diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs index caa22dace..86b36c565 100644 --- a/crates/hir/src/lib.rs +++ b/crates/hir/src/lib.rs @@ -53,7 +53,7 @@ use hir_def::{ use hir_expand::{diagnostics::DiagnosticSink, name::name, MacroDefKind}; use hir_ty::{ autoderef, could_unify, - method_resolution::{self, TyFingerprint}, + method_resolution::{self, def_crates, TyFingerprint}, primitive::UintTy, subst_prefix, traits::FnTrait, @@ -1568,7 +1568,7 @@ impl Impl { } pub fn all_for_type(db: &dyn HirDatabase, Type { krate, ty, .. }: Type) -> Vec { - let def_crates = match ty.def_crates(db, krate) { + let def_crates = match def_crates(db, &ty, krate) { Some(def_crates) => def_crates, None => return Vec::new(), }; @@ -1955,7 +1955,7 @@ impl Type { krate: Crate, mut callback: impl FnMut(AssocItem) -> Option, ) -> Option { - for krate in self.ty.def_crates(db, krate.id)? { + for krate in def_crates(db, &self.ty, krate.id)? { let impls = db.inherent_impls_in_crate(krate); for impl_def in impls.for_self_ty(&self.ty) { diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index f29319f20..c601f2d53 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -239,15 +239,14 @@ impl InherentImpls { } } -impl Ty { - pub fn def_crates( - &self, - db: &dyn HirDatabase, - cur_crate: CrateId, - ) -> Option> { - // Types like slice can have inherent impls in several crates, (core and alloc). - // The corresponding impls are marked with lang items, so we can use them to find the required crates. - macro_rules! lang_item_crate { +pub fn def_crates( + db: &dyn HirDatabase, + ty: &Ty, + cur_crate: CrateId, +) -> Option> { + // Types like slice can have inherent impls in several crates, (core and alloc). + // The corresponding impls are marked with lang items, so we can use them to find the required crates. + macro_rules! lang_item_crate { ($($name:expr),+ $(,)?) => {{ let mut v = ArrayVec::::new(); $( @@ -257,51 +256,50 @@ impl Ty { }}; } - let mod_to_crate_ids = |module: ModuleId| Some(std::iter::once(module.krate()).collect()); + let mod_to_crate_ids = |module: ModuleId| Some(std::iter::once(module.krate()).collect()); - let lang_item_targets = match self.kind(&Interner) { - TyKind::Adt(AdtId(def_id), _) => { - return mod_to_crate_ids(def_id.module(db.upcast())); - } - TyKind::Foreign(id) => { - return mod_to_crate_ids( - from_foreign_def_id(*id).lookup(db.upcast()).module(db.upcast()), - ); - } - TyKind::Scalar(Scalar::Bool) => lang_item_crate!("bool"), - TyKind::Scalar(Scalar::Char) => lang_item_crate!("char"), - TyKind::Scalar(Scalar::Float(f)) => match f { - // There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime) - FloatTy::F32 => lang_item_crate!("f32", "f32_runtime"), - FloatTy::F64 => lang_item_crate!("f64", "f64_runtime"), - }, - &TyKind::Scalar(Scalar::Int(t)) => { - lang_item_crate!(primitive::int_ty_to_string(t)) - } - &TyKind::Scalar(Scalar::Uint(t)) => { - lang_item_crate!(primitive::uint_ty_to_string(t)) - } - TyKind::Str => lang_item_crate!("str_alloc", "str"), - TyKind::Slice(_) => lang_item_crate!("slice_alloc", "slice"), - TyKind::Raw(Mutability::Not, _) => lang_item_crate!("const_ptr"), - TyKind::Raw(Mutability::Mut, _) => lang_item_crate!("mut_ptr"), - TyKind::Dyn(_) => { - return self.dyn_trait().and_then(|trait_| { - mod_to_crate_ids(GenericDefId::TraitId(trait_).module(db.upcast())) - }); - } - _ => return None, - }; - let res = lang_item_targets - .into_iter() - .filter_map(|it| match it { - LangItemTarget::ImplDefId(it) => Some(it), - _ => None, - }) - .map(|it| it.lookup(db.upcast()).container.krate()) - .collect(); - Some(res) - } + let lang_item_targets = match ty.kind(&Interner) { + TyKind::Adt(AdtId(def_id), _) => { + return mod_to_crate_ids(def_id.module(db.upcast())); + } + TyKind::Foreign(id) => { + return mod_to_crate_ids( + from_foreign_def_id(*id).lookup(db.upcast()).module(db.upcast()), + ); + } + TyKind::Scalar(Scalar::Bool) => lang_item_crate!("bool"), + TyKind::Scalar(Scalar::Char) => lang_item_crate!("char"), + TyKind::Scalar(Scalar::Float(f)) => match f { + // There are two lang items: one in libcore (fXX) and one in libstd (fXX_runtime) + FloatTy::F32 => lang_item_crate!("f32", "f32_runtime"), + FloatTy::F64 => lang_item_crate!("f64", "f64_runtime"), + }, + &TyKind::Scalar(Scalar::Int(t)) => { + lang_item_crate!(primitive::int_ty_to_string(t)) + } + &TyKind::Scalar(Scalar::Uint(t)) => { + lang_item_crate!(primitive::uint_ty_to_string(t)) + } + TyKind::Str => lang_item_crate!("str_alloc", "str"), + TyKind::Slice(_) => lang_item_crate!("slice_alloc", "slice"), + TyKind::Raw(Mutability::Not, _) => lang_item_crate!("const_ptr"), + TyKind::Raw(Mutability::Mut, _) => lang_item_crate!("mut_ptr"), + TyKind::Dyn(_) => { + return ty.dyn_trait().and_then(|trait_| { + mod_to_crate_ids(GenericDefId::TraitId(trait_).module(db.upcast())) + }); + } + _ => return None, + }; + let res = lang_item_targets + .into_iter() + .filter_map(|it| match it { + LangItemTarget::ImplDefId(it) => Some(it), + _ => None, + }) + .map(|it| it.lookup(db.upcast()).container.krate()) + .collect(); + Some(res) } /// Look up the method with the given name, returning the actual autoderefed @@ -628,7 +626,7 @@ fn iterate_inherent_methods( visible_from_module: Option, callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool, ) -> bool { - let def_crates = match self_ty.value.def_crates(db, krate) { + let def_crates = match def_crates(db, &self_ty.value, krate) { Some(k) => k, None => return false, }; -- cgit v1.2.3