This commit is contained in:
Aleksey Kladov 2019-11-21 13:32:03 +03:00
parent 24964ca58e
commit 621cf06156
5 changed files with 59 additions and 43 deletions

View file

@ -4,12 +4,13 @@
//! are splitting the hir.
use hir_def::{
AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ModuleDefId, StructId,
TypeAliasId, UnionId,
AdtId, AssocItemId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId,
ModuleDefId, StaticId, StructId, TypeAliasId, UnionId,
};
use crate::{
ty::TypableDef, Adt, AssocItem, DefWithBody, EnumVariant, GenericDef, ModuleDef, TypeAlias,
ty::TypableDef, Adt, AssocItem, Const, DefWithBody, EnumVariant, Function, GenericDef,
ModuleDef, Static, TypeAlias,
};
macro_rules! from_id {
@ -174,6 +175,22 @@ impl From<TypeAliasId> for TypableDef {
}
}
impl From<FunctionId> for TypableDef {
fn from(id: FunctionId) -> Self {
Function::from(id).into()
}
}
impl From<ConstId> for TypableDef {
fn from(id: ConstId) -> Self {
Const::from(id).into()
}
}
impl From<StaticId> for TypableDef {
fn from(id: StaticId) -> Self {
Static::from(id).into()
}
}
impl From<Adt> for GenericDefId {
fn from(id: Adt) -> Self {
match id {

View file

@ -8,8 +8,8 @@ use hir_def::{
generics::GenericParams,
nameres::CrateDefMap,
path::{Path, PathKind},
AdtId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ImplId, ModuleDefId,
StructId, TraitId, TypeAliasId,
AdtId, ConstId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId,
ImplId, ModuleDefId, StaticId, StructId, TraitId, TypeAliasId,
};
use hir_expand::name::{self, Name};
use rustc_hash::FxHashSet;
@ -18,8 +18,8 @@ use crate::{
code_model::Crate,
db::{DefDatabase, HirDatabase},
expr::{ExprScopes, PatId, ScopeId},
Adt, Const, Container, DefWithBody, EnumVariant, Function, GenericDef, HasBody, ImplBlock,
Local, MacroDef, Module, ModuleDef, PerNs, Static, Struct, Trait, TypeAlias,
Adt, Const, Container, DefWithBody, Function, GenericDef, ImplBlock, Local, MacroDef, Module,
ModuleDef, PerNs, Static, Trait, TypeAlias,
};
#[derive(Debug, Clone, Default)]
@ -79,11 +79,11 @@ pub(crate) enum ResolveValueResult {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub(crate) enum ValueNs {
LocalBinding(PatId),
Function(Function),
Const(Const),
Static(Static),
Struct(Struct),
EnumVariant(EnumVariant),
FunctionId(FunctionId),
ConstId(ConstId),
StaticId(StaticId),
StructId(StructId),
EnumVariantId(EnumVariantId),
}
impl Resolver {
@ -266,13 +266,11 @@ impl Resolver {
return match idx {
None => {
let value = match module_def.take_values()? {
ModuleDefId::FunctionId(it) => ValueNs::Function(it.into()),
ModuleDefId::AdtId(AdtId::StructId(it)) => {
ValueNs::Struct(it.into())
}
ModuleDefId::EnumVariantId(it) => ValueNs::EnumVariant(it.into()),
ModuleDefId::ConstId(it) => ValueNs::Const(it.into()),
ModuleDefId::StaticId(it) => ValueNs::Static(it.into()),
ModuleDefId::FunctionId(it) => ValueNs::FunctionId(it),
ModuleDefId::AdtId(AdtId::StructId(it)) => ValueNs::StructId(it),
ModuleDefId::EnumVariantId(it) => ValueNs::EnumVariantId(it),
ModuleDefId::ConstId(it) => ValueNs::ConstId(it),
ModuleDefId::StaticId(it) => ValueNs::StaticId(it),
ModuleDefId::AdtId(AdtId::EnumId(_))
| ModuleDefId::AdtId(AdtId::UnionId(_))
@ -496,23 +494,23 @@ impl Scope {
// needs arbitrary_self_types to be a method... or maybe move to the def?
pub(crate) fn resolver_for_expr(
db: &impl HirDatabase,
owner: DefWithBody,
owner: DefWithBodyId,
expr_id: ExprId,
) -> Resolver {
let scopes = owner.expr_scopes(db);
let scopes = db.expr_scopes(owner);
resolver_for_scope(db, owner, scopes.scope_for(expr_id))
}
pub(crate) fn resolver_for_scope(
db: &impl HirDatabase,
owner: DefWithBody,
owner: DefWithBodyId,
scope_id: Option<ScopeId>,
) -> Resolver {
let mut r = owner.resolver(db);
let scopes = owner.expr_scopes(db);
let mut r = DefWithBody::from(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() {
r = r.push_expr_scope(owner.into(), Arc::clone(&scopes), scope);
r = r.push_expr_scope(owner, Arc::clone(&scopes), scope);
}
r
}

View file

@ -160,7 +160,7 @@ impl SourceAnalyzer {
None => scope_for(&scopes, &source_map, node),
Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)),
};
let resolver = resolver_for_scope(db, def, scope);
let resolver = resolver_for_scope(db, def.into(), scope);
SourceAnalyzer {
resolver,
body_owner: Some(def),
@ -260,11 +260,11 @@ impl SourceAnalyzer {
let var = Local { parent: self.body_owner?, pat_id };
PathResolution::Local(var)
}
ValueNs::Function(it) => PathResolution::Def(it.into()),
ValueNs::Const(it) => PathResolution::Def(it.into()),
ValueNs::Static(it) => PathResolution::Def(it.into()),
ValueNs::Struct(it) => PathResolution::Def(it.into()),
ValueNs::EnumVariant(it) => PathResolution::Def(it.into()),
ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()),
ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()),
ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()),
ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()),
ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
};
Some(res)
});

View file

@ -187,7 +187,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
}
Expr::Path(p) => {
// FIXME this could be more efficient...
let resolver = resolver_for_expr(self.db, self.owner, tgt_expr);
let resolver = resolver_for_expr(self.db, self.owner.into(), tgt_expr);
self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown)
}
Expr::Continue => Ty::simple(TypeCtor::Never),

View file

@ -8,7 +8,7 @@ use crate::{
generics::HasGenericParams,
resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs},
ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk},
AssocItem, Container, Name, Path,
AssocItem, Container, Function, Name, Path,
};
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
@ -60,11 +60,11 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
let ty = self.resolve_ty_as_possible(&mut vec![], ty);
return Some(ty);
}
ValueNs::Function(it) => it.into(),
ValueNs::Const(it) => it.into(),
ValueNs::Static(it) => it.into(),
ValueNs::Struct(it) => it.into(),
ValueNs::EnumVariant(it) => it.into(),
ValueNs::FunctionId(it) => it.into(),
ValueNs::ConstId(it) => it.into(),
ValueNs::StaticId(it) => it.into(),
ValueNs::StructId(it) => it.into(),
ValueNs::EnumVariantId(it) => it.into(),
};
let mut ty = self.db.type_for_def(typable, Namespace::Values);
@ -160,8 +160,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
AssocItem::TypeAlias(_) => None,
})?;
let def = match item {
AssocItem::Function(f) => ValueNs::Function(f),
AssocItem::Const(c) => ValueNs::Const(c),
AssocItem::Function(f) => ValueNs::FunctionId(f.id),
AssocItem::Const(c) => ValueNs::ConstId(c.id),
AssocItem::TypeAlias(_) => unreachable!(),
};
let substs = Substs::build_for_def(self.db, item)
@ -193,8 +193,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
method_resolution::LookupMode::Path,
move |_ty, item| {
let def = match item {
AssocItem::Function(f) => ValueNs::Function(f),
AssocItem::Const(c) => ValueNs::Const(c),
AssocItem::Function(f) => ValueNs::FunctionId(f.id),
AssocItem::Const(c) => ValueNs::ConstId(c.id),
AssocItem::TypeAlias(_) => unreachable!(),
};
let substs = match item.container(self.db) {
@ -224,7 +224,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
}
fn find_self_types(&self, def: &ValueNs, actual_def_ty: Ty) -> Option<Substs> {
if let ValueNs::Function(func) = def {
if let ValueNs::FunctionId(func) = def {
let func = Function::from(*func);
// We only do the infer if parent has generic params
let gen = func.generic_params(self.db);
if gen.count_parent_params() == 0 {