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(&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(
119 from_module: crate::LocalModuleId,
121 let to_module = match self {
122 Visibility::Module(m) => m,
123 Visibility::Public => return true,
125 // from_module needs to be a descendant of to_module
126 let mut ancestors = std::iter::successors(Some(from_module), |m| {
127 let parent_id = def_map[*m].parent?;
130 ancestors.any(|m| m == to_module.local_id)
133 /// Returns the most permissive visibility of `self` and `other`.
135 /// If there is no subset relation between `self` and `other`, returns `None` (ie. they're only
136 /// visible in unrelated modules).
137 pub(crate) fn max(self, other: Visibility, def_map: &DefMap) -> Option<Visibility> {
138 match (self, other) {
139 (Visibility::Module(_), Visibility::Public)
140 | (Visibility::Public, Visibility::Module(_))
141 | (Visibility::Public, Visibility::Public) => Some(Visibility::Public),
142 (Visibility::Module(mod_a), Visibility::Module(mod_b)) => {
143 if mod_a.krate != mod_b.krate {
147 let mut a_ancestors = std::iter::successors(Some(mod_a.local_id), |m| {
148 let parent_id = def_map[*m].parent?;
151 let mut b_ancestors = std::iter::successors(Some(mod_b.local_id), |m| {
152 let parent_id = def_map[*m].parent?;
156 if a_ancestors.any(|m| m == mod_b.local_id) {
158 return Some(Visibility::Module(mod_b));
161 if b_ancestors.any(|m| m == mod_a.local_id) {
163 return Some(Visibility::Module(mod_a));