From e85ee60c42db57368e24ad9ac24840c2494d383c Mon Sep 17 00:00:00 2001 From: Marco Groppo Date: Mon, 15 Apr 2019 00:03:54 +0200 Subject: Initial support for lang items. --- crates/ra_hir/src/ty/method_resolution.rs | 38 +++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir/src/ty/method_resolution.rs') diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index 667b66095..ba516313c 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -14,6 +14,8 @@ use crate::{ resolve::Resolver, traits::TraitItem, generics::HasGenericParams, + lang_item::lang_item_lookup, + ty::primitive::{UncertainIntTy, UncertainFloatTy} }; use super::{TraitRef, Substs}; @@ -110,10 +112,33 @@ impl CrateImplBlocks { } } -fn def_crate(db: &impl HirDatabase, ty: &Ty) -> Option { +/// Rudimentary check whether an impl exists for a given type and trait; this +/// will actually be done by chalk. +pub(crate) fn implements(db: &impl HirDatabase, trait_ref: TraitRef) -> bool { + // FIXME use all trait impls in the whole crate graph + let krate = trait_ref.trait_.module(db).krate(db); + let krate = match krate { + Some(krate) => krate, + None => return false, + }; + let crate_impl_blocks = db.impls_in_crate(krate); + let mut impl_blocks = crate_impl_blocks.lookup_impl_blocks_for_trait(&trait_ref.trait_); + impl_blocks.any(|impl_block| &impl_block.target_ty(db) == trait_ref.self_ty()) +} + +fn def_crate(db: &impl HirDatabase, cur_krate: Crate, ty: &Ty) -> Option { match ty { Ty::Apply(a_ty) => match a_ty.ctor { TypeCtor::Adt(def_id) => def_id.krate(db), + TypeCtor::Bool => lang_item_lookup(db, cur_krate, "bool")?.krate(db), + TypeCtor::Char => lang_item_lookup(db, cur_krate, "char")?.krate(db), + TypeCtor::Float(UncertainFloatTy::Known(f)) => { + lang_item_lookup(db, cur_krate, f.ty_to_string())?.krate(db) + } + TypeCtor::Int(UncertainIntTy::Known(i)) => { + lang_item_lookup(db, cur_krate, i.ty_to_string())?.krate(db) + } + TypeCtor::Str => lang_item_lookup(db, cur_krate, "str")?.krate(db), _ => None, }, _ => None, @@ -150,8 +175,11 @@ impl Ty { // find in the end takes &self, we still do the autoderef step (just as // rustc does an autoderef and then autoref again). + let krate = resolver.module().map(|t| t.0.krate())?; for derefed_ty in self.autoderef(db) { - if let Some(result) = derefed_ty.iterate_inherent_methods(db, name, &mut callback) { + if let Some(result) = + derefed_ty.iterate_inherent_methods(db, name, krate, &mut callback) + { return Some(result); } if let Some(result) = @@ -208,9 +236,10 @@ impl Ty { &self, db: &impl HirDatabase, name: Option<&Name>, + krate: Crate, mut callback: impl FnMut(&Ty, Function) -> Option, ) -> Option { - let krate = match def_crate(db, self) { + let krate = match def_crate(db, krate, self) { Some(krate) => krate, None => return None, }; @@ -239,9 +268,10 @@ impl Ty { pub fn iterate_impl_items( self, db: &impl HirDatabase, + krate: Crate, mut callback: impl FnMut(ImplItem) -> Option, ) -> Option { - let krate = def_crate(db, &self)?; + let krate = def_crate(db, krate, &self)?; let impls = db.impls_in_crate(krate); for impl_block in impls.lookup_impl_blocks(&self) { -- cgit v1.2.3 From 8ebb20edce85b74eab0ed78ba5c4969ec733ad12 Mon Sep 17 00:00:00 2001 From: Marco Groppo Date: Wed, 17 Apr 2019 23:40:00 +0200 Subject: New krate() method in Resolver. Renamed Impl to ImplBlock. --- crates/ra_hir/src/ty/method_resolution.rs | 28 +++++++--------------------- 1 file changed, 7 insertions(+), 21 deletions(-) (limited to 'crates/ra_hir/src/ty/method_resolution.rs') diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index ba516313c..dca56a434 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -112,33 +112,19 @@ impl CrateImplBlocks { } } -/// Rudimentary check whether an impl exists for a given type and trait; this -/// will actually be done by chalk. -pub(crate) fn implements(db: &impl HirDatabase, trait_ref: TraitRef) -> bool { - // FIXME use all trait impls in the whole crate graph - let krate = trait_ref.trait_.module(db).krate(db); - let krate = match krate { - Some(krate) => krate, - None => return false, - }; - let crate_impl_blocks = db.impls_in_crate(krate); - let mut impl_blocks = crate_impl_blocks.lookup_impl_blocks_for_trait(&trait_ref.trait_); - impl_blocks.any(|impl_block| &impl_block.target_ty(db) == trait_ref.self_ty()) -} - -fn def_crate(db: &impl HirDatabase, cur_krate: Crate, ty: &Ty) -> Option { +fn def_crate(db: &impl HirDatabase, cur_crate: Crate, ty: &Ty) -> Option { match ty { Ty::Apply(a_ty) => match a_ty.ctor { TypeCtor::Adt(def_id) => def_id.krate(db), - TypeCtor::Bool => lang_item_lookup(db, cur_krate, "bool")?.krate(db), - TypeCtor::Char => lang_item_lookup(db, cur_krate, "char")?.krate(db), + TypeCtor::Bool => lang_item_lookup(db, cur_crate, "bool")?.krate(db), + TypeCtor::Char => lang_item_lookup(db, cur_crate, "char")?.krate(db), TypeCtor::Float(UncertainFloatTy::Known(f)) => { - lang_item_lookup(db, cur_krate, f.ty_to_string())?.krate(db) + lang_item_lookup(db, cur_crate, f.ty_to_string())?.krate(db) } TypeCtor::Int(UncertainIntTy::Known(i)) => { - lang_item_lookup(db, cur_krate, i.ty_to_string())?.krate(db) + lang_item_lookup(db, cur_crate, i.ty_to_string())?.krate(db) } - TypeCtor::Str => lang_item_lookup(db, cur_krate, "str")?.krate(db), + TypeCtor::Str => lang_item_lookup(db, cur_crate, "str")?.krate(db), _ => None, }, _ => None, @@ -175,7 +161,7 @@ impl Ty { // find in the end takes &self, we still do the autoderef step (just as // rustc does an autoderef and then autoref again). - let krate = resolver.module().map(|t| t.0.krate())?; + let krate = resolver.krate()?; for derefed_ty in self.autoderef(db) { if let Some(result) = derefed_ty.iterate_inherent_methods(db, name, krate, &mut callback) -- cgit v1.2.3 From 8ac3d1f9aa892fc891b69c7d8d00d39b9371d246 Mon Sep 17 00:00:00 2001 From: Marco Groppo Date: Thu, 18 Apr 2019 20:34:10 +0200 Subject: lang_item_lookup is now a salsa query. --- crates/ra_hir/src/ty/method_resolution.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'crates/ra_hir/src/ty/method_resolution.rs') diff --git a/crates/ra_hir/src/ty/method_resolution.rs b/crates/ra_hir/src/ty/method_resolution.rs index dca56a434..ea6e0dc0f 100644 --- a/crates/ra_hir/src/ty/method_resolution.rs +++ b/crates/ra_hir/src/ty/method_resolution.rs @@ -14,7 +14,6 @@ use crate::{ resolve::Resolver, traits::TraitItem, generics::HasGenericParams, - lang_item::lang_item_lookup, ty::primitive::{UncertainIntTy, UncertainFloatTy} }; use super::{TraitRef, Substs}; @@ -116,15 +115,15 @@ fn def_crate(db: &impl HirDatabase, cur_crate: Crate, ty: &Ty) -> Option match ty { Ty::Apply(a_ty) => match a_ty.ctor { TypeCtor::Adt(def_id) => def_id.krate(db), - TypeCtor::Bool => lang_item_lookup(db, cur_crate, "bool")?.krate(db), - TypeCtor::Char => lang_item_lookup(db, cur_crate, "char")?.krate(db), + TypeCtor::Bool => db.lang_item(cur_crate, "bool".into())?.krate(db), + TypeCtor::Char => db.lang_item(cur_crate, "char".into())?.krate(db), TypeCtor::Float(UncertainFloatTy::Known(f)) => { - lang_item_lookup(db, cur_crate, f.ty_to_string())?.krate(db) + db.lang_item(cur_crate, f.ty_to_string().into())?.krate(db) } TypeCtor::Int(UncertainIntTy::Known(i)) => { - lang_item_lookup(db, cur_crate, i.ty_to_string())?.krate(db) + db.lang_item(cur_crate, i.ty_to_string().into())?.krate(db) } - TypeCtor::Str => lang_item_lookup(db, cur_crate, "str")?.krate(db), + TypeCtor::Str => db.lang_item(cur_crate, "str".into())?.krate(db), _ => None, }, _ => None, -- cgit v1.2.3