New krate() method in Resolver.

Renamed Impl to ImplBlock.
This commit is contained in:
Marco Groppo 2019-04-17 23:40:00 +02:00
parent e85ee60c42
commit 8ebb20edce
5 changed files with 49 additions and 59 deletions

View File

@ -11,7 +11,7 @@ use crate::{
pub enum LangItemTarget {
Enum(Enum),
Function(Function),
Impl(ImplBlock),
ImplBlock(ImplBlock),
Static(Static),
Struct(Struct),
Trait(Trait),
@ -22,7 +22,7 @@ impl LangItemTarget {
match self {
LangItemTarget::Enum(e) => e.module(db).krate(db),
LangItemTarget::Function(f) => f.module(db).krate(db),
LangItemTarget::Impl(i) => i.module().krate(db),
LangItemTarget::ImplBlock(i) => i.module().krate(db),
LangItemTarget::Static(s) => s.module(db).krate(db),
LangItemTarget::Struct(s) => s.module(db).krate(db),
LangItemTarget::Trait(t) => t.module(db).krate(db),
@ -65,7 +65,7 @@ impl LangItems {
.nth(0);
if let Some(lang_item_name) = lang_item_name {
let imp = ImplBlock::from_id(*module, impl_id);
self.items.entry(lang_item_name).or_insert(LangItemTarget::Impl(imp));
self.items.entry(lang_item_name).or_insert(LangItemTarget::ImplBlock(imp));
}
}

View File

@ -5,13 +5,15 @@ use rustc_hash::FxHashMap;
use crate::{
ModuleDef,
code_model_api::Crate,
db::HirDatabase,
name::{Name, KnownName},
nameres::{PerNs, CrateDefMap, CrateModuleId},
generics::GenericParams,
expr::{scope::{ExprScopes, ScopeId}, PatId},
impl_block::ImplBlock,
path::Path, Trait
path::Path,
Trait
};
#[derive(Debug, Clone, Default)]
@ -190,13 +192,17 @@ impl Resolver {
.flatten()
}
pub(crate) fn module(&self) -> Option<(&CrateDefMap, CrateModuleId)> {
fn module(&self) -> Option<(&CrateDefMap, CrateModuleId)> {
self.scopes.iter().rev().find_map(|scope| match scope {
Scope::ModuleScope(m) => Some((&*m.crate_def_map, m.module_id)),
_ => None,
})
}
pub(crate) fn krate(&self) -> Option<Crate> {
self.module().map(|t| t.0.krate())
}
}
impl Resolver {

View File

@ -462,7 +462,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
let remaining_index = remaining_index.unwrap_or(path.segments.len());
let mut actual_def_ty: Option<Ty> = None;
let krate = resolver.module().map(|t| t.0.krate());
let krate = resolver.krate()?;
// resolve intermediate segments
for (i, segment) in path.segments[remaining_index..].iter().enumerate() {
let ty = match resolved {
@ -504,38 +504,36 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
actual_def_ty = Some(ty.clone());
let item: crate::ModuleDef = krate.and_then(|k| {
ty.iterate_impl_items(self.db, k, |item| {
let matching_def: Option<crate::ModuleDef> = match item {
crate::ImplItem::Method(func) => {
let sig = func.signature(self.db);
if segment.name == *sig.name() {
Some(func.into())
} else {
None
}
let item: crate::ModuleDef = ty.iterate_impl_items(self.db, krate, |item| {
let matching_def: Option<crate::ModuleDef> = match item {
crate::ImplItem::Method(func) => {
let sig = func.signature(self.db);
if segment.name == *sig.name() {
Some(func.into())
} else {
None
}
crate::ImplItem::Const(konst) => {
let sig = konst.signature(self.db);
if segment.name == *sig.name() {
Some(konst.into())
} else {
None
}
}
// FIXME: Resolve associated types
crate::ImplItem::TypeAlias(_) => None,
};
match matching_def {
Some(_) => {
self.write_assoc_resolution(id, item);
return matching_def;
}
None => None,
}
})
crate::ImplItem::Const(konst) => {
let sig = konst.signature(self.db);
if segment.name == *sig.name() {
Some(konst.into())
} else {
None
}
}
// FIXME: Resolve associated types
crate::ImplItem::TypeAlias(_) => None,
};
match matching_def {
Some(_) => {
self.write_assoc_resolution(id, item);
return matching_def;
}
None => None,
}
})?;
resolved = Resolution::Def(item.into());

View File

@ -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<Crate> {
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_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)

View File

@ -39,7 +39,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
hir::ModuleDef::Struct(s) => {
let ty = s.ty(ctx.db);
let krate = ctx.module.and_then(|m| m.krate(ctx.db));
krate.map_or((), |krate| {
if let Some(krate) = krate {
ty.iterate_impl_items(ctx.db, krate, |item| {
match item {
hir::ImplItem::Method(func) => {
@ -53,7 +53,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
}
None::<()>
});
});
}
}
_ => return,
};