lang_item_lookup is now a salsa query.
This commit is contained in:
parent
8ebb20edce
commit
8ac3d1f9aa
@ -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<LangItems>;
|
||||
|
||||
#[salsa::invoke(crate::lang_item::LangItems::lang_item_query)]
|
||||
fn lang_item(&self, start_crate: Crate, item: SmolStr) -> Option<LangItemTarget>;
|
||||
}
|
||||
|
||||
#[salsa::query_group(HirDatabaseStorage)]
|
||||
|
@ -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<LangItems> {
|
||||
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<LangItemTarget> {
|
||||
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<LangItemTarget> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -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<Crate>
|
||||
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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user