]> git.lizzy.rs Git - rust.git/blob - crates/hir_def/src/visibility.rs
Merge #8317
[rust.git] / crates / hir_def / src / visibility.rs
1 //! Defines hir-level representation of visibility (e.g. `pub` and `pub(crate)`).
2
3 use std::sync::Arc;
4
5 use hir_expand::{hygiene::Hygiene, InFile};
6 use la_arena::ArenaMap;
7 use syntax::ast;
8
9 use crate::{
10     db::DefDatabase,
11     nameres::DefMap,
12     path::{ModPath, PathKind},
13     resolver::HasResolver,
14     FunctionId, HasModule, LocalFieldId, ModuleId, VariantId,
15 };
16
17 /// Visibility of an item, not yet resolved.
18 #[derive(Debug, Clone, PartialEq, Eq)]
19 pub enum RawVisibility {
20     /// `pub(in module)`, `pub(crate)` or `pub(super)`. Also private, which is
21     /// equivalent to `pub(self)`.
22     Module(ModPath),
23     /// `pub`.
24     Public,
25 }
26
27 impl RawVisibility {
28     pub(crate) fn private() -> RawVisibility {
29         RawVisibility::Module(ModPath::from_kind(PathKind::Super(0)))
30     }
31
32     pub(crate) fn from_ast(
33         db: &dyn DefDatabase,
34         node: InFile<Option<ast::Visibility>>,
35     ) -> RawVisibility {
36         Self::from_ast_with_hygiene(node.value, &Hygiene::new(db.upcast(), node.file_id))
37     }
38
39     pub(crate) fn from_ast_with_hygiene(
40         node: Option<ast::Visibility>,
41         hygiene: &Hygiene,
42     ) -> RawVisibility {
43         Self::from_ast_with_hygiene_and_default(node, RawVisibility::private(), hygiene)
44     }
45
46     pub(crate) fn from_ast_with_hygiene_and_default(
47         node: Option<ast::Visibility>,
48         default: RawVisibility,
49         hygiene: &Hygiene,
50     ) -> RawVisibility {
51         let node = match node {
52             None => return default,
53             Some(node) => node,
54         };
55         match node.kind() {
56             ast::VisibilityKind::In(path) => {
57                 let path = ModPath::from_src(path, hygiene);
58                 let path = match path {
59                     None => return RawVisibility::private(),
60                     Some(path) => path,
61                 };
62                 RawVisibility::Module(path)
63             }
64             ast::VisibilityKind::PubCrate => {
65                 let path = ModPath::from_kind(PathKind::Crate);
66                 RawVisibility::Module(path)
67             }
68             ast::VisibilityKind::PubSuper => {
69                 let path = ModPath::from_kind(PathKind::Super(1));
70                 RawVisibility::Module(path)
71             }
72             ast::VisibilityKind::PubSelf => {
73                 let path = ModPath::from_kind(PathKind::Plain);
74                 RawVisibility::Module(path)
75             }
76             ast::VisibilityKind::Pub => RawVisibility::Public,
77         }
78     }
79
80     pub fn resolve(
81         &self,
82         db: &dyn DefDatabase,
83         resolver: &crate::resolver::Resolver,
84     ) -> Visibility {
85         // we fall back to public visibility (i.e. fail open) if the path can't be resolved
86         resolver.resolve_visibility(db, self).unwrap_or(Visibility::Public)
87     }
88 }
89
90 /// Visibility of an item, with the path resolved.
91 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
92 pub enum Visibility {
93     /// Visibility is restricted to a certain module.
94     Module(ModuleId),
95     /// Visibility is unrestricted.
96     Public,
97 }
98
99 impl Visibility {
100     pub fn is_visible_from(self, db: &dyn DefDatabase, from_module: ModuleId) -> bool {
101         let to_module = match self {
102             Visibility::Module(m) => m,
103             Visibility::Public => return true,
104         };
105         // if they're not in the same crate, it can't be visible
106         if from_module.krate != to_module.krate {
107             return false;
108         }
109         let def_map = from_module.def_map(db);
110         self.is_visible_from_def_map(db, &def_map, from_module.local_id)
111     }
112
113     pub(crate) fn is_visible_from_other_crate(self) -> bool {
114         match self {
115             Visibility::Module(_) => false,
116             Visibility::Public => true,
117         }
118     }
119
120     pub(crate) fn is_visible_from_def_map(
121         self,
122         db: &dyn DefDatabase,
123         def_map: &DefMap,
124         mut from_module: crate::LocalModuleId,
125     ) -> bool {
126         let mut to_module = match self {
127             Visibility::Module(m) => m,
128             Visibility::Public => return true,
129         };
130
131         // `to_module` might be the root module of a block expression. Those have the same
132         // visibility as the containing module (even though no items are directly nameable from
133         // there, getting this right is important for method resolution).
134         // In that case, we adjust the visibility of `to_module` to point to the containing module.
135         if to_module.is_block_root(db) {
136             to_module = to_module.containing_module(db).unwrap();
137         }
138
139         // from_module needs to be a descendant of to_module
140         let mut def_map = def_map;
141         let mut parent_arc;
142         loop {
143             if def_map.module_id(from_module) == to_module {
144                 return true;
145             }
146             match def_map[from_module].parent {
147                 Some(parent) => {
148                     from_module = parent;
149                 }
150                 None => {
151                     match def_map.parent() {
152                         Some(module) => {
153                             parent_arc = module.def_map(db);
154                             def_map = &*parent_arc;
155                             from_module = module.local_id;
156                         }
157                         None => {
158                             // Reached the root module, nothing left to check.
159                             return false;
160                         }
161                     }
162                 }
163             }
164         }
165     }
166
167     /// Returns the most permissive visibility of `self` and `other`.
168     ///
169     /// If there is no subset relation between `self` and `other`, returns `None` (ie. they're only
170     /// visible in unrelated modules).
171     pub(crate) fn max(self, other: Visibility, def_map: &DefMap) -> Option<Visibility> {
172         match (self, other) {
173             (Visibility::Module(_), Visibility::Public)
174             | (Visibility::Public, Visibility::Module(_))
175             | (Visibility::Public, Visibility::Public) => Some(Visibility::Public),
176             (Visibility::Module(mod_a), Visibility::Module(mod_b)) => {
177                 if mod_a.krate != mod_b.krate {
178                     return None;
179                 }
180
181                 let mut a_ancestors = std::iter::successors(Some(mod_a.local_id), |m| {
182                     let parent_id = def_map[*m].parent?;
183                     Some(parent_id)
184                 });
185                 let mut b_ancestors = std::iter::successors(Some(mod_b.local_id), |m| {
186                     let parent_id = def_map[*m].parent?;
187                     Some(parent_id)
188                 });
189
190                 if a_ancestors.any(|m| m == mod_b.local_id) {
191                     // B is above A
192                     return Some(Visibility::Module(mod_b));
193                 }
194
195                 if b_ancestors.any(|m| m == mod_a.local_id) {
196                     // A is above B
197                     return Some(Visibility::Module(mod_a));
198                 }
199
200                 None
201             }
202         }
203     }
204 }
205
206 /// Resolve visibility of all specific fields of a struct or union variant.
207 pub(crate) fn field_visibilities_query(
208     db: &dyn DefDatabase,
209     variant_id: VariantId,
210 ) -> Arc<ArenaMap<LocalFieldId, Visibility>> {
211     let var_data = match variant_id {
212         VariantId::StructId(it) => db.struct_data(it).variant_data.clone(),
213         VariantId::UnionId(it) => db.union_data(it).variant_data.clone(),
214         VariantId::EnumVariantId(it) => {
215             db.enum_data(it.parent).variants[it.local_id].variant_data.clone()
216         }
217     };
218     let resolver = variant_id.module(db).resolver(db);
219     let mut res = ArenaMap::default();
220     for (field_id, field_data) in var_data.fields().iter() {
221         res.insert(field_id, field_data.visibility.resolve(db, &resolver))
222     }
223     Arc::new(res)
224 }
225
226 /// Resolve visibility of a function.
227 pub(crate) fn function_visibility_query(db: &dyn DefDatabase, def: FunctionId) -> Visibility {
228     let resolver = def.resolver(db);
229     db.function_data(def).visibility.resolve(db, &resolver)
230 }