Rename ResolvedVisibility -> Visibility

This commit is contained in:
Florian Diebold 2019-12-26 16:00:10 +01:00
parent e1a2961273
commit 50ebff257d
8 changed files with 54 additions and 67 deletions

View File

@ -118,7 +118,7 @@ impl_froms!(
BuiltinType
);
pub use hir_def::{attr::Attrs, visibility::ResolvedVisibility};
pub use hir_def::{attr::Attrs, visibility::Visibility};
impl Module {
pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module {
@ -256,7 +256,7 @@ impl StructField {
}
impl HasVisibility for StructField {
fn visibility(&self, db: &impl HirDatabase) -> ResolvedVisibility {
fn visibility(&self, db: &impl HirDatabase) -> Visibility {
let struct_field_id: hir_def::StructFieldId = (*self).into();
let visibility = db.visibility(struct_field_id.into());
let parent_id: hir_def::VariantId = self.parent.into();
@ -1052,7 +1052,7 @@ impl<T: Into<AttrDef> + Copy> Docs for T {
}
pub trait HasVisibility {
fn visibility(&self, db: &impl HirDatabase) -> ResolvedVisibility;
fn visibility(&self, db: &impl HirDatabase) -> Visibility;
fn visible_from(&self, db: &impl HirDatabase, module: Module) -> bool {
let vis = self.visibility(db);
vis.visible_from(db, module.id)

View File

@ -543,7 +543,7 @@ where
};
self.body.item_scope.define_def(def);
if let Some(name) = name {
let vis = crate::visibility::ResolvedVisibility::Public; // FIXME determine correctly
let vis = crate::visibility::Visibility::Public; // FIXME determine correctly
self.body
.item_scope
.push_res(name.as_name(), crate::per_ns::PerNs::from_def(def, vis));

View File

@ -6,8 +6,8 @@ use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;
use crate::{
per_ns::PerNs, visibility::ResolvedVisibility, AdtId, BuiltinType, ImplId, MacroDefId,
ModuleDefId, TraitId,
per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ImplId, MacroDefId, ModuleDefId,
TraitId,
};
#[derive(Debug, Default, PartialEq, Eq)]
@ -33,9 +33,7 @@ pub struct ItemScope {
static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| {
BuiltinType::ALL
.iter()
.map(|(name, ty)| {
(name.clone(), PerNs::types(ty.clone().into(), ResolvedVisibility::Public))
})
.map(|(name, ty)| (name.clone(), PerNs::types(ty.clone().into(), Visibility::Public)))
.collect()
});
@ -159,7 +157,7 @@ impl ItemScope {
}
impl PerNs {
pub(crate) fn from_def(def: ModuleDefId, v: ResolvedVisibility) -> PerNs {
pub(crate) fn from_def(def: ModuleDefId, v: Visibility) -> PerNs {
match def {
ModuleDefId::ModuleId(_) => PerNs::types(def, v),
ModuleDefId::FunctionId(_) => PerNs::values(def, v),

View File

@ -24,7 +24,7 @@ use crate::{
},
path::{ModPath, PathKind},
per_ns::PerNs,
visibility::ResolvedVisibility,
visibility::Visibility,
AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
};
@ -109,7 +109,7 @@ struct MacroDirective {
struct DefCollector<'a, DB> {
db: &'a DB,
def_map: CrateDefMap,
glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, ResolvedVisibility)>>,
glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, Visibility)>>,
unresolved_imports: Vec<ImportDirective>,
resolved_imports: Vec<ImportDirective>,
unexpanded_macros: Vec<MacroDirective>,
@ -217,8 +217,8 @@ where
if export {
self.update(
self.def_map.root,
&[(name, PerNs::macros(macro_, ResolvedVisibility::Public))],
ResolvedVisibility::Public,
&[(name, PerNs::macros(macro_, Visibility::Public))],
Visibility::Public,
);
}
}
@ -358,7 +358,7 @@ where
let vis = self
.def_map
.resolve_visibility(self.db, module_id, &directive.import.visibility)
.unwrap_or(ResolvedVisibility::Public);
.unwrap_or(Visibility::Public);
if import.is_glob {
log::debug!("glob import: {:?}", import);
@ -461,12 +461,7 @@ where
}
}
fn update(
&mut self,
module_id: LocalModuleId,
resolutions: &[(Name, PerNs)],
vis: ResolvedVisibility,
) {
fn update(&mut self, module_id: LocalModuleId, resolutions: &[(Name, PerNs)], vis: Visibility) {
self.update_recursive(module_id, resolutions, vis, 0)
}
@ -476,7 +471,7 @@ where
resolutions: &[(Name, PerNs)],
// All resolutions are imported with this visibility; the visibilies in
// the `PerNs` values are ignored and overwritten
vis: ResolvedVisibility,
vis: Visibility,
depth: usize,
) {
if depth > 100 {
@ -749,7 +744,7 @@ where
.def_collector
.def_map
.resolve_visibility(self.def_collector.db, self.module_id, visibility)
.unwrap_or(ResolvedVisibility::Public);
.unwrap_or(Visibility::Public);
let modules = &mut self.def_collector.def_map.modules;
let res = modules.alloc(ModuleData::default());
modules[res].parent = Some(self.module_id);
@ -825,7 +820,7 @@ where
.def_collector
.def_map
.resolve_visibility(self.def_collector.db, self.module_id, vis)
.unwrap_or(ResolvedVisibility::Public);
.unwrap_or(Visibility::Public);
self.def_collector.update(self.module_id, &[(name, PerNs::from_def(def, vis))], vis)
}

View File

@ -21,7 +21,7 @@ use crate::{
nameres::{BuiltinShadowMode, CrateDefMap},
path::{ModPath, PathKind},
per_ns::PerNs,
visibility::{RawVisibility, ResolvedVisibility},
visibility::{RawVisibility, Visibility},
AdtId, CrateId, EnumVariantId, LocalModuleId, ModuleDefId, ModuleId,
};
@ -64,7 +64,7 @@ impl CrateDefMap {
pub(super) fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs {
self.extern_prelude
.get(name)
.map_or(PerNs::none(), |&it| PerNs::types(it, ResolvedVisibility::Public))
.map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public))
}
pub(crate) fn resolve_visibility(
@ -72,7 +72,7 @@ impl CrateDefMap {
db: &impl DefDatabase,
original_module: LocalModuleId,
visibility: &RawVisibility,
) -> Option<ResolvedVisibility> {
) -> Option<Visibility> {
match visibility {
RawVisibility::Module(path) => {
let (result, remaining) =
@ -82,14 +82,14 @@ impl CrateDefMap {
}
let types = result.take_types()?;
match types {
ModuleDefId::ModuleId(m) => Some(ResolvedVisibility::Module(m)),
ModuleDefId::ModuleId(m) => Some(Visibility::Module(m)),
_ => {
// error: visibility needs to refer to module
None
}
}
}
RawVisibility::Public => Some(ResolvedVisibility::Public),
RawVisibility::Public => Some(Visibility::Public),
}
}
@ -119,18 +119,18 @@ impl CrateDefMap {
tested_by!(macro_dollar_crate_self);
PerNs::types(
ModuleId { krate: self.krate, local_id: self.root }.into(),
ResolvedVisibility::Public,
Visibility::Public,
)
} else {
let def_map = db.crate_def_map(krate);
let module = ModuleId { krate, local_id: def_map.root };
tested_by!(macro_dollar_crate_other);
PerNs::types(module.into(), ResolvedVisibility::Public)
PerNs::types(module.into(), Visibility::Public)
}
}
PathKind::Crate => PerNs::types(
ModuleId { krate: self.krate, local_id: self.root }.into(),
ResolvedVisibility::Public,
Visibility::Public,
),
// plain import or absolute path in 2015: crate-relative with
// fallback to extern prelude (with the simplification in
@ -161,7 +161,7 @@ impl CrateDefMap {
if let Some(local_id) = m {
PerNs::types(
ModuleId { krate: self.krate, local_id }.into(),
ResolvedVisibility::Public,
Visibility::Public,
)
} else {
log::debug!("super path in root module");
@ -176,7 +176,7 @@ impl CrateDefMap {
};
if let Some(def) = self.extern_prelude.get(&segment) {
log::debug!("absolute path {:?} resolved to crate {:?}", path, def);
PerNs::types(*def, ResolvedVisibility::Public)
PerNs::types(*def, Visibility::Public)
} else {
return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude
}
@ -225,7 +225,7 @@ impl CrateDefMap {
match enum_data.variant(&segment) {
Some(local_id) => {
let variant = EnumVariantId { parent: e, local_id };
PerNs::both(variant.into(), variant.into(), ResolvedVisibility::Public)
PerNs::both(variant.into(), variant.into(), Visibility::Public)
}
None => {
return ResolvePathResult::with(
@ -274,12 +274,12 @@ impl CrateDefMap {
let from_legacy_macro = self[module]
.scope
.get_legacy_macro(name)
.map_or_else(PerNs::none, |m| PerNs::macros(m, ResolvedVisibility::Public));
.map_or_else(PerNs::none, |m| PerNs::macros(m, Visibility::Public));
let from_scope = self[module].scope.get(name, shadow);
let from_extern_prelude = self
.extern_prelude
.get(name)
.map_or(PerNs::none(), |&it| PerNs::types(it, ResolvedVisibility::Public));
.map_or(PerNs::none(), |&it| PerNs::types(it, Visibility::Public));
let from_prelude = self.resolve_in_prelude(db, name, shadow);
from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude)

View File

@ -5,13 +5,13 @@
use hir_expand::MacroDefId;
use crate::{visibility::ResolvedVisibility, ModuleDefId};
use crate::{visibility::Visibility, ModuleDefId};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct PerNs {
pub types: Option<(ModuleDefId, ResolvedVisibility)>,
pub values: Option<(ModuleDefId, ResolvedVisibility)>,
pub macros: Option<(MacroDefId, ResolvedVisibility)>,
pub types: Option<(ModuleDefId, Visibility)>,
pub values: Option<(ModuleDefId, Visibility)>,
pub macros: Option<(MacroDefId, Visibility)>,
}
impl Default for PerNs {
@ -25,19 +25,19 @@ impl PerNs {
PerNs { types: None, values: None, macros: None }
}
pub fn values(t: ModuleDefId, v: ResolvedVisibility) -> PerNs {
pub fn values(t: ModuleDefId, v: Visibility) -> PerNs {
PerNs { types: None, values: Some((t, v)), macros: None }
}
pub fn types(t: ModuleDefId, v: ResolvedVisibility) -> PerNs {
pub fn types(t: ModuleDefId, v: Visibility) -> PerNs {
PerNs { types: Some((t, v)), values: None, macros: None }
}
pub fn both(types: ModuleDefId, values: ModuleDefId, v: ResolvedVisibility) -> PerNs {
pub fn both(types: ModuleDefId, values: ModuleDefId, v: Visibility) -> PerNs {
PerNs { types: Some((types, v)), values: Some((values, v)), macros: None }
}
pub fn macros(macro_: MacroDefId, v: ResolvedVisibility) -> PerNs {
pub fn macros(macro_: MacroDefId, v: Visibility) -> PerNs {
PerNs { types: None, values: None, macros: Some((macro_, v)) }
}
@ -49,7 +49,7 @@ impl PerNs {
self.types.map(|it| it.0)
}
pub fn take_types_vis(self) -> Option<(ModuleDefId, ResolvedVisibility)> {
pub fn take_types_vis(self) -> Option<(ModuleDefId, Visibility)> {
self.types
}
@ -61,7 +61,7 @@ impl PerNs {
self.macros.map(|it| it.0)
}
pub fn filter_visibility(self, mut f: impl FnMut(ResolvedVisibility) -> bool) -> PerNs {
pub fn filter_visibility(self, mut f: impl FnMut(Visibility) -> bool) -> PerNs {
PerNs {
types: self.types.filter(|(_, v)| f(*v)),
values: self.values.filter(|(_, v)| f(*v)),
@ -69,7 +69,7 @@ impl PerNs {
}
}
pub fn with_visibility(self, vis: ResolvedVisibility) -> PerNs {
pub fn with_visibility(self, vis: Visibility) -> PerNs {
PerNs {
types: self.types.map(|(it, _)| (it, vis)),
values: self.values.map(|(it, _)| (it, vis)),

View File

@ -19,7 +19,7 @@ use crate::{
nameres::CrateDefMap,
path::{ModPath, PathKind},
per_ns::PerNs,
visibility::{RawVisibility, ResolvedVisibility},
visibility::{RawVisibility, Visibility},
AdtId, AssocContainerId, ConstId, ContainerId, DefWithBodyId, EnumId, EnumVariantId,
FunctionId, GenericDefId, HasModule, ImplId, LocalModuleId, Lookup, ModuleDefId, ModuleId,
StaticId, StructId, TraitId, TypeAliasId, TypeParamId, VariantId,
@ -236,7 +236,7 @@ impl Resolver {
&self,
db: &impl DefDatabase,
visibility: &RawVisibility,
) -> Option<ResolvedVisibility> {
) -> Option<Visibility> {
match visibility {
RawVisibility::Module(_) => {
let (item_map, module) = match self.module() {
@ -245,7 +245,7 @@ impl Resolver {
};
item_map.resolve_visibility(db, module, visibility)
}
RawVisibility::Public => Some(ResolvedVisibility::Public),
RawVisibility::Public => Some(Visibility::Public),
}
}
@ -466,16 +466,10 @@ impl Scope {
f(name.clone(), ScopeDef::PerNs(def));
});
m.crate_def_map[m.module_id].scope.legacy_macros().for_each(|(name, macro_)| {
f(
name.clone(),
ScopeDef::PerNs(PerNs::macros(macro_, ResolvedVisibility::Public)),
);
f(name.clone(), ScopeDef::PerNs(PerNs::macros(macro_, Visibility::Public)));
});
m.crate_def_map.extern_prelude.iter().for_each(|(name, &def)| {
f(
name.clone(),
ScopeDef::PerNs(PerNs::types(def.into(), ResolvedVisibility::Public)),
);
f(name.clone(), ScopeDef::PerNs(PerNs::types(def.into(), Visibility::Public)));
});
if let Some(prelude) = m.crate_def_map.prelude {
let prelude_def_map = db.crate_def_map(prelude.krate);

View File

@ -109,26 +109,26 @@ impl RawVisibility {
&self,
db: &impl DefDatabase,
resolver: &crate::resolver::Resolver,
) -> ResolvedVisibility {
) -> Visibility {
// we fall back to public visibility (i.e. fail open) if the path can't be resolved
resolver.resolve_visibility(db, self).unwrap_or(ResolvedVisibility::Public)
resolver.resolve_visibility(db, self).unwrap_or(Visibility::Public)
}
}
/// Visibility of an item, with the path resolved.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ResolvedVisibility {
pub enum Visibility {
/// Visibility is restricted to a certain module.
Module(ModuleId),
/// Visibility is unrestricted.
Public,
}
impl ResolvedVisibility {
impl Visibility {
pub fn visible_from(self, db: &impl DefDatabase, from_module: ModuleId) -> bool {
let to_module = match self {
ResolvedVisibility::Module(m) => m,
ResolvedVisibility::Public => return true,
Visibility::Module(m) => m,
Visibility::Public => return true,
};
// if they're not in the same crate, it can't be visible
if from_module.krate != to_module.krate {
@ -144,8 +144,8 @@ impl ResolvedVisibility {
from_module: crate::LocalModuleId,
) -> bool {
let to_module = match self {
ResolvedVisibility::Module(m) => m,
ResolvedVisibility::Public => return true,
Visibility::Module(m) => m,
Visibility::Public => return true,
};
// from_module needs to be a descendant of to_module
let mut ancestors = std::iter::successors(Some(from_module), |m| {