This commit is contained in:
Aleksey Kladov 2019-11-21 14:13:49 +03:00
parent 621cf06156
commit 061e6c77b5
6 changed files with 108 additions and 78 deletions

View file

@ -829,7 +829,7 @@ impl Trait {
}
fn direct_super_traits(self, db: &impl HirDatabase) -> Vec<Trait> {
let resolver = self.resolver(db);
let resolver = self.id.resolver(db);
// returning the iterator directly doesn't easily work because of
// lifetime problems, but since there usually shouldn't be more than a
// few direct traits this should be fine (we could even use some kind of

View file

@ -122,7 +122,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
let std_result_path = known::std_result_result();
let resolver = self.func.resolver(db);
let resolver = self.func.id.resolver(db);
let std_result_enum = match resolver.resolve_known_enum(db, &std_result_path) {
Some(it) => it,
_ => return,

View file

@ -27,12 +27,12 @@ impl ImplBlock {
}
pub fn target_ty(&self, db: &impl HirDatabase) -> Ty {
Ty::from_hir(db, &self.resolver(db), &self.target_type(db))
Ty::from_hir(db, &self.id.resolver(db), &self.target_type(db))
}
pub fn target_trait_ref(&self, db: &impl HirDatabase) -> Option<TraitRef> {
let target_ty = self.target_ty(db);
TraitRef::from_hir(db, &self.resolver(db), &self.target_trait(db)?, Some(target_ty))
TraitRef::from_hir(db, &self.id.resolver(db), &self.target_trait(db)?, Some(target_ty))
}
pub fn items(&self, db: &impl DefDatabase) -> Vec<AssocItem> {

View file

@ -8,18 +8,18 @@ use hir_def::{
generics::GenericParams,
nameres::CrateDefMap,
path::{Path, PathKind},
AdtId, ConstId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId,
ImplId, ModuleDefId, StaticId, StructId, TraitId, TypeAliasId,
AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId,
FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId,
TypeAliasId, UnionId,
};
use hir_expand::name::{self, Name};
use rustc_hash::FxHashSet;
use crate::{
code_model::Crate,
db::{DefDatabase, HirDatabase},
db::HirDatabase,
expr::{ExprScopes, PatId, ScopeId},
Adt, Const, Container, DefWithBody, Function, GenericDef, ImplBlock, Local, MacroDef, Module,
ModuleDef, PerNs, Static, Trait, TypeAlias,
Adt, DefWithBody, GenericDef, ImplBlock, Local, MacroDef, ModuleDef, PerNs,
};
#[derive(Debug, Clone, Default)]
@ -506,7 +506,7 @@ pub(crate) fn resolver_for_scope(
owner: DefWithBodyId,
scope_id: Option<ScopeId>,
) -> Resolver {
let mut r = DefWithBody::from(owner).resolver(db);
let mut r = owner.resolver(db);
let scopes = db.expr_scopes(owner);
let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>();
for scope in scope_chain.into_iter().rev() {
@ -517,100 +517,130 @@ pub(crate) fn resolver_for_scope(
pub(crate) trait HasResolver {
/// Builds a resolver for type references inside this def.
fn resolver(self, db: &impl DefDatabase) -> Resolver;
fn resolver(self, db: &impl DefDatabase2) -> Resolver;
}
impl HasResolver for Module {
fn resolver(self, db: &impl DefDatabase) -> Resolver {
let def_map = db.crate_def_map(self.id.krate);
Resolver::default().push_module_scope(def_map, self.id.module_id)
impl HasResolver for ModuleId {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
let def_map = db.crate_def_map(self.krate);
Resolver::default().push_module_scope(def_map, self.module_id)
}
}
impl HasResolver for Trait {
fn resolver(self, db: &impl DefDatabase) -> Resolver {
self.module(db).resolver(db).push_generic_params_scope(db, self.id.into())
impl HasResolver for TraitId {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
self.module(db).resolver(db).push_generic_params_scope(db, self.into())
}
}
impl<T: Into<Adt>> HasResolver for T {
fn resolver(self, db: &impl DefDatabase) -> Resolver {
let def = self.into();
def.module(db)
impl HasResolver for AdtId {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
let module = match self {
AdtId::StructId(it) => it.0.module(db),
AdtId::UnionId(it) => it.0.module(db),
AdtId::EnumId(it) => it.module(db),
};
module
.resolver(db)
.push_generic_params_scope(db, def.into())
.push_scope(Scope::AdtScope(def.into()))
.push_generic_params_scope(db, self.into())
.push_scope(Scope::AdtScope(self.into()))
}
}
impl HasResolver for Function {
fn resolver(self, db: &impl DefDatabase) -> Resolver {
self.container(db)
.map(|c| c.resolver(db))
.unwrap_or_else(|| self.module(db).resolver(db))
.push_generic_params_scope(db, self.id.into())
impl HasResolver for StructId {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
AdtId::from(self).resolver(db)
}
}
impl HasResolver for DefWithBody {
fn resolver(self, db: &impl DefDatabase) -> Resolver {
impl HasResolver for UnionId {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
AdtId::from(self).resolver(db)
}
}
impl HasResolver for EnumId {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
AdtId::from(self).resolver(db)
}
}
impl HasResolver for FunctionId {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into())
}
}
impl HasResolver for DefWithBodyId {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
match self {
DefWithBody::Const(c) => c.resolver(db),
DefWithBody::Function(f) => f.resolver(db),
DefWithBody::Static(s) => s.resolver(db),
DefWithBodyId::ConstId(c) => c.resolver(db),
DefWithBodyId::FunctionId(f) => f.resolver(db),
DefWithBodyId::StaticId(s) => s.resolver(db),
}
}
}
impl HasResolver for Const {
fn resolver(self, db: &impl DefDatabase) -> Resolver {
self.container(db).map(|c| c.resolver(db)).unwrap_or_else(|| self.module(db).resolver(db))
impl HasResolver for ConstId {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
self.lookup(db).container.resolver(db)
}
}
impl HasResolver for Static {
fn resolver(self, db: &impl DefDatabase) -> Resolver {
impl HasResolver for StaticId {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
self.module(db).resolver(db)
}
}
impl HasResolver for TypeAlias {
fn resolver(self, db: &impl DefDatabase) -> Resolver {
self.container(db)
.map(|ib| ib.resolver(db))
.unwrap_or_else(|| self.module(db).resolver(db))
.push_generic_params_scope(db, self.id.into())
impl HasResolver for TypeAliasId {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into())
}
}
impl HasResolver for Container {
fn resolver(self, db: &impl DefDatabase) -> Resolver {
impl HasResolver for ContainerId {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
match self {
Container::Trait(trait_) => trait_.resolver(db),
Container::ImplBlock(impl_block) => impl_block.resolver(db),
ContainerId::TraitId(it) => it.resolver(db),
ContainerId::ImplId(it) => it.resolver(db),
ContainerId::ModuleId(it) => it.resolver(db),
}
}
}
impl HasResolver for GenericDefId {
fn resolver(self, db: &impl DefDatabase2) -> crate::Resolver {
match self {
GenericDefId::FunctionId(inner) => inner.resolver(db),
GenericDefId::AdtId(adt) => adt.resolver(db),
GenericDefId::TraitId(inner) => inner.resolver(db),
GenericDefId::TypeAliasId(inner) => inner.resolver(db),
GenericDefId::ImplId(inner) => inner.resolver(db),
GenericDefId::EnumVariantId(inner) => inner.parent.resolver(db),
GenericDefId::ConstId(inner) => inner.resolver(db),
}
}
}
impl HasResolver for ImplId {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
self.module(db)
.resolver(db)
.push_generic_params_scope(db, self.into())
.push_impl_block_scope(self)
}
}
impl HasResolver for GenericDef {
fn resolver(self, db: &impl DefDatabase) -> crate::Resolver {
match self {
GenericDef::Function(inner) => inner.resolver(db),
GenericDef::Adt(adt) => adt.resolver(db),
GenericDef::Trait(inner) => inner.resolver(db),
GenericDef::TypeAlias(inner) => inner.resolver(db),
GenericDef::ImplBlock(inner) => inner.resolver(db),
GenericDef::EnumVariant(inner) => inner.parent_enum(db).resolver(db),
GenericDef::Const(inner) => inner.resolver(db),
}
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
GenericDefId::from(self).resolver(db)
}
}
impl HasResolver for ImplBlock {
fn resolver(self, db: &impl DefDatabase) -> Resolver {
self.module(db)
.resolver(db)
.push_generic_params_scope(db, self.id.into())
.push_impl_block_scope(self.id)
impl HasResolver for DefWithBody {
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
DefWithBodyId::from(self).resolver(db)
}
}

View file

@ -35,19 +35,19 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -
match (node.value) {
ast::Module(it) => {
let src = node.with_value(it);
Some(crate::Module::from_declaration(db, src)?.resolver(db))
Some(crate::Module::from_declaration(db, src)?.id.resolver(db))
},
ast::SourceFile(it) => {
let src = node.with_value(crate::ModuleSource::SourceFile(it));
Some(crate::Module::from_definition(db, src)?.resolver(db))
Some(crate::Module::from_definition(db, src)?.id.resolver(db))
},
ast::StructDef(it) => {
let src = node.with_value(it);
Some(Struct::from_source(db, src)?.resolver(db))
Some(Struct::from_source(db, src)?.id.resolver(db))
},
ast::EnumDef(it) => {
let src = node.with_value(it);
Some(Enum::from_source(db, src)?.resolver(db))
Some(Enum::from_source(db, src)?.id.resolver(db))
},
_ => match node.value.kind() {
FN_DEF | CONST_DEF | STATIC_DEF => {

View file

@ -553,8 +553,8 @@ pub(crate) fn callable_item_sig(db: &impl HirDatabase, def: CallableDef) -> FnSi
pub(crate) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty {
let parent_def = field.parent_def(db);
let resolver = match parent_def {
VariantDef::Struct(it) => it.resolver(db),
VariantDef::EnumVariant(it) => it.parent_enum(db).resolver(db),
VariantDef::Struct(it) => it.id.resolver(db),
VariantDef::EnumVariant(it) => it.parent.id.resolver(db),
};
let var_data = parent_def.variant_data(db);
let type_ref = &var_data.fields().unwrap()[field.id].type_ref;
@ -623,7 +623,7 @@ pub(crate) fn generic_defaults_query(db: &impl HirDatabase, def: GenericDef) ->
fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig {
let data = def.data(db);
let resolver = def.resolver(db);
let resolver = def.id.resolver(db);
let params = data.params().iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::<Vec<_>>();
let ret = Ty::from_hir(db, &resolver, data.ret_type());
FnSig::from_params_and_return(params, ret)
@ -640,7 +640,7 @@ fn type_for_fn(db: &impl HirDatabase, def: Function) -> Ty {
/// Build the declared type of a const.
fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty {
let data = def.data(db);
let resolver = def.resolver(db);
let resolver = def.id.resolver(db);
Ty::from_hir(db, &resolver, data.type_ref())
}
@ -648,7 +648,7 @@ fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty {
/// Build the declared type of a static.
fn type_for_static(db: &impl HirDatabase, def: Static) -> Ty {
let data = def.data(db);
let resolver = def.resolver(db);
let resolver = def.id.resolver(db);
Ty::from_hir(db, &resolver, data.type_ref())
}
@ -700,7 +700,7 @@ fn fn_sig_for_struct_constructor(db: &impl HirDatabase, def: Struct) -> FnSig {
Some(fields) => fields,
None => panic!("fn_sig_for_struct_constructor called on unit struct"),
};
let resolver = def.resolver(db);
let resolver = def.id.resolver(db);
let params = fields
.iter()
.map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref))
@ -726,7 +726,7 @@ fn fn_sig_for_enum_variant_constructor(db: &impl HirDatabase, def: EnumVariant)
Some(fields) => fields,
None => panic!("fn_sig_for_enum_variant_constructor called for unit variant"),
};
let resolver = def.parent_enum(db).resolver(db);
let resolver = def.parent.id.resolver(db);
let params = fields
.iter()
.map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref))
@ -755,7 +755,7 @@ fn type_for_adt(db: &impl HirDatabase, adt: impl Into<Adt> + HasGenericParams) -
fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty {
let generics = t.generic_params(db);
let resolver = t.resolver(db);
let resolver = t.id.resolver(db);
let type_ref = t.type_ref(db);
let substs = Substs::identity(&generics);
let inner = Ty::from_hir(db, &resolver, &type_ref.unwrap_or(TypeRef::Error));