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 BuiltinType
); );
pub use hir_def::{attr::Attrs, visibility::ResolvedVisibility}; pub use hir_def::{attr::Attrs, visibility::Visibility};
impl Module { impl Module {
pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module { pub(crate) fn new(krate: Crate, crate_module_id: LocalModuleId) -> Module {
@ -256,7 +256,7 @@ impl StructField {
} }
impl HasVisibility for 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 struct_field_id: hir_def::StructFieldId = (*self).into();
let visibility = db.visibility(struct_field_id.into()); let visibility = db.visibility(struct_field_id.into());
let parent_id: hir_def::VariantId = self.parent.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 { 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 { fn visible_from(&self, db: &impl HirDatabase, module: Module) -> bool {
let vis = self.visibility(db); let vis = self.visibility(db);
vis.visible_from(db, module.id) vis.visible_from(db, module.id)

View file

@ -543,7 +543,7 @@ where
}; };
self.body.item_scope.define_def(def); self.body.item_scope.define_def(def);
if let Some(name) = name { 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 self.body
.item_scope .item_scope
.push_res(name.as_name(), crate::per_ns::PerNs::from_def(def, vis)); .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 rustc_hash::FxHashMap;
use crate::{ use crate::{
per_ns::PerNs, visibility::ResolvedVisibility, AdtId, BuiltinType, ImplId, MacroDefId, per_ns::PerNs, visibility::Visibility, AdtId, BuiltinType, ImplId, MacroDefId, ModuleDefId,
ModuleDefId, TraitId, TraitId,
}; };
#[derive(Debug, Default, PartialEq, Eq)] #[derive(Debug, Default, PartialEq, Eq)]
@ -33,9 +33,7 @@ pub struct ItemScope {
static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| { static BUILTIN_SCOPE: Lazy<FxHashMap<Name, PerNs>> = Lazy::new(|| {
BuiltinType::ALL BuiltinType::ALL
.iter() .iter()
.map(|(name, ty)| { .map(|(name, ty)| (name.clone(), PerNs::types(ty.clone().into(), Visibility::Public)))
(name.clone(), PerNs::types(ty.clone().into(), ResolvedVisibility::Public))
})
.collect() .collect()
}); });
@ -159,7 +157,7 @@ impl ItemScope {
} }
impl PerNs { impl PerNs {
pub(crate) fn from_def(def: ModuleDefId, v: ResolvedVisibility) -> PerNs { pub(crate) fn from_def(def: ModuleDefId, v: Visibility) -> PerNs {
match def { match def {
ModuleDefId::ModuleId(_) => PerNs::types(def, v), ModuleDefId::ModuleId(_) => PerNs::types(def, v),
ModuleDefId::FunctionId(_) => PerNs::values(def, v), ModuleDefId::FunctionId(_) => PerNs::values(def, v),

View file

@ -24,7 +24,7 @@ use crate::{
}, },
path::{ModPath, PathKind}, path::{ModPath, PathKind},
per_ns::PerNs, per_ns::PerNs,
visibility::ResolvedVisibility, visibility::Visibility,
AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern, AdtId, AstId, ConstLoc, ContainerId, EnumLoc, EnumVariantId, FunctionLoc, ImplLoc, Intern,
LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc, LocalModuleId, ModuleDefId, ModuleId, StaticLoc, StructLoc, TraitLoc, TypeAliasLoc, UnionLoc,
}; };
@ -109,7 +109,7 @@ struct MacroDirective {
struct DefCollector<'a, DB> { struct DefCollector<'a, DB> {
db: &'a DB, db: &'a DB,
def_map: CrateDefMap, def_map: CrateDefMap,
glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, ResolvedVisibility)>>, glob_imports: FxHashMap<LocalModuleId, Vec<(LocalModuleId, Visibility)>>,
unresolved_imports: Vec<ImportDirective>, unresolved_imports: Vec<ImportDirective>,
resolved_imports: Vec<ImportDirective>, resolved_imports: Vec<ImportDirective>,
unexpanded_macros: Vec<MacroDirective>, unexpanded_macros: Vec<MacroDirective>,
@ -217,8 +217,8 @@ where
if export { if export {
self.update( self.update(
self.def_map.root, self.def_map.root,
&[(name, PerNs::macros(macro_, ResolvedVisibility::Public))], &[(name, PerNs::macros(macro_, Visibility::Public))],
ResolvedVisibility::Public, Visibility::Public,
); );
} }
} }
@ -358,7 +358,7 @@ where
let vis = self let vis = self
.def_map .def_map
.resolve_visibility(self.db, module_id, &directive.import.visibility) .resolve_visibility(self.db, module_id, &directive.import.visibility)
.unwrap_or(ResolvedVisibility::Public); .unwrap_or(Visibility::Public);
if import.is_glob { if import.is_glob {
log::debug!("glob import: {:?}", import); log::debug!("glob import: {:?}", import);
@ -461,12 +461,7 @@ where
} }
} }
fn update( fn update(&mut self, module_id: LocalModuleId, resolutions: &[(Name, PerNs)], vis: Visibility) {
&mut self,
module_id: LocalModuleId,
resolutions: &[(Name, PerNs)],
vis: ResolvedVisibility,
) {
self.update_recursive(module_id, resolutions, vis, 0) self.update_recursive(module_id, resolutions, vis, 0)
} }
@ -476,7 +471,7 @@ where
resolutions: &[(Name, PerNs)], resolutions: &[(Name, PerNs)],
// All resolutions are imported with this visibility; the visibilies in // All resolutions are imported with this visibility; the visibilies in
// the `PerNs` values are ignored and overwritten // the `PerNs` values are ignored and overwritten
vis: ResolvedVisibility, vis: Visibility,
depth: usize, depth: usize,
) { ) {
if depth > 100 { if depth > 100 {
@ -749,7 +744,7 @@ where
.def_collector .def_collector
.def_map .def_map
.resolve_visibility(self.def_collector.db, self.module_id, visibility) .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 modules = &mut self.def_collector.def_map.modules;
let res = modules.alloc(ModuleData::default()); let res = modules.alloc(ModuleData::default());
modules[res].parent = Some(self.module_id); modules[res].parent = Some(self.module_id);
@ -825,7 +820,7 @@ where
.def_collector .def_collector
.def_map .def_map
.resolve_visibility(self.def_collector.db, self.module_id, vis) .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) 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}, nameres::{BuiltinShadowMode, CrateDefMap},
path::{ModPath, PathKind}, path::{ModPath, PathKind},
per_ns::PerNs, per_ns::PerNs,
visibility::{RawVisibility, ResolvedVisibility}, visibility::{RawVisibility, Visibility},
AdtId, CrateId, EnumVariantId, LocalModuleId, ModuleDefId, ModuleId, AdtId, CrateId, EnumVariantId, LocalModuleId, ModuleDefId, ModuleId,
}; };
@ -64,7 +64,7 @@ impl CrateDefMap {
pub(super) fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs { pub(super) fn resolve_name_in_extern_prelude(&self, name: &Name) -> PerNs {
self.extern_prelude self.extern_prelude
.get(name) .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( pub(crate) fn resolve_visibility(
@ -72,7 +72,7 @@ impl CrateDefMap {
db: &impl DefDatabase, db: &impl DefDatabase,
original_module: LocalModuleId, original_module: LocalModuleId,
visibility: &RawVisibility, visibility: &RawVisibility,
) -> Option<ResolvedVisibility> { ) -> Option<Visibility> {
match visibility { match visibility {
RawVisibility::Module(path) => { RawVisibility::Module(path) => {
let (result, remaining) = let (result, remaining) =
@ -82,14 +82,14 @@ impl CrateDefMap {
} }
let types = result.take_types()?; let types = result.take_types()?;
match types { match types {
ModuleDefId::ModuleId(m) => Some(ResolvedVisibility::Module(m)), ModuleDefId::ModuleId(m) => Some(Visibility::Module(m)),
_ => { _ => {
// error: visibility needs to refer to module // error: visibility needs to refer to module
None None
} }
} }
} }
RawVisibility::Public => Some(ResolvedVisibility::Public), RawVisibility::Public => Some(Visibility::Public),
} }
} }
@ -119,18 +119,18 @@ impl CrateDefMap {
tested_by!(macro_dollar_crate_self); tested_by!(macro_dollar_crate_self);
PerNs::types( PerNs::types(
ModuleId { krate: self.krate, local_id: self.root }.into(), ModuleId { krate: self.krate, local_id: self.root }.into(),
ResolvedVisibility::Public, Visibility::Public,
) )
} else { } else {
let def_map = db.crate_def_map(krate); let def_map = db.crate_def_map(krate);
let module = ModuleId { krate, local_id: def_map.root }; let module = ModuleId { krate, local_id: def_map.root };
tested_by!(macro_dollar_crate_other); tested_by!(macro_dollar_crate_other);
PerNs::types(module.into(), ResolvedVisibility::Public) PerNs::types(module.into(), Visibility::Public)
} }
} }
PathKind::Crate => PerNs::types( PathKind::Crate => PerNs::types(
ModuleId { krate: self.krate, local_id: self.root }.into(), ModuleId { krate: self.krate, local_id: self.root }.into(),
ResolvedVisibility::Public, Visibility::Public,
), ),
// plain import or absolute path in 2015: crate-relative with // plain import or absolute path in 2015: crate-relative with
// fallback to extern prelude (with the simplification in // fallback to extern prelude (with the simplification in
@ -161,7 +161,7 @@ impl CrateDefMap {
if let Some(local_id) = m { if let Some(local_id) = m {
PerNs::types( PerNs::types(
ModuleId { krate: self.krate, local_id }.into(), ModuleId { krate: self.krate, local_id }.into(),
ResolvedVisibility::Public, Visibility::Public,
) )
} else { } else {
log::debug!("super path in root module"); log::debug!("super path in root module");
@ -176,7 +176,7 @@ impl CrateDefMap {
}; };
if let Some(def) = self.extern_prelude.get(&segment) { if let Some(def) = self.extern_prelude.get(&segment) {
log::debug!("absolute path {:?} resolved to crate {:?}", path, def); log::debug!("absolute path {:?} resolved to crate {:?}", path, def);
PerNs::types(*def, ResolvedVisibility::Public) PerNs::types(*def, Visibility::Public)
} else { } else {
return ResolvePathResult::empty(ReachedFixedPoint::No); // extern crate declarations can add to the extern prelude 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) { match enum_data.variant(&segment) {
Some(local_id) => { Some(local_id) => {
let variant = EnumVariantId { parent: e, 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 => { None => {
return ResolvePathResult::with( return ResolvePathResult::with(
@ -274,12 +274,12 @@ impl CrateDefMap {
let from_legacy_macro = self[module] let from_legacy_macro = self[module]
.scope .scope
.get_legacy_macro(name) .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_scope = self[module].scope.get(name, shadow);
let from_extern_prelude = self let from_extern_prelude = self
.extern_prelude .extern_prelude
.get(name) .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); let from_prelude = self.resolve_in_prelude(db, name, shadow);
from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude) from_legacy_macro.or(from_scope).or(from_extern_prelude).or(from_prelude)

View file

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

View file

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

View file

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