mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 21:54:42 +00:00
Decouple
This commit is contained in:
parent
621cf06156
commit
061e6c77b5
6 changed files with 108 additions and 78 deletions
|
@ -829,7 +829,7 @@ impl Trait {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn direct_super_traits(self, db: &impl HirDatabase) -> Vec<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
|
// returning the iterator directly doesn't easily work because of
|
||||||
// lifetime problems, but since there usually shouldn't be more than a
|
// 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
|
// few direct traits this should be fine (we could even use some kind of
|
||||||
|
|
|
@ -122,7 +122,7 @@ impl<'a, 'b> ExprValidator<'a, 'b> {
|
||||||
|
|
||||||
let std_result_path = known::std_result_result();
|
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) {
|
let std_result_enum = match resolver.resolve_known_enum(db, &std_result_path) {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
_ => return,
|
_ => return,
|
||||||
|
|
|
@ -27,12 +27,12 @@ impl ImplBlock {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn target_ty(&self, db: &impl HirDatabase) -> Ty {
|
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> {
|
pub fn target_trait_ref(&self, db: &impl HirDatabase) -> Option<TraitRef> {
|
||||||
let target_ty = self.target_ty(db);
|
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> {
|
pub fn items(&self, db: &impl DefDatabase) -> Vec<AssocItem> {
|
||||||
|
|
|
@ -8,18 +8,18 @@ use hir_def::{
|
||||||
generics::GenericParams,
|
generics::GenericParams,
|
||||||
nameres::CrateDefMap,
|
nameres::CrateDefMap,
|
||||||
path::{Path, PathKind},
|
path::{Path, PathKind},
|
||||||
AdtId, ConstId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId,
|
AdtId, AstItemDef, ConstId, ContainerId, CrateModuleId, DefWithBodyId, EnumId, EnumVariantId,
|
||||||
ImplId, ModuleDefId, StaticId, StructId, TraitId, TypeAliasId,
|
FunctionId, GenericDefId, ImplId, Lookup, ModuleDefId, ModuleId, StaticId, StructId, TraitId,
|
||||||
|
TypeAliasId, UnionId,
|
||||||
};
|
};
|
||||||
use hir_expand::name::{self, Name};
|
use hir_expand::name::{self, Name};
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
code_model::Crate,
|
code_model::Crate,
|
||||||
db::{DefDatabase, HirDatabase},
|
db::HirDatabase,
|
||||||
expr::{ExprScopes, PatId, ScopeId},
|
expr::{ExprScopes, PatId, ScopeId},
|
||||||
Adt, Const, Container, DefWithBody, Function, GenericDef, ImplBlock, Local, MacroDef, Module,
|
Adt, DefWithBody, GenericDef, ImplBlock, Local, MacroDef, ModuleDef, PerNs,
|
||||||
ModuleDef, PerNs, Static, Trait, TypeAlias,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default)]
|
||||||
|
@ -506,7 +506,7 @@ pub(crate) fn resolver_for_scope(
|
||||||
owner: DefWithBodyId,
|
owner: DefWithBodyId,
|
||||||
scope_id: Option<ScopeId>,
|
scope_id: Option<ScopeId>,
|
||||||
) -> Resolver {
|
) -> Resolver {
|
||||||
let mut r = DefWithBody::from(owner).resolver(db);
|
let mut r = owner.resolver(db);
|
||||||
let scopes = db.expr_scopes(owner);
|
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() {
|
||||||
|
@ -517,100 +517,130 @@ pub(crate) fn resolver_for_scope(
|
||||||
|
|
||||||
pub(crate) trait HasResolver {
|
pub(crate) trait HasResolver {
|
||||||
/// Builds a resolver for type references inside this def.
|
/// 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 {
|
impl HasResolver for ModuleId {
|
||||||
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
|
||||||
let def_map = db.crate_def_map(self.id.krate);
|
let def_map = db.crate_def_map(self.krate);
|
||||||
Resolver::default().push_module_scope(def_map, self.id.module_id)
|
Resolver::default().push_module_scope(def_map, self.module_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HasResolver for Trait {
|
impl HasResolver for TraitId {
|
||||||
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
|
||||||
self.module(db).resolver(db).push_generic_params_scope(db, self.id.into())
|
self.module(db).resolver(db).push_generic_params_scope(db, self.into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Into<Adt>> HasResolver for T {
|
impl HasResolver for AdtId {
|
||||||
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
|
||||||
let def = self.into();
|
let module = match self {
|
||||||
def.module(db)
|
AdtId::StructId(it) => it.0.module(db),
|
||||||
|
AdtId::UnionId(it) => it.0.module(db),
|
||||||
|
AdtId::EnumId(it) => it.module(db),
|
||||||
|
};
|
||||||
|
|
||||||
|
module
|
||||||
.resolver(db)
|
.resolver(db)
|
||||||
.push_generic_params_scope(db, def.into())
|
.push_generic_params_scope(db, self.into())
|
||||||
.push_scope(Scope::AdtScope(def.into()))
|
.push_scope(Scope::AdtScope(self.into()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HasResolver for Function {
|
impl HasResolver for StructId {
|
||||||
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
|
||||||
self.container(db)
|
AdtId::from(self).resolver(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 DefWithBody {
|
impl HasResolver for UnionId {
|
||||||
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
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 {
|
match self {
|
||||||
DefWithBody::Const(c) => c.resolver(db),
|
DefWithBodyId::ConstId(c) => c.resolver(db),
|
||||||
DefWithBody::Function(f) => f.resolver(db),
|
DefWithBodyId::FunctionId(f) => f.resolver(db),
|
||||||
DefWithBody::Static(s) => s.resolver(db),
|
DefWithBodyId::StaticId(s) => s.resolver(db),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HasResolver for Const {
|
impl HasResolver for ConstId {
|
||||||
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
|
||||||
self.container(db).map(|c| c.resolver(db)).unwrap_or_else(|| self.module(db).resolver(db))
|
self.lookup(db).container.resolver(db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HasResolver for Static {
|
impl HasResolver for StaticId {
|
||||||
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
|
||||||
self.module(db).resolver(db)
|
self.module(db).resolver(db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HasResolver for TypeAlias {
|
impl HasResolver for TypeAliasId {
|
||||||
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
|
||||||
self.container(db)
|
self.lookup(db).container.resolver(db).push_generic_params_scope(db, self.into())
|
||||||
.map(|ib| ib.resolver(db))
|
|
||||||
.unwrap_or_else(|| self.module(db).resolver(db))
|
|
||||||
.push_generic_params_scope(db, self.id.into())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HasResolver for Container {
|
impl HasResolver for ContainerId {
|
||||||
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
|
||||||
match self {
|
match self {
|
||||||
Container::Trait(trait_) => trait_.resolver(db),
|
ContainerId::TraitId(it) => it.resolver(db),
|
||||||
Container::ImplBlock(impl_block) => impl_block.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 {
|
impl HasResolver for GenericDef {
|
||||||
fn resolver(self, db: &impl DefDatabase) -> crate::Resolver {
|
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
|
||||||
match self {
|
GenericDefId::from(self).resolver(db)
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HasResolver for ImplBlock {
|
impl HasResolver for DefWithBody {
|
||||||
fn resolver(self, db: &impl DefDatabase) -> Resolver {
|
fn resolver(self, db: &impl DefDatabase2) -> Resolver {
|
||||||
self.module(db)
|
DefWithBodyId::from(self).resolver(db)
|
||||||
.resolver(db)
|
|
||||||
.push_generic_params_scope(db, self.id.into())
|
|
||||||
.push_impl_block_scope(self.id)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,19 +35,19 @@ fn try_get_resolver_for_node(db: &impl HirDatabase, node: Source<&SyntaxNode>) -
|
||||||
match (node.value) {
|
match (node.value) {
|
||||||
ast::Module(it) => {
|
ast::Module(it) => {
|
||||||
let src = node.with_value(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) => {
|
ast::SourceFile(it) => {
|
||||||
let src = node.with_value(crate::ModuleSource::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) => {
|
ast::StructDef(it) => {
|
||||||
let src = node.with_value(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) => {
|
ast::EnumDef(it) => {
|
||||||
let src = node.with_value(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() {
|
_ => match node.value.kind() {
|
||||||
FN_DEF | CONST_DEF | STATIC_DEF => {
|
FN_DEF | CONST_DEF | STATIC_DEF => {
|
||||||
|
|
|
@ -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 {
|
pub(crate) fn type_for_field(db: &impl HirDatabase, field: StructField) -> Ty {
|
||||||
let parent_def = field.parent_def(db);
|
let parent_def = field.parent_def(db);
|
||||||
let resolver = match parent_def {
|
let resolver = match parent_def {
|
||||||
VariantDef::Struct(it) => it.resolver(db),
|
VariantDef::Struct(it) => it.id.resolver(db),
|
||||||
VariantDef::EnumVariant(it) => it.parent_enum(db).resolver(db),
|
VariantDef::EnumVariant(it) => it.parent.id.resolver(db),
|
||||||
};
|
};
|
||||||
let var_data = parent_def.variant_data(db);
|
let var_data = parent_def.variant_data(db);
|
||||||
let type_ref = &var_data.fields().unwrap()[field.id].type_ref;
|
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 {
|
fn fn_sig_for_fn(db: &impl HirDatabase, def: Function) -> FnSig {
|
||||||
let data = def.data(db);
|
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 params = data.params().iter().map(|tr| Ty::from_hir(db, &resolver, tr)).collect::<Vec<_>>();
|
||||||
let ret = Ty::from_hir(db, &resolver, data.ret_type());
|
let ret = Ty::from_hir(db, &resolver, data.ret_type());
|
||||||
FnSig::from_params_and_return(params, ret)
|
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.
|
/// Build the declared type of a const.
|
||||||
fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty {
|
fn type_for_const(db: &impl HirDatabase, def: Const) -> Ty {
|
||||||
let data = def.data(db);
|
let data = def.data(db);
|
||||||
let resolver = def.resolver(db);
|
let resolver = def.id.resolver(db);
|
||||||
|
|
||||||
Ty::from_hir(db, &resolver, data.type_ref())
|
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.
|
/// Build the declared type of a static.
|
||||||
fn type_for_static(db: &impl HirDatabase, def: Static) -> Ty {
|
fn type_for_static(db: &impl HirDatabase, def: Static) -> Ty {
|
||||||
let data = def.data(db);
|
let data = def.data(db);
|
||||||
let resolver = def.resolver(db);
|
let resolver = def.id.resolver(db);
|
||||||
|
|
||||||
Ty::from_hir(db, &resolver, data.type_ref())
|
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,
|
Some(fields) => fields,
|
||||||
None => panic!("fn_sig_for_struct_constructor called on unit struct"),
|
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
|
let params = fields
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref))
|
.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,
|
Some(fields) => fields,
|
||||||
None => panic!("fn_sig_for_enum_variant_constructor called for unit variant"),
|
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
|
let params = fields
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(_, field)| Ty::from_hir(db, &resolver, &field.type_ref))
|
.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 {
|
fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty {
|
||||||
let generics = t.generic_params(db);
|
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 type_ref = t.type_ref(db);
|
||||||
let substs = Substs::identity(&generics);
|
let substs = Substs::identity(&generics);
|
||||||
let inner = Ty::from_hir(db, &resolver, &type_ref.unwrap_or(TypeRef::Error));
|
let inner = Ty::from_hir(db, &resolver, &type_ref.unwrap_or(TypeRef::Error));
|
||||||
|
|
Loading…
Reference in a new issue