2019-12-24 15:11:50 -06:00
|
|
|
//! Defines hir-level representation of visibility (e.g. `pub` and `pub(crate)`).
|
|
|
|
|
2019-12-24 16:45:14 -06:00
|
|
|
use hir_expand::{hygiene::Hygiene, InFile};
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::ast;
|
2019-12-24 13:32:42 -06:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
db::DefDatabase,
|
2021-01-18 13:18:05 -06:00
|
|
|
nameres::DefMap,
|
2019-12-24 13:32:42 -06:00
|
|
|
path::{ModPath, PathKind},
|
2020-06-22 09:41:10 -05:00
|
|
|
ModuleId,
|
2019-12-24 13:32:42 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Visibility of an item, not yet resolved.
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
2019-12-26 08:57:14 -06:00
|
|
|
pub enum RawVisibility {
|
2019-12-24 13:32:42 -06:00
|
|
|
/// `pub(in module)`, `pub(crate)` or `pub(super)`. Also private, which is
|
|
|
|
/// equivalent to `pub(self)`.
|
2019-12-26 09:42:57 -06:00
|
|
|
Module(ModPath),
|
2019-12-24 13:32:42 -06:00
|
|
|
/// `pub`.
|
|
|
|
Public,
|
|
|
|
}
|
|
|
|
|
2019-12-26 08:57:14 -06:00
|
|
|
impl RawVisibility {
|
2020-03-14 14:48:36 -05:00
|
|
|
pub(crate) const fn private() -> RawVisibility {
|
2021-02-04 13:49:24 -06:00
|
|
|
RawVisibility::Module(ModPath::from_kind(PathKind::Super(0)))
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|
|
|
|
|
2019-12-26 09:22:15 -06:00
|
|
|
pub(crate) fn from_ast(
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-26 09:22:15 -06:00
|
|
|
node: InFile<Option<ast::Visibility>>,
|
|
|
|
) -> RawVisibility {
|
2020-03-13 10:05:46 -05:00
|
|
|
Self::from_ast_with_hygiene(node.value, &Hygiene::new(db.upcast(), node.file_id))
|
2019-12-24 16:45:14 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn from_ast_with_hygiene(
|
|
|
|
node: Option<ast::Visibility>,
|
|
|
|
hygiene: &Hygiene,
|
2020-03-14 14:48:36 -05:00
|
|
|
) -> RawVisibility {
|
|
|
|
Self::from_ast_with_hygiene_and_default(node, RawVisibility::private(), hygiene)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn from_ast_with_hygiene_and_default(
|
|
|
|
node: Option<ast::Visibility>,
|
|
|
|
default: RawVisibility,
|
|
|
|
hygiene: &Hygiene,
|
2019-12-26 08:57:14 -06:00
|
|
|
) -> RawVisibility {
|
2019-12-24 16:45:14 -06:00
|
|
|
let node = match node {
|
2020-03-14 14:48:36 -05:00
|
|
|
None => return default,
|
2019-12-24 13:32:42 -06:00
|
|
|
Some(node) => node,
|
|
|
|
};
|
|
|
|
match node.kind() {
|
|
|
|
ast::VisibilityKind::In(path) => {
|
2019-12-24 16:45:14 -06:00
|
|
|
let path = ModPath::from_src(path, hygiene);
|
2019-12-24 13:32:42 -06:00
|
|
|
let path = match path {
|
2019-12-26 08:57:14 -06:00
|
|
|
None => return RawVisibility::private(),
|
2019-12-24 13:32:42 -06:00
|
|
|
Some(path) => path,
|
|
|
|
};
|
2019-12-26 09:42:57 -06:00
|
|
|
RawVisibility::Module(path)
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|
|
|
|
ast::VisibilityKind::PubCrate => {
|
2021-02-04 13:49:24 -06:00
|
|
|
let path = ModPath::from_kind(PathKind::Crate);
|
2019-12-26 09:42:57 -06:00
|
|
|
RawVisibility::Module(path)
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|
|
|
|
ast::VisibilityKind::PubSuper => {
|
2021-02-04 13:49:24 -06:00
|
|
|
let path = ModPath::from_kind(PathKind::Super(1));
|
2019-12-26 09:42:57 -06:00
|
|
|
RawVisibility::Module(path)
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|
2020-04-03 14:12:09 -05:00
|
|
|
ast::VisibilityKind::PubSelf => {
|
2021-02-04 13:49:24 -06:00
|
|
|
let path = ModPath::from_kind(PathKind::Plain);
|
2020-04-03 14:12:09 -05:00
|
|
|
RawVisibility::Module(path)
|
|
|
|
}
|
2019-12-26 08:57:14 -06:00
|
|
|
ast::VisibilityKind::Pub => RawVisibility::Public,
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|
|
|
|
}
|
2019-12-24 14:23:22 -06:00
|
|
|
|
|
|
|
pub fn resolve(
|
|
|
|
&self,
|
2020-03-13 10:05:46 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-24 14:23:22 -06:00
|
|
|
resolver: &crate::resolver::Resolver,
|
2019-12-26 09:00:10 -06:00
|
|
|
) -> Visibility {
|
2019-12-24 14:23:22 -06:00
|
|
|
// we fall back to public visibility (i.e. fail open) if the path can't be resolved
|
2019-12-26 09:00:10 -06:00
|
|
|
resolver.resolve_visibility(db, self).unwrap_or(Visibility::Public)
|
2019-12-24 14:23:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Visibility of an item, with the path resolved.
|
2020-12-31 14:41:49 -06:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2019-12-26 09:00:10 -06:00
|
|
|
pub enum Visibility {
|
2019-12-24 14:23:22 -06:00
|
|
|
/// Visibility is restricted to a certain module.
|
|
|
|
Module(ModuleId),
|
|
|
|
/// Visibility is unrestricted.
|
|
|
|
Public,
|
|
|
|
}
|
|
|
|
|
2019-12-26 09:00:10 -06:00
|
|
|
impl Visibility {
|
2020-03-13 10:05:46 -05:00
|
|
|
pub fn is_visible_from(self, db: &dyn DefDatabase, from_module: ModuleId) -> bool {
|
2019-12-24 14:23:22 -06:00
|
|
|
let to_module = match self {
|
2019-12-26 09:00:10 -06:00
|
|
|
Visibility::Module(m) => m,
|
|
|
|
Visibility::Public => return true,
|
2019-12-24 14:23:22 -06:00
|
|
|
};
|
|
|
|
// if they're not in the same crate, it can't be visible
|
|
|
|
if from_module.krate != to_module.krate {
|
|
|
|
return false;
|
|
|
|
}
|
2021-01-22 09:31:40 -06:00
|
|
|
let def_map = from_module.def_map(db);
|
2019-12-27 04:24:31 -06:00
|
|
|
self.is_visible_from_def_map(&def_map, from_module.local_id)
|
2019-12-25 11:05:16 -06:00
|
|
|
}
|
|
|
|
|
2019-12-27 04:24:31 -06:00
|
|
|
pub(crate) fn is_visible_from_other_crate(self) -> bool {
|
2019-12-26 09:31:38 -06:00
|
|
|
match self {
|
|
|
|
Visibility::Module(_) => false,
|
|
|
|
Visibility::Public => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 04:24:31 -06:00
|
|
|
pub(crate) fn is_visible_from_def_map(
|
2019-12-25 11:05:16 -06:00
|
|
|
self,
|
2021-01-18 13:18:05 -06:00
|
|
|
def_map: &DefMap,
|
2019-12-25 11:05:16 -06:00
|
|
|
from_module: crate::LocalModuleId,
|
|
|
|
) -> bool {
|
|
|
|
let to_module = match self {
|
2019-12-26 09:00:10 -06:00
|
|
|
Visibility::Module(m) => m,
|
|
|
|
Visibility::Public => return true,
|
2019-12-25 11:05:16 -06:00
|
|
|
};
|
|
|
|
// from_module needs to be a descendant of to_module
|
2019-12-24 14:23:22 -06:00
|
|
|
let mut ancestors = std::iter::successors(Some(from_module), |m| {
|
2019-12-25 11:05:16 -06:00
|
|
|
let parent_id = def_map[*m].parent?;
|
|
|
|
Some(parent_id)
|
2019-12-24 14:23:22 -06:00
|
|
|
});
|
2019-12-25 11:05:16 -06:00
|
|
|
ancestors.any(|m| m == to_module.local_id)
|
2019-12-24 14:23:22 -06:00
|
|
|
}
|
2020-07-21 10:52:43 -05:00
|
|
|
|
|
|
|
/// Returns the most permissive visibility of `self` and `other`.
|
|
|
|
///
|
|
|
|
/// If there is no subset relation between `self` and `other`, returns `None` (ie. they're only
|
|
|
|
/// visible in unrelated modules).
|
2021-01-18 13:18:05 -06:00
|
|
|
pub(crate) fn max(self, other: Visibility, def_map: &DefMap) -> Option<Visibility> {
|
2020-07-21 10:52:43 -05:00
|
|
|
match (self, other) {
|
|
|
|
(Visibility::Module(_), Visibility::Public)
|
|
|
|
| (Visibility::Public, Visibility::Module(_))
|
|
|
|
| (Visibility::Public, Visibility::Public) => Some(Visibility::Public),
|
|
|
|
(Visibility::Module(mod_a), Visibility::Module(mod_b)) => {
|
|
|
|
if mod_a.krate != mod_b.krate {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut a_ancestors = std::iter::successors(Some(mod_a.local_id), |m| {
|
|
|
|
let parent_id = def_map[*m].parent?;
|
|
|
|
Some(parent_id)
|
|
|
|
});
|
|
|
|
let mut b_ancestors = std::iter::successors(Some(mod_b.local_id), |m| {
|
|
|
|
let parent_id = def_map[*m].parent?;
|
|
|
|
Some(parent_id)
|
|
|
|
});
|
|
|
|
|
|
|
|
if a_ancestors.any(|m| m == mod_b.local_id) {
|
|
|
|
// B is above A
|
|
|
|
return Some(Visibility::Module(mod_b));
|
|
|
|
}
|
|
|
|
|
|
|
|
if b_ancestors.any(|m| m == mod_a.local_id) {
|
|
|
|
// A is above B
|
|
|
|
return Some(Visibility::Module(mod_a));
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|