1 //! Defines hir-level representation of visibility (e.g. `pub` and `pub(crate)`).
3 use hir_expand::{hygiene::Hygiene, InFile};
9 path::{ModPath, PathKind},
13 /// Visibility of an item, not yet resolved.
14 #[derive(Debug, Clone, PartialEq, Eq)]
15 pub enum RawVisibility {
16 /// `pub(in module)`, `pub(crate)` or `pub(super)`. Also private, which is
17 /// equivalent to `pub(self)`.
24 pub(crate) const fn private() -> RawVisibility {
25 RawVisibility::Module(ModPath::from_kind(PathKind::Super(0)))
28 pub(crate) fn from_ast(
30 node: InFile<Option<ast::Visibility>>,
32 Self::from_ast_with_hygiene(node.value, &Hygiene::new(db.upcast(), node.file_id))
35 pub(crate) fn from_ast_with_hygiene(
36 node: Option<ast::Visibility>,
39 Self::from_ast_with_hygiene_and_default(node, RawVisibility::private(), hygiene)
42 pub(crate) fn from_ast_with_hygiene_and_default(
43 node: Option<ast::Visibility>,
44 default: RawVisibility,
47 let node = match node {
48 None => return default,
52 ast::VisibilityKind::In(path) => {
53 let path = ModPath::from_src(path, hygiene);
54 let path = match path {
55 None => return RawVisibility::private(),
58 RawVisibility::Module(path)
60 ast::VisibilityKind::PubCrate => {
61 let path = ModPath::from_kind(PathKind::Crate);
62 RawVisibility::Module(path)
64 ast::VisibilityKind::PubSuper => {
65 let path = ModPath::from_kind(PathKind::Super(1));
66 RawVisibility::Module(path)
68 ast::VisibilityKind::PubSelf => {
69 let path = ModPath::from_kind(PathKind::Plain);
70 RawVisibility::Module(path)
72 ast::VisibilityKind::Pub => RawVisibility::Public,
79 resolver: &crate::resolver::Resolver,
81 // we fall back to public visibility (i.e. fail open) if the path can't be resolved
82 resolver.resolve_visibility(db, self).unwrap_or(Visibility::Public)
86 /// Visibility of an item, with the path resolved.
87 #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
89 /// Visibility is restricted to a certain module.
91 /// Visibility is unrestricted.
96 pub fn is_visible_from(self, db: &dyn DefDatabase, from_module: ModuleId) -> bool {
97 let to_module = match self {
98 Visibility::Module(m) => m,
99 Visibility::Public => return true,
101 // if they're not in the same crate, it can't be visible
102 if from_module.krate != to_module.krate {
105 let def_map = from_module.def_map(db);
106 self.is_visible_from_def_map(db, &def_map, from_module.local_id)
109 pub(crate) fn is_visible_from_other_crate(self) -> bool {
111 Visibility::Module(_) => false,
112 Visibility::Public => true,
116 pub(crate) fn is_visible_from_def_map(
118 db: &dyn DefDatabase,
120 mut from_module: crate::LocalModuleId,
122 let to_module = match self {
123 Visibility::Module(m) => m,
124 Visibility::Public => return true,
127 // from_module needs to be a descendant of to_module
128 let mut def_map = def_map;
131 if def_map.module_id(from_module) == to_module {
134 match def_map[from_module].parent {
136 from_module = parent;
139 match def_map.parent() {
141 parent_arc = module.def_map(db);
142 def_map = &*parent_arc;
143 from_module = module.local_id;
146 // Reached the root module, nothing left to check.
155 /// Returns the most permissive visibility of `self` and `other`.
157 /// If there is no subset relation between `self` and `other`, returns `None` (ie. they're only
158 /// visible in unrelated modules).
159 pub(crate) fn max(self, other: Visibility, def_map: &DefMap) -> Option<Visibility> {
160 match (self, other) {
161 (Visibility::Module(_), Visibility::Public)
162 | (Visibility::Public, Visibility::Module(_))
163 | (Visibility::Public, Visibility::Public) => Some(Visibility::Public),
164 (Visibility::Module(mod_a), Visibility::Module(mod_b)) => {
165 if mod_a.krate != mod_b.krate {
169 let mut a_ancestors = std::iter::successors(Some(mod_a.local_id), |m| {
170 let parent_id = def_map[*m].parent?;
173 let mut b_ancestors = std::iter::successors(Some(mod_b.local_id), |m| {
174 let parent_id = def_map[*m].parent?;
178 if a_ancestors.any(|m| m == mod_b.local_id) {
180 return Some(Visibility::Module(mod_b));
183 if b_ancestors.any(|m| m == mod_a.local_id) {
185 return Some(Visibility::Module(mod_a));