2019-12-24 15:11:50 -06:00
|
|
|
//! Defines hir-level representation of visibility (e.g. `pub` and `pub(crate)`).
|
|
|
|
|
2023-05-02 09:12:22 -05:00
|
|
|
use std::iter;
|
2021-03-24 16:59:59 -05:00
|
|
|
|
2023-12-18 06:30:41 -06:00
|
|
|
use hir_expand::{span_map::SpanMapRef, InFile};
|
2021-03-24 16:59:59 -05:00
|
|
|
use la_arena::ArenaMap;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::ast;
|
2023-05-02 09:12:22 -05:00
|
|
|
use triomphe::Arc;
|
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},
|
2021-03-24 16:59:59 -05:00
|
|
|
resolver::HasResolver,
|
2023-02-19 04:02:51 -06:00
|
|
|
ConstId, FunctionId, HasModule, LocalFieldId, LocalModuleId, ModuleId, VariantId,
|
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)`.
|
2024-02-02 04:22:54 -06:00
|
|
|
Module(ModPath, VisibilityExplicitness),
|
2019-12-24 13:32:42 -06:00
|
|
|
/// `pub`.
|
|
|
|
Public,
|
|
|
|
}
|
|
|
|
|
2019-12-26 08:57:14 -06:00
|
|
|
impl RawVisibility {
|
2022-02-23 08:55:06 -06:00
|
|
|
pub(crate) const fn private() -> RawVisibility {
|
2024-02-02 04:22:54 -06:00
|
|
|
RawVisibility::Module(
|
|
|
|
ModPath::from_kind(PathKind::Super(0)),
|
|
|
|
VisibilityExplicitness::Implicit,
|
|
|
|
)
|
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 {
|
2024-02-10 06:49:21 -06:00
|
|
|
let node = match node.transpose() {
|
|
|
|
None => return RawVisibility::private(),
|
|
|
|
Some(node) => node,
|
|
|
|
};
|
2023-11-25 08:37:40 -06:00
|
|
|
Self::from_ast_with_span_map(db, node.value, db.span_map(node.file_id).as_ref())
|
2019-12-24 16:45:14 -06:00
|
|
|
}
|
|
|
|
|
2024-02-10 06:49:21 -06:00
|
|
|
pub(crate) fn from_opt_ast_with_span_map(
|
2021-05-06 12:59:54 -05:00
|
|
|
db: &dyn DefDatabase,
|
2019-12-24 16:45:14 -06:00
|
|
|
node: Option<ast::Visibility>,
|
2023-11-25 08:37:40 -06:00
|
|
|
span_map: SpanMapRef<'_>,
|
2019-12-26 08:57:14 -06:00
|
|
|
) -> RawVisibility {
|
2019-12-24 16:45:14 -06:00
|
|
|
let node = match node {
|
2024-01-11 04:36:10 -06:00
|
|
|
None => return RawVisibility::private(),
|
2019-12-24 13:32:42 -06:00
|
|
|
Some(node) => node,
|
|
|
|
};
|
2024-02-10 06:49:21 -06:00
|
|
|
Self::from_ast_with_span_map(db, node, span_map)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_ast_with_span_map(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
node: ast::Visibility,
|
|
|
|
span_map: SpanMapRef<'_>,
|
|
|
|
) -> RawVisibility {
|
|
|
|
let path = match node.kind() {
|
2019-12-24 13:32:42 -06:00
|
|
|
ast::VisibilityKind::In(path) => {
|
2023-11-25 08:37:40 -06:00
|
|
|
let path = ModPath::from_src(db.upcast(), path, span_map);
|
2024-02-10 06:49:21 -06:00
|
|
|
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,
|
2024-02-10 06:49:21 -06:00
|
|
|
}
|
2020-04-03 14:12:09 -05:00
|
|
|
}
|
2024-02-10 06:49:21 -06:00
|
|
|
ast::VisibilityKind::PubCrate => ModPath::from_kind(PathKind::Crate),
|
|
|
|
ast::VisibilityKind::PubSuper => ModPath::from_kind(PathKind::Super(1)),
|
|
|
|
ast::VisibilityKind::PubSelf => ModPath::from_kind(PathKind::Super(0)),
|
|
|
|
ast::VisibilityKind::Pub => return RawVisibility::Public,
|
|
|
|
};
|
|
|
|
RawVisibility::Module(path, VisibilityExplicitness::Explicit)
|
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.
|
2024-02-02 04:22:54 -06:00
|
|
|
Module(ModuleId, VisibilityExplicitness),
|
2019-12-24 14:23:22 -06:00
|
|
|
/// Visibility is unrestricted.
|
|
|
|
Public,
|
|
|
|
}
|
|
|
|
|
2019-12-26 09:00:10 -06:00
|
|
|
impl Visibility {
|
2024-02-10 06:49:21 -06:00
|
|
|
pub(crate) fn is_visible_from_other_crate(self) -> bool {
|
|
|
|
matches!(self, Visibility::Public)
|
|
|
|
}
|
|
|
|
|
2024-02-01 15:10:32 -06:00
|
|
|
#[tracing::instrument(skip_all)]
|
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 {
|
2024-01-05 04:00:29 -06:00
|
|
|
Visibility::Module(m, _) => m,
|
2019-12-26 09:00:10 -06:00
|
|
|
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);
|
2024-02-10 06:49:21 -06:00
|
|
|
Self::is_visible_from_def_map_(db, &def_map, to_module, from_module.local_id)
|
2019-12-26 09:31:38 -06:00
|
|
|
}
|
|
|
|
|
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-02-23 10:56:16 -06:00
|
|
|
db: &dyn DefDatabase,
|
2021-01-18 13:18:05 -06:00
|
|
|
def_map: &DefMap,
|
2024-02-10 06:49:21 -06:00
|
|
|
from_module: LocalModuleId,
|
2019-12-25 11:05:16 -06:00
|
|
|
) -> bool {
|
2024-02-10 06:49:21 -06:00
|
|
|
let to_module = match self {
|
2024-01-05 04:00:29 -06:00
|
|
|
Visibility::Module(m, _) => m,
|
2019-12-26 09:00:10 -06:00
|
|
|
Visibility::Public => return true,
|
2019-12-25 11:05:16 -06:00
|
|
|
};
|
2024-02-10 06:49:21 -06:00
|
|
|
// if they're not in the same crate, it can't be visible
|
|
|
|
if def_map.krate() != to_module.krate {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Self::is_visible_from_def_map_(db, def_map, to_module, from_module)
|
|
|
|
}
|
2021-02-23 10:56:16 -06:00
|
|
|
|
2024-02-10 06:49:21 -06:00
|
|
|
fn is_visible_from_def_map_(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
def_map: &DefMap,
|
|
|
|
mut to_module: ModuleId,
|
|
|
|
mut from_module: LocalModuleId,
|
|
|
|
) -> bool {
|
|
|
|
debug_assert_eq!(to_module.krate, def_map.krate());
|
2021-04-18 18:06:04 -05:00
|
|
|
// `to_module` might be the root module of a block expression. Those have the same
|
|
|
|
// visibility as the containing module (even though no items are directly nameable from
|
|
|
|
// there, getting this right is important for method resolution).
|
|
|
|
// In that case, we adjust the visibility of `to_module` to point to the containing module.
|
2023-03-14 07:42:08 -05:00
|
|
|
|
2021-07-05 09:06:50 -05:00
|
|
|
// Additional complication: `to_module` might be in `from_module`'s `DefMap`, which we're
|
|
|
|
// currently computing, so we must not call the `def_map` query for it.
|
2024-02-10 06:49:21 -06:00
|
|
|
let def_map_block = def_map.block_id();
|
2023-03-14 07:42:08 -05:00
|
|
|
loop {
|
2024-02-10 06:49:21 -06:00
|
|
|
match (to_module.block, def_map_block) {
|
|
|
|
// to_module is not a block, so there is no parent def map to use
|
|
|
|
(None, _) => (),
|
|
|
|
(Some(a), Some(b)) if a == b => {
|
2023-03-14 07:42:08 -05:00
|
|
|
cov_mark::hit!(is_visible_from_same_block_def_map);
|
2024-02-10 06:49:21 -06:00
|
|
|
if let Some(parent) = def_map.parent() {
|
|
|
|
to_module = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
if let Some(parent) = to_module.def_map(db).parent() {
|
|
|
|
to_module = parent;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2023-03-14 07:42:08 -05:00
|
|
|
}
|
2024-02-10 06:49:21 -06:00
|
|
|
break;
|
2021-04-18 18:06:04 -05:00
|
|
|
}
|
|
|
|
|
2019-12-25 11:05:16 -06:00
|
|
|
// from_module needs to be a descendant of to_module
|
2021-02-23 10:56:16 -06:00
|
|
|
let mut def_map = def_map;
|
|
|
|
let mut parent_arc;
|
|
|
|
loop {
|
|
|
|
if def_map.module_id(from_module) == to_module {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
match def_map[from_module].parent {
|
2022-02-23 08:55:06 -06:00
|
|
|
Some(parent) => from_module = parent,
|
2021-02-23 10:56:16 -06:00
|
|
|
None => {
|
|
|
|
match def_map.parent() {
|
|
|
|
Some(module) => {
|
|
|
|
parent_arc = module.def_map(db);
|
|
|
|
def_map = &*parent_arc;
|
|
|
|
from_module = module.local_id;
|
|
|
|
}
|
2022-02-23 08:55:06 -06:00
|
|
|
// Reached the root module, nothing left to check.
|
|
|
|
None => return false,
|
2021-02-23 10:56:16 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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) {
|
2024-02-10 06:49:21 -06:00
|
|
|
(_, Visibility::Public) | (Visibility::Public, _) => Some(Visibility::Public),
|
|
|
|
(Visibility::Module(mod_a, expl_a), Visibility::Module(mod_b, expl_b)) => {
|
2020-07-21 10:52:43 -05:00
|
|
|
if mod_a.krate != mod_b.krate {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2024-02-10 06:49:21 -06:00
|
|
|
let mut a_ancestors =
|
|
|
|
iter::successors(Some(mod_a.local_id), |&m| def_map[m].parent);
|
|
|
|
let mut b_ancestors =
|
|
|
|
iter::successors(Some(mod_b.local_id), |&m| def_map[m].parent);
|
2020-07-21 10:52:43 -05:00
|
|
|
|
|
|
|
if a_ancestors.any(|m| m == mod_b.local_id) {
|
|
|
|
// B is above A
|
2024-02-10 06:49:21 -06:00
|
|
|
return Some(Visibility::Module(mod_b, expl_b));
|
2020-07-21 10:52:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if b_ancestors.any(|m| m == mod_a.local_id) {
|
|
|
|
// A is above B
|
2024-02-10 06:49:21 -06:00
|
|
|
return Some(Visibility::Module(mod_a, expl_a));
|
2020-07-21 10:52:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-12-24 13:32:42 -06:00
|
|
|
}
|
2021-03-24 16:59:59 -05:00
|
|
|
|
2024-02-10 06:49:21 -06:00
|
|
|
/// Whether the item was imported through an explicit `pub(crate) use` or just a `use` without
|
|
|
|
/// visibility.
|
2024-01-11 04:36:10 -06:00
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
|
2024-02-02 04:22:54 -06:00
|
|
|
pub enum VisibilityExplicitness {
|
2024-01-05 04:00:29 -06:00
|
|
|
Explicit,
|
|
|
|
Implicit,
|
|
|
|
}
|
|
|
|
|
2024-02-02 04:22:54 -06:00
|
|
|
impl VisibilityExplicitness {
|
2024-01-05 04:00:29 -06:00
|
|
|
pub fn is_explicit(&self) -> bool {
|
|
|
|
matches!(self, Self::Explicit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 16:59:59 -05:00
|
|
|
/// Resolve visibility of all specific fields of a struct or union variant.
|
|
|
|
pub(crate) fn field_visibilities_query(
|
|
|
|
db: &dyn DefDatabase,
|
|
|
|
variant_id: VariantId,
|
|
|
|
) -> Arc<ArenaMap<LocalFieldId, Visibility>> {
|
2024-01-15 05:03:31 -06:00
|
|
|
let var_data = variant_id.variant_data(db);
|
2021-03-24 16:59:59 -05:00
|
|
|
let resolver = variant_id.module(db).resolver(db);
|
|
|
|
let mut res = ArenaMap::default();
|
|
|
|
for (field_id, field_data) in var_data.fields().iter() {
|
2022-08-06 15:48:52 -05:00
|
|
|
res.insert(field_id, field_data.visibility.resolve(db, &resolver));
|
2021-03-24 16:59:59 -05:00
|
|
|
}
|
|
|
|
Arc::new(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Resolve visibility of a function.
|
|
|
|
pub(crate) fn function_visibility_query(db: &dyn DefDatabase, def: FunctionId) -> Visibility {
|
2021-04-04 21:27:57 -05:00
|
|
|
let resolver = def.resolver(db);
|
2021-03-24 16:59:59 -05:00
|
|
|
db.function_data(def).visibility.resolve(db, &resolver)
|
|
|
|
}
|
2022-03-17 11:02:58 -05:00
|
|
|
|
|
|
|
/// Resolve visibility of a const.
|
|
|
|
pub(crate) fn const_visibility_query(db: &dyn DefDatabase, def: ConstId) -> Visibility {
|
|
|
|
let resolver = def.resolver(db);
|
|
|
|
db.const_data(def).visibility.resolve(db, &resolver)
|
|
|
|
}
|