aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_def
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-03-24 22:37:48 +0000
committerGitHub <[email protected]>2021-03-24 22:37:48 +0000
commitd7db38fff9c251c36d0796309b43678bdf9e5bd8 (patch)
tree83b422d45c28e3ae9a2eac550ce8a6048bf5fd71 /crates/hir_def
parent9d81618f11eb403cc0644a22f30648393ec77cf6 (diff)
parentd1156bb52e900c015afd490f509d744c7a5adf10 (diff)
Merge #7907
7907: Autoderef with visibility r=cynecx a=cynecx Fixes https://github.com/rust-analyzer/rust-analyzer/issues/7841. I am not sure about the general approach here. Right now this simply tries to check whether the autoderef candidate is reachable from the current module. ~~However this doesn't exactly work with traits (see the `tests::macros::infer_derive_clone_in_core` test, which fails right now).~~ see comment below Refs: - `rustc_typeck` checking fields: https://github.com/rust-lang/rust/blob/66ec64ccf31883cd2c28d045912a76179c0c6ed2/compiler/rustc_typeck/src/check/expr.rs#L1610 r? @flodiebold Co-authored-by: cynecx <[email protected]>
Diffstat (limited to 'crates/hir_def')
-rw-r--r--crates/hir_def/src/db.rs7
-rw-r--r--crates/hir_def/src/visibility.rs32
2 files changed, 38 insertions, 1 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}