From 96c88680b2d680f42809f838e79213e087d1fa1c Mon Sep 17 00:00:00 2001 From: cynecx Date: Wed, 24 Mar 2021 22:59:59 +0100 Subject: hir_def: move visibility queries from hir_ty to hir_def --- crates/hir_def/src/db.rs | 7 +++++++ crates/hir_def/src/visibility.rs | 32 +++++++++++++++++++++++++++++++- crates/hir_ty/src/db.rs | 10 ++-------- crates/hir_ty/src/lower.rs | 27 ++------------------------- crates/hir_ty/src/method_resolution.rs | 2 +- 5 files changed, 43 insertions(+), 35 deletions(-) (limited to 'crates') diff --git a/crates/hir_def/src/db.rs b/crates/hir_def/src/db.rs index 53df85089..068b2ee38 100644 --- a/crates/hir_def/src/db.rs +++ b/crates/hir_def/src/db.rs @@ -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 { #[salsa::invoke(ImportMap::import_map_query)] fn import_map(&self, krate: CrateId) -> Arc; + + #[salsa::invoke(visibility::field_visibilities_query)] + fn field_visibilities(&self, var: VariantId) -> Arc>; + + #[salsa::invoke(visibility::function_visibility_query)] + fn function_visibility(&self, def: FunctionId) -> Visibility; } fn crate_def_map_wait(db: &dyn DefDatabase, krate: CrateId) -> Arc { diff --git a/crates/hir_def/src/visibility.rs b/crates/hir_def/src/visibility.rs index 0e3951910..7d00a37c4 100644 --- a/crates/hir_def/src/visibility.rs +++ b/crates/hir_def/src/visibility.rs @@ -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> { + 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) +} diff --git a/crates/hir_ty/src/db.rs b/crates/hir_ty/src/db.rs index b1d22a9f9..91a2e0b5b 100644 --- a/crates/hir_ty/src/db.rs +++ b/crates/hir_ty/src/db.rs @@ -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 { krate: CrateId, env: chalk_ir::Environment, ) -> chalk_ir::ProgramClauses; - - #[salsa::invoke(crate::lower::field_visibilities_query)] - fn field_visibilities(&self, var: VariantId) -> Arc>; - - #[salsa::invoke(crate::lower::fn_visibility_query)] - fn fn_visibility(&self, def: FunctionId) -> Visibility; } fn infer_wait(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc { diff --git a/crates/hir_ty/src/lower.rs b/crates/hir_ty/src/lower.rs index c4062a41d..cbbb535e5 100644 --- a/crates/hir_ty/src/lower.rs +++ b/crates/hir_ty/src/lower.rs @@ -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> { - 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`. /// diff --git a/crates/hir_ty/src/method_resolution.rs b/crates/hir_ty/src/method_resolution.rs index f91208068..54192ec30 100644 --- a/crates/hir_ty/src/method_resolution.rs +++ b/crates/hir_ty/src/method_resolution.rs @@ -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; } -- cgit v1.2.3