mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
Decouple
This commit is contained in:
parent
24964ca58e
commit
621cf06156
@ -4,12 +4,13 @@
|
|||||||
//! are splitting the hir.
|
//! are splitting the hir.
|
||||||
|
|
||||||
use hir_def::{
|
use hir_def::{
|
||||||
AdtId, AssocItemId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ModuleDefId, StructId,
|
AdtId, AssocItemId, ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId,
|
||||||
TypeAliasId, UnionId,
|
ModuleDefId, StaticId, StructId, TypeAliasId, UnionId,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
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 {
|
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 {
|
impl From<Adt> for GenericDefId {
|
||||||
fn from(id: Adt) -> Self {
|
fn from(id: Adt) -> Self {
|
||||||
match id {
|
match id {
|
||||||
|
@ -8,8 +8,8 @@ use hir_def::{
|
|||||||
generics::GenericParams,
|
generics::GenericParams,
|
||||||
nameres::CrateDefMap,
|
nameres::CrateDefMap,
|
||||||
path::{Path, PathKind},
|
path::{Path, PathKind},
|
||||||
AdtId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, GenericDefId, ImplId, ModuleDefId,
|
AdtId, ConstId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId,
|
||||||
StructId, TraitId, TypeAliasId,
|
ImplId, ModuleDefId, StaticId, StructId, TraitId, TypeAliasId,
|
||||||
};
|
};
|
||||||
use hir_expand::name::{self, Name};
|
use hir_expand::name::{self, Name};
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
@ -18,8 +18,8 @@ use crate::{
|
|||||||
code_model::Crate,
|
code_model::Crate,
|
||||||
db::{DefDatabase, HirDatabase},
|
db::{DefDatabase, HirDatabase},
|
||||||
expr::{ExprScopes, PatId, ScopeId},
|
expr::{ExprScopes, PatId, ScopeId},
|
||||||
Adt, Const, Container, DefWithBody, EnumVariant, Function, GenericDef, HasBody, ImplBlock,
|
Adt, Const, Container, DefWithBody, Function, GenericDef, ImplBlock, Local, MacroDef, Module,
|
||||||
Local, MacroDef, Module, ModuleDef, PerNs, Static, Struct, Trait, TypeAlias,
|
ModuleDef, PerNs, Static, Trait, TypeAlias,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
@ -79,11 +79,11 @@ pub(crate) enum ResolveValueResult {
|
|||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||||
pub(crate) enum ValueNs {
|
pub(crate) enum ValueNs {
|
||||||
LocalBinding(PatId),
|
LocalBinding(PatId),
|
||||||
Function(Function),
|
FunctionId(FunctionId),
|
||||||
Const(Const),
|
ConstId(ConstId),
|
||||||
Static(Static),
|
StaticId(StaticId),
|
||||||
Struct(Struct),
|
StructId(StructId),
|
||||||
EnumVariant(EnumVariant),
|
EnumVariantId(EnumVariantId),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Resolver {
|
impl Resolver {
|
||||||
@ -266,13 +266,11 @@ impl Resolver {
|
|||||||
return match idx {
|
return match idx {
|
||||||
None => {
|
None => {
|
||||||
let value = match module_def.take_values()? {
|
let value = match module_def.take_values()? {
|
||||||
ModuleDefId::FunctionId(it) => ValueNs::Function(it.into()),
|
ModuleDefId::FunctionId(it) => ValueNs::FunctionId(it),
|
||||||
ModuleDefId::AdtId(AdtId::StructId(it)) => {
|
ModuleDefId::AdtId(AdtId::StructId(it)) => ValueNs::StructId(it),
|
||||||
ValueNs::Struct(it.into())
|
ModuleDefId::EnumVariantId(it) => ValueNs::EnumVariantId(it),
|
||||||
}
|
ModuleDefId::ConstId(it) => ValueNs::ConstId(it),
|
||||||
ModuleDefId::EnumVariantId(it) => ValueNs::EnumVariant(it.into()),
|
ModuleDefId::StaticId(it) => ValueNs::StaticId(it),
|
||||||
ModuleDefId::ConstId(it) => ValueNs::Const(it.into()),
|
|
||||||
ModuleDefId::StaticId(it) => ValueNs::Static(it.into()),
|
|
||||||
|
|
||||||
ModuleDefId::AdtId(AdtId::EnumId(_))
|
ModuleDefId::AdtId(AdtId::EnumId(_))
|
||||||
| ModuleDefId::AdtId(AdtId::UnionId(_))
|
| ModuleDefId::AdtId(AdtId::UnionId(_))
|
||||||
@ -496,23 +494,23 @@ impl Scope {
|
|||||||
// needs arbitrary_self_types to be a method... or maybe move to the def?
|
// needs arbitrary_self_types to be a method... or maybe move to the def?
|
||||||
pub(crate) fn resolver_for_expr(
|
pub(crate) fn resolver_for_expr(
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
owner: DefWithBody,
|
owner: DefWithBodyId,
|
||||||
expr_id: ExprId,
|
expr_id: ExprId,
|
||||||
) -> Resolver {
|
) -> Resolver {
|
||||||
let scopes = owner.expr_scopes(db);
|
let scopes = db.expr_scopes(owner);
|
||||||
resolver_for_scope(db, owner, scopes.scope_for(expr_id))
|
resolver_for_scope(db, owner, scopes.scope_for(expr_id))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn resolver_for_scope(
|
pub(crate) fn resolver_for_scope(
|
||||||
db: &impl HirDatabase,
|
db: &impl HirDatabase,
|
||||||
owner: DefWithBody,
|
owner: DefWithBodyId,
|
||||||
scope_id: Option<ScopeId>,
|
scope_id: Option<ScopeId>,
|
||||||
) -> Resolver {
|
) -> Resolver {
|
||||||
let mut r = owner.resolver(db);
|
let mut r = DefWithBody::from(owner).resolver(db);
|
||||||
let scopes = owner.expr_scopes(db);
|
let scopes = db.expr_scopes(owner);
|
||||||
let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>();
|
let scope_chain = scopes.scope_chain(scope_id).collect::<Vec<_>>();
|
||||||
for scope in scope_chain.into_iter().rev() {
|
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
|
r
|
||||||
}
|
}
|
||||||
|
@ -160,7 +160,7 @@ impl SourceAnalyzer {
|
|||||||
None => scope_for(&scopes, &source_map, node),
|
None => scope_for(&scopes, &source_map, node),
|
||||||
Some(offset) => scope_for_offset(&scopes, &source_map, node.with_value(offset)),
|
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 {
|
SourceAnalyzer {
|
||||||
resolver,
|
resolver,
|
||||||
body_owner: Some(def),
|
body_owner: Some(def),
|
||||||
@ -260,11 +260,11 @@ impl SourceAnalyzer {
|
|||||||
let var = Local { parent: self.body_owner?, pat_id };
|
let var = Local { parent: self.body_owner?, pat_id };
|
||||||
PathResolution::Local(var)
|
PathResolution::Local(var)
|
||||||
}
|
}
|
||||||
ValueNs::Function(it) => PathResolution::Def(it.into()),
|
ValueNs::FunctionId(it) => PathResolution::Def(Function::from(it).into()),
|
||||||
ValueNs::Const(it) => PathResolution::Def(it.into()),
|
ValueNs::ConstId(it) => PathResolution::Def(Const::from(it).into()),
|
||||||
ValueNs::Static(it) => PathResolution::Def(it.into()),
|
ValueNs::StaticId(it) => PathResolution::Def(Static::from(it).into()),
|
||||||
ValueNs::Struct(it) => PathResolution::Def(it.into()),
|
ValueNs::StructId(it) => PathResolution::Def(Struct::from(it).into()),
|
||||||
ValueNs::EnumVariant(it) => PathResolution::Def(it.into()),
|
ValueNs::EnumVariantId(it) => PathResolution::Def(EnumVariant::from(it).into()),
|
||||||
};
|
};
|
||||||
Some(res)
|
Some(res)
|
||||||
});
|
});
|
||||||
|
@ -187,7 +187,7 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||||||
}
|
}
|
||||||
Expr::Path(p) => {
|
Expr::Path(p) => {
|
||||||
// FIXME this could be more efficient...
|
// 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)
|
self.infer_path(&resolver, p, tgt_expr.into()).unwrap_or(Ty::Unknown)
|
||||||
}
|
}
|
||||||
Expr::Continue => Ty::simple(TypeCtor::Never),
|
Expr::Continue => Ty::simple(TypeCtor::Never),
|
||||||
|
@ -8,7 +8,7 @@ use crate::{
|
|||||||
generics::HasGenericParams,
|
generics::HasGenericParams,
|
||||||
resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs},
|
resolve::{ResolveValueResult, Resolver, TypeNs, ValueNs},
|
||||||
ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk},
|
ty::{method_resolution, Namespace, Substs, Ty, TypableDef, TypeWalk},
|
||||||
AssocItem, Container, Name, Path,
|
AssocItem, Container, Function, Name, Path,
|
||||||
};
|
};
|
||||||
|
|
||||||
impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
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);
|
let ty = self.resolve_ty_as_possible(&mut vec![], ty);
|
||||||
return Some(ty);
|
return Some(ty);
|
||||||
}
|
}
|
||||||
ValueNs::Function(it) => it.into(),
|
ValueNs::FunctionId(it) => it.into(),
|
||||||
ValueNs::Const(it) => it.into(),
|
ValueNs::ConstId(it) => it.into(),
|
||||||
ValueNs::Static(it) => it.into(),
|
ValueNs::StaticId(it) => it.into(),
|
||||||
ValueNs::Struct(it) => it.into(),
|
ValueNs::StructId(it) => it.into(),
|
||||||
ValueNs::EnumVariant(it) => it.into(),
|
ValueNs::EnumVariantId(it) => it.into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut ty = self.db.type_for_def(typable, Namespace::Values);
|
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,
|
AssocItem::TypeAlias(_) => None,
|
||||||
})?;
|
})?;
|
||||||
let def = match item {
|
let def = match item {
|
||||||
AssocItem::Function(f) => ValueNs::Function(f),
|
AssocItem::Function(f) => ValueNs::FunctionId(f.id),
|
||||||
AssocItem::Const(c) => ValueNs::Const(c),
|
AssocItem::Const(c) => ValueNs::ConstId(c.id),
|
||||||
AssocItem::TypeAlias(_) => unreachable!(),
|
AssocItem::TypeAlias(_) => unreachable!(),
|
||||||
};
|
};
|
||||||
let substs = Substs::build_for_def(self.db, item)
|
let substs = Substs::build_for_def(self.db, item)
|
||||||
@ -193,8 +193,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> {
|
|||||||
method_resolution::LookupMode::Path,
|
method_resolution::LookupMode::Path,
|
||||||
move |_ty, item| {
|
move |_ty, item| {
|
||||||
let def = match item {
|
let def = match item {
|
||||||
AssocItem::Function(f) => ValueNs::Function(f),
|
AssocItem::Function(f) => ValueNs::FunctionId(f.id),
|
||||||
AssocItem::Const(c) => ValueNs::Const(c),
|
AssocItem::Const(c) => ValueNs::ConstId(c.id),
|
||||||
AssocItem::TypeAlias(_) => unreachable!(),
|
AssocItem::TypeAlias(_) => unreachable!(),
|
||||||
};
|
};
|
||||||
let substs = match item.container(self.db) {
|
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> {
|
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
|
// We only do the infer if parent has generic params
|
||||||
let gen = func.generic_params(self.db);
|
let gen = func.generic_params(self.db);
|
||||||
if gen.count_parent_params() == 0 {
|
if gen.count_parent_params() == 0 {
|
||||||
|
Loading…
Reference in New Issue
Block a user