1 //! Defines hir-level representation of visibility (e.g. `pub` and `pub(crate)`).
3 use hir_expand::{hygiene::Hygiene, InFile};
8 path::{ModPath, PathKind},
12 /// Visibility of an item, not yet resolved.
13 #[derive(Debug, Clone, PartialEq, Eq)]
14 pub enum RawVisibility {
15 /// `pub(in module)`, `pub(crate)` or `pub(super)`. Also private, which is
16 /// equivalent to `pub(self)`.
23 pub(crate) const fn private() -> RawVisibility {
24 let path = ModPath { kind: PathKind::Super(0), segments: Vec::new() };
25 RawVisibility::Module(path)
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 { kind: PathKind::Crate, segments: Vec::new() };
62 RawVisibility::Module(path)
64 ast::VisibilityKind::PubSuper => {
65 let path = ModPath { kind: PathKind::Super(1), segments: Vec::new() };
66 RawVisibility::Module(path)
68 ast::VisibilityKind::PubSelf => {
69 let path = ModPath { kind: PathKind::Plain, segments: Vec::new() };
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)]
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 = db.crate_def_map(from_module.krate);
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(
118 def_map: &crate::nameres::CrateDefMap,
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)