hir_def: move visibility queries from hir_ty to hir_def

This commit is contained in:
cynecx 2021-03-24 22:59:59 +01:00
parent 42abfa0f88
commit 96c88680b2
5 changed files with 43 additions and 35 deletions

View File

@ -16,6 +16,7 @@ use crate::{
item_tree::ItemTree,
lang_item::{LangItemTarget, LangItems},
nameres::DefMap,
visibility::{self, Visibility},
AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId,
FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalEnumVariantId, LocalFieldId, StaticId,
StaticLoc, StructId, StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId,
@ -131,6 +132,12 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
#[salsa::invoke(ImportMap::import_map_query)]
fn import_map(&self, krate: CrateId) -> Arc<ImportMap>;
#[salsa::invoke(visibility::field_visibilities_query)]
fn field_visibilities(&self, var: VariantId) -> Arc<ArenaMap<LocalFieldId, Visibility>>;
#[salsa::invoke(visibility::function_visibility_query)]
fn function_visibility(&self, def: FunctionId) -> Visibility;
}
fn crate_def_map_wait(db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {

View File

@ -1,13 +1,17 @@
//! Defines hir-level representation of visibility (e.g. `pub` and `pub(crate)`).
use std::sync::Arc;
use hir_expand::{hygiene::Hygiene, InFile};
use la_arena::ArenaMap;
use syntax::ast;
use crate::{
db::DefDatabase,
nameres::DefMap,
path::{ModPath, PathKind},
ModuleId,
resolver::HasResolver,
FunctionId, HasModule, LocalFieldId, ModuleDefId, ModuleId, VariantId,
};
/// Visibility of an item, not yet resolved.
@ -190,3 +194,29 @@ impl Visibility {
}
}
}
/// Resolve visibility of all specific fields of a struct or union variant.
pub(crate) fn field_visibilities_query(
db: &dyn DefDatabase,
variant_id: VariantId,
) -> Arc<ArenaMap<LocalFieldId, Visibility>> {
let var_data = match variant_id {
VariantId::StructId(it) => db.struct_data(it).variant_data.clone(),
VariantId::UnionId(it) => db.union_data(it).variant_data.clone(),
VariantId::EnumVariantId(it) => {
db.enum_data(it.parent).variants[it.local_id].variant_data.clone()
}
};
let resolver = variant_id.module(db).resolver(db);
let mut res = ArenaMap::default();
for (field_id, field_data) in var_data.fields().iter() {
res.insert(field_id, field_data.visibility.resolve(db, &resolver))
}
Arc::new(res)
}
/// Resolve visibility of a function.
pub(crate) fn function_visibility_query(db: &dyn DefDatabase, def: FunctionId) -> Visibility {
let resolver = ModuleDefId::from(def).module(db).unwrap().resolver(db);
db.function_data(def).visibility.resolve(db, &resolver)
}

View File

@ -4,8 +4,8 @@ use std::sync::Arc;
use base_db::{impl_intern_key, salsa, CrateId, Upcast};
use hir_def::{
db::DefDatabase, expr::ExprId, visibility::Visibility, ConstParamId, DefWithBodyId, FunctionId,
GenericDefId, ImplId, LocalFieldId, TypeParamId, VariantId,
db::DefDatabase, expr::ExprId, ConstParamId, DefWithBodyId, FunctionId, GenericDefId, ImplId,
LocalFieldId, TypeParamId, VariantId,
};
use la_arena::ArenaMap;
@ -128,12 +128,6 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
krate: CrateId,
env: chalk_ir::Environment<chalk::Interner>,
) -> chalk_ir::ProgramClauses<chalk::Interner>;
#[salsa::invoke(crate::lower::field_visibilities_query)]
fn field_visibilities(&self, var: VariantId) -> Arc<ArenaMap<LocalFieldId, Visibility>>;
#[salsa::invoke(crate::lower::fn_visibility_query)]
fn fn_visibility(&self, def: FunctionId) -> Visibility;
}
fn infer_wait(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {

View File

@ -16,10 +16,9 @@ use hir_def::{
path::{GenericArg, Path, PathSegment, PathSegments},
resolver::{HasResolver, Resolver, TypeNs},
type_ref::{TypeBound, TypeRef},
visibility::Visibility,
AdtId, AssocContainerId, AssocItemId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId,
GenericDefId, HasModule, ImplId, LocalFieldId, Lookup, ModuleDefId, StaticId, StructId,
TraitId, TypeAliasId, TypeParamId, UnionId, VariantId,
GenericDefId, HasModule, ImplId, LocalFieldId, Lookup, StaticId, StructId, TraitId,
TypeAliasId, TypeParamId, UnionId, VariantId,
};
use hir_expand::name::Name;
use la_arena::ArenaMap;
@ -868,28 +867,6 @@ pub(crate) fn field_types_query(
Arc::new(res)
}
/// Resolve visibility of all specific fields of a struct or union variant.
pub(crate) fn field_visibilities_query(
db: &dyn HirDatabase,
variant_id: VariantId,
) -> Arc<ArenaMap<LocalFieldId, Visibility>> {
let def_db = db.upcast();
let var_data = variant_data(def_db, variant_id);
let resolver = variant_id.module(def_db).resolver(def_db);
let mut res = ArenaMap::default();
for (field_id, field_data) in var_data.fields().iter() {
res.insert(field_id, field_data.visibility.resolve(def_db, &resolver))
}
Arc::new(res)
}
/// Resolve visibility of a function.
pub(crate) fn fn_visibility_query(db: &dyn HirDatabase, def: FunctionId) -> Visibility {
let def_db = db.upcast();
let resolver = ModuleDefId::from(def).module(def_db).unwrap().resolver(def_db);
db.function_data(def).visibility.resolve(def_db, &resolver)
}
/// This query exists only to be used when resolving short-hand associated types
/// like `T::Item`.
///

View File

@ -686,7 +686,7 @@ fn is_valid_candidate(
}
}
if let Some(from_module) = visible_from_module {
if !db.fn_visibility(m).is_visible_from(db.upcast(), from_module) {
if !db.function_visibility(m).is_visible_from(db.upcast(), from_module) {
cov_mark::hit!(autoderef_candidate_not_visible);
return false;
}