Use the node id from the Restricted variant when checking accessibility

in `typeck` and in `privacy::PrivacyVisitor`.
This commit is contained in:
Jeffrey Seyfried 2016-03-31 07:03:00 +00:00
parent bb66d91c98
commit 0d34c5dd8a
6 changed files with 34 additions and 42 deletions

View file

@ -440,18 +440,6 @@ impl<'ast> Map<'ast> {
}
}
pub fn private_item_is_visible_from(&self, item: NodeId, block: NodeId) -> bool {
// A private item is visible from everything in its nearest module parent.
let visibility = self.get_module_parent(item);
let mut block_ancestor = self.get_module_parent(block);
loop {
if block_ancestor == visibility { return true }
let block_ancestor_parent = self.get_module_parent(block_ancestor);
if block_ancestor_parent == block_ancestor { return false }
block_ancestor = block_ancestor_parent;
}
}
/// Returns the nearest enclosing scope. A scope is an item or block.
/// FIXME it is not clear to me that all items qualify as scopes - statics
/// and associated types probably shouldn't, for example. Behaviour in this

View file

@ -290,6 +290,26 @@ impl Visibility {
hir::Inherited => Visibility::Restricted(tcx.map.get_module_parent(id)),
}
}
/// Returns true if an item with this visibility is accessible from the given block.
pub fn is_accessible_from(self, block: NodeId, map: &ast_map::Map) -> bool {
let restriction = match self {
// Public items are visible everywhere.
Visibility::Public => return true,
// Private items from other crates are visible nowhere.
Visibility::PrivateExternal => return false,
// Restricted items are visible in an arbitrary local module.
Visibility::Restricted(module) => module,
};
let mut block_ancestor = map.get_module_parent(block);
loop {
if block_ancestor == restriction { return true }
let block_ancestor_parent = map.get_module_parent(block_ancestor);
if block_ancestor_parent == block_ancestor { return false }
block_ancestor = block_ancestor_parent;
}
}
}
#[derive(Clone, Debug)]