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/db.rs | 7 +++-- crates/ra_hir/src/lang_item.rs | 48 +++++++++++++++---------------- crates/ra_hir/src/ty/method_resolution.rs | 11 ++++--- 3 files changed, 34 insertions(+), 32 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir/src/db.rs b/crates/ra_hir/src/db.rs index b27efcbe3..8af0a3176 100644 --- a/crates/ra_hir/src/db.rs +++ b/crates/ra_hir/src/db.rs @@ -1,6 +1,6 @@ use std::sync::Arc; -use ra_syntax::{SyntaxNode, TreeArc, SourceFile, ast}; +use ra_syntax::{SyntaxNode, TreeArc, SourceFile, SmolStr, ast}; use ra_db::{SourceDatabase, salsa}; use crate::{ @@ -16,7 +16,7 @@ use crate::{ generics::{GenericParams, GenericDef}, type_ref::TypeRef, traits::TraitData, Trait, ty::TraitRef, - lang_item::LangItems, + lang_item::{LangItems, LangItemTarget}, ids }; @@ -104,6 +104,9 @@ pub trait DefDatabase: SourceDatabase { #[salsa::invoke(crate::lang_item::LangItems::lang_items_query)] fn lang_items(&self, krate: Crate) -> Arc; + + #[salsa::invoke(crate::lang_item::LangItems::lang_item_query)] + fn lang_item(&self, start_crate: Crate, item: SmolStr) -> Option; } #[salsa::query_group(HirDatabaseStorage)] diff --git a/crates/ra_hir/src/lang_item.rs b/crates/ra_hir/src/lang_item.rs index a25d419e7..5f3f91cba 100644 --- a/crates/ra_hir/src/lang_item.rs +++ b/crates/ra_hir/src/lang_item.rs @@ -40,7 +40,7 @@ impl LangItems { self.items.get(item) } - /// Salsa query. This will query a specific crate for lang items. + /// Salsa query. This will look for lang items in a specific crate. pub(crate) fn lang_items_query(db: &impl DefDatabase, krate: Crate) -> Arc { let mut lang_items = LangItems { items: FxHashMap::default() }; @@ -51,6 +51,29 @@ impl LangItems { Arc::new(lang_items) } + /// Salsa query. Look for a lang item, starting from the specified crate and recursively + /// traversing its dependencies. + pub(crate) fn lang_item_query( + db: &impl DefDatabase, + start_crate: Crate, + item: SmolStr, + ) -> Option { + let lang_items = db.lang_items(start_crate); + let start_crate_target = lang_items.items.get(&item); + if let Some(target) = start_crate_target { + Some(*target) + } else { + for dep in start_crate.dependencies(db) { + let dep_crate = dep.krate; + let dep_target = db.lang_item(dep_crate, item.clone()); + if dep_target.is_some() { + return dep_target; + } + } + None + } + } + fn collect_lang_items_recursive(&mut self, db: &impl DefDatabase, module: &Module) { // Look for impl targets let (impl_blocks, source_map) = db.impls_in_module_with_source_map(module.clone()); @@ -77,26 +100,3 @@ impl LangItems { } } } - -/// Look for a lang item, starting from the specified crate and recursively traversing its -/// dependencies. -pub(crate) fn lang_item_lookup( - db: &impl DefDatabase, - start_krate: Crate, - item: &str, -) -> Option { - let lang_items = db.lang_items(start_krate); - let start_krate_target = lang_items.items.get(item); - if start_krate_target.is_some() { - start_krate_target.map(|t| *t) - } else { - for dep in start_krate.dependencies(db) { - let dep_krate = dep.krate; - let dep_target = lang_item_lookup(db, dep_krate, item); - if dep_target.is_some() { - return dep_target; - } - } - None - } -} 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