aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcynecx <[email protected]>2021-03-24 21:59:59 +0000
committercynecx <[email protected]>2021-03-24 22:00:03 +0000
commit96c88680b2d680f42809f838e79213e087d1fa1c (patch)
tree95f172cd93c206914c891f912f4105ac1b466621
parent42abfa0f885834c3c2bf61a4d0fafaa3f570debd (diff)
hir_def: move visibility queries from hir_ty to hir_def
-rw-r--r--crates/hir_def/src/db.rs7
-rw-r--r--crates/hir_def/src/visibility.rs32
-rw-r--r--crates/hir_ty/src/db.rs10
-rw-r--r--crates/hir_ty/src/lower.rs27
-rw-r--r--crates/hir_ty/src/method_resolution.rs2
5 files changed, 43 insertions, 35 deletions
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::{
16 item_tree::ItemTree, 16 item_tree::ItemTree,
17 lang_item::{LangItemTarget, LangItems}, 17 lang_item::{LangItemTarget, LangItems},
18 nameres::DefMap, 18 nameres::DefMap,
19 visibility::{self, Visibility},
19 AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId, 20 AttrDefId, BlockId, BlockLoc, ConstId, ConstLoc, DefWithBodyId, EnumId, EnumLoc, FunctionId,
20 FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalEnumVariantId, LocalFieldId, StaticId, 21 FunctionLoc, GenericDefId, ImplId, ImplLoc, LocalEnumVariantId, LocalFieldId, StaticId,
21 StaticLoc, StructId, StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId, 22 StaticLoc, StructId, StructLoc, TraitId, TraitLoc, TypeAliasId, TypeAliasLoc, UnionId,
@@ -131,6 +132,12 @@ pub trait DefDatabase: InternDatabase + AstDatabase + Upcast<dyn AstDatabase> {
131 132
132 #[salsa::invoke(ImportMap::import_map_query)] 133 #[salsa::invoke(ImportMap::import_map_query)]
133 fn import_map(&self, krate: CrateId) -> Arc<ImportMap>; 134 fn import_map(&self, krate: CrateId) -> Arc<ImportMap>;
135
136 #[salsa::invoke(visibility::field_visibilities_query)]
137 fn field_visibilities(&self, var: VariantId) -> Arc<ArenaMap<LocalFieldId, Visibility>>;
138
139 #[salsa::invoke(visibility::function_visibility_query)]
140 fn function_visibility(&self, def: FunctionId) -> Visibility;
134} 141}
135 142
136fn crate_def_map_wait(db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> { 143fn crate_def_map_wait(db: &dyn DefDatabase, krate: CrateId) -> Arc<DefMap> {
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 @@
1//! Defines hir-level representation of visibility (e.g. `pub` and `pub(crate)`). 1//! Defines hir-level representation of visibility (e.g. `pub` and `pub(crate)`).
2 2
3use std::sync::Arc;
4
3use hir_expand::{hygiene::Hygiene, InFile}; 5use hir_expand::{hygiene::Hygiene, InFile};
6use la_arena::ArenaMap;
4use syntax::ast; 7use syntax::ast;
5 8
6use crate::{ 9use crate::{
7 db::DefDatabase, 10 db::DefDatabase,
8 nameres::DefMap, 11 nameres::DefMap,
9 path::{ModPath, PathKind}, 12 path::{ModPath, PathKind},
10 ModuleId, 13 resolver::HasResolver,
14 FunctionId, HasModule, LocalFieldId, ModuleDefId, ModuleId, VariantId,
11}; 15};
12 16
13/// Visibility of an item, not yet resolved. 17/// Visibility of an item, not yet resolved.
@@ -190,3 +194,29 @@ impl Visibility {
190 } 194 }
191 } 195 }
192} 196}
197
198/// Resolve visibility of all specific fields of a struct or union variant.
199pub(crate) fn field_visibilities_query(
200 db: &dyn DefDatabase,
201 variant_id: VariantId,
202) -> Arc<ArenaMap<LocalFieldId, Visibility>> {
203 let var_data = match variant_id {
204 VariantId::StructId(it) => db.struct_data(it).variant_data.clone(),
205 VariantId::UnionId(it) => db.union_data(it).variant_data.clone(),
206 VariantId::EnumVariantId(it) => {
207 db.enum_data(it.parent).variants[it.local_id].variant_data.clone()
208 }
209 };
210 let resolver = variant_id.module(db).resolver(db);
211 let mut res = ArenaMap::default();
212 for (field_id, field_data) in var_data.fields().iter() {
213 res.insert(field_id, field_data.visibility.resolve(db, &resolver))
214 }
215 Arc::new(res)
216}
217
218/// Resolve visibility of a function.
219pub(crate) fn function_visibility_query(db: &dyn DefDatabase, def: FunctionId) -> Visibility {
220 let resolver = ModuleDefId::from(def).module(db).unwrap().resolver(db);
221 db.function_data(def).visibility.resolve(db, &resolver)
222}
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;
4 4
5use base_db::{impl_intern_key, salsa, CrateId, Upcast}; 5use base_db::{impl_intern_key, salsa, CrateId, Upcast};
6use hir_def::{ 6use hir_def::{
7 db::DefDatabase, expr::ExprId, visibility::Visibility, ConstParamId, DefWithBodyId, FunctionId, 7 db::DefDatabase, expr::ExprId, ConstParamId, DefWithBodyId, FunctionId, GenericDefId, ImplId,
8 GenericDefId, ImplId, LocalFieldId, TypeParamId, VariantId, 8 LocalFieldId, TypeParamId, VariantId,
9}; 9};
10use la_arena::ArenaMap; 10use la_arena::ArenaMap;
11 11
@@ -128,12 +128,6 @@ pub trait HirDatabase: DefDatabase + Upcast<dyn DefDatabase> {
128 krate: CrateId, 128 krate: CrateId,
129 env: chalk_ir::Environment<chalk::Interner>, 129 env: chalk_ir::Environment<chalk::Interner>,
130 ) -> chalk_ir::ProgramClauses<chalk::Interner>; 130 ) -> chalk_ir::ProgramClauses<chalk::Interner>;
131
132 #[salsa::invoke(crate::lower::field_visibilities_query)]
133 fn field_visibilities(&self, var: VariantId) -> Arc<ArenaMap<LocalFieldId, Visibility>>;
134
135 #[salsa::invoke(crate::lower::fn_visibility_query)]
136 fn fn_visibility(&self, def: FunctionId) -> Visibility;
137} 131}
138 132
139fn infer_wait(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> { 133fn infer_wait(db: &dyn HirDatabase, def: DefWithBodyId) -> Arc<InferenceResult> {
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::{
16 path::{GenericArg, Path, PathSegment, PathSegments}, 16 path::{GenericArg, Path, PathSegment, PathSegments},
17 resolver::{HasResolver, Resolver, TypeNs}, 17 resolver::{HasResolver, Resolver, TypeNs},
18 type_ref::{TypeBound, TypeRef}, 18 type_ref::{TypeBound, TypeRef},
19 visibility::Visibility,
20 AdtId, AssocContainerId, AssocItemId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId, 19 AdtId, AssocContainerId, AssocItemId, ConstId, ConstParamId, EnumId, EnumVariantId, FunctionId,
21 GenericDefId, HasModule, ImplId, LocalFieldId, Lookup, ModuleDefId, StaticId, StructId, 20 GenericDefId, HasModule, ImplId, LocalFieldId, Lookup, StaticId, StructId, TraitId,
22 TraitId, TypeAliasId, TypeParamId, UnionId, VariantId, 21 TypeAliasId, TypeParamId, UnionId, VariantId,
23}; 22};
24use hir_expand::name::Name; 23use hir_expand::name::Name;
25use la_arena::ArenaMap; 24use la_arena::ArenaMap;
@@ -868,28 +867,6 @@ pub(crate) fn field_types_query(
868 Arc::new(res) 867 Arc::new(res)
869} 868}
870 869
871/// Resolve visibility of all specific fields of a struct or union variant.
872pub(crate) fn field_visibilities_query(
873 db: &dyn HirDatabase,
874 variant_id: VariantId,
875) -> Arc<ArenaMap<LocalFieldId, Visibility>> {
876 let def_db = db.upcast();
877 let var_data = variant_data(def_db, variant_id);
878 let resolver = variant_id.module(def_db).resolver(def_db);
879 let mut res = ArenaMap::default();
880 for (field_id, field_data) in var_data.fields().iter() {
881 res.insert(field_id, field_data.visibility.resolve(def_db, &resolver))
882 }
883 Arc::new(res)
884}
885
886/// Resolve visibility of a function.
887pub(crate) fn fn_visibility_query(db: &dyn HirDatabase, def: FunctionId) -> Visibility {
888 let def_db = db.upcast();
889 let resolver = ModuleDefId::from(def).module(def_db).unwrap().resolver(def_db);
890 db.function_data(def).visibility.resolve(def_db, &resolver)
891}
892
893/// This query exists only to be used when resolving short-hand associated types 870/// This query exists only to be used when resolving short-hand associated types
894/// like `T::Item`. 871/// like `T::Item`.
895/// 872///
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(
686 } 686 }
687 } 687 }
688 if let Some(from_module) = visible_from_module { 688 if let Some(from_module) = visible_from_module {
689 if !db.fn_visibility(m).is_visible_from(db.upcast(), from_module) { 689 if !db.function_visibility(m).is_visible_from(db.upcast(), from_module) {
690 cov_mark::hit!(autoderef_candidate_not_visible); 690 cov_mark::hit!(autoderef_candidate_not_visible);
691 return false; 691 return false;
692 } 692 }