Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check paths for privacy as they are resolved and refactor away LastPrivate #31824

Merged
merged 7 commits into from
Mar 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/librustc/front/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,30 @@ impl<'ast> Map<'ast> {
}
}

/// Returns the NodeId of `id`'s nearest module parent, or `id` itself if no
/// module parent is in this map.
fn get_module_parent(&self, id: NodeId) -> NodeId {
match self.walk_parent_nodes(id, |node| match *node {
NodeItem(&Item { node: Item_::ItemMod(_), .. }) => true,
_ => false,
}) {
Ok(id) => id,
Err(id) => id,
}
}

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
Expand Down
29 changes: 25 additions & 4 deletions src/librustc/middle/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// except according to those terms.

use middle::def_id::DefId;
use middle::privacy::LastPrivate;
use middle::subst::ParamSpace;
use util::nodemap::NodeMap;
use syntax::ast;
Expand Down Expand Up @@ -65,7 +64,6 @@ pub enum Def {
#[derive(Copy, Clone, Debug)]
pub struct PathResolution {
pub base_def: Def,
pub last_private: LastPrivate,
pub depth: usize
}

Expand All @@ -84,12 +82,10 @@ impl PathResolution {
}

pub fn new(base_def: Def,
last_private: LastPrivate,
depth: usize)
-> PathResolution {
PathResolution {
base_def: base_def,
last_private: last_private,
depth: depth,
}
}
Expand Down Expand Up @@ -152,4 +148,29 @@ impl Def {
_ => None
}
}

pub fn kind_name(&self) -> &'static str {
match *self {
Def::Fn(..) => "function",
Def::Mod(..) => "module",
Def::ForeignMod(..) => "foreign module",
Def::Static(..) => "static",
Def::Variant(..) => "variant",
Def::Enum(..) => "enum",
Def::TyAlias(..) => "type",
Def::AssociatedTy(..) => "associated type",
Def::Struct(..) => "struct",
Def::Trait(..) => "trait",
Def::Method(..) => "method",
Def::Const(..) => "const",
Def::AssociatedConst(..) => "associated const",
Def::TyParam(..) => "type parameter",
Def::PrimTy(..) => "builtin type",
Def::Local(..) => "local variable",
Def::Upvar(..) => "closure capture",
Def::Label(..) => "label",
Def::SelfTy(..) => "self type",
Def::Err => "unresolved item",
}
}
}
41 changes: 0 additions & 41 deletions src/librustc/middle/privacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@
//! outside their scopes. This pass will also generate a set of exported items
//! which are available for use externally when compiled as a library.

pub use self::PrivateDep::*;
pub use self::ImportUse::*;
pub use self::LastPrivate::*;

use middle::def_id::DefId;
use util::nodemap::{DefIdSet, FnvHashMap};

use std::hash::Hash;
Expand Down Expand Up @@ -64,39 +59,3 @@ impl<Id: Hash + Eq> Default for AccessLevels<Id> {
/// A set containing all exported definitions from external crates.
/// The set does not contain any entries from local crates.
pub type ExternalExports = DefIdSet;

#[derive(Copy, Clone, Debug)]
pub enum LastPrivate {
LastMod(PrivateDep),
// `use` directives (imports) can refer to two separate definitions in the
// type and value namespaces. We record here the last private node for each
// and whether the import is in fact used for each.
// If the Option<PrivateDep> fields are None, it means there is no definition
// in that namespace.
LastImport{value_priv: Option<PrivateDep>,
value_used: ImportUse,
type_priv: Option<PrivateDep>,
type_used: ImportUse},
}

#[derive(Copy, Clone, Debug)]
pub enum PrivateDep {
AllPublic,
DependsOn(DefId),
}

// How an import is used.
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum ImportUse {
Unused, // The import is not used.
Used, // The import is used.
}

impl LastPrivate {
pub fn or(self, other: LastPrivate) -> LastPrivate {
match (self, other) {
(me, LastMod(AllPublic)) => me,
(_, other) => other,
}
}
}
8 changes: 8 additions & 0 deletions src/librustc/middle/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ impl<'tcx> ImplOrTraitItem<'tcx> {
}
}

pub fn def(&self) -> Def {
match *self {
ConstTraitItem(ref associated_const) => Def::AssociatedConst(associated_const.def_id),
MethodTraitItem(ref method) => Def::Method(method.def_id),
TypeTraitItem(ref ty) => Def::AssociatedTy(ty.container.id(), ty.def_id),
}
}

pub fn def_id(&self) -> DefId {
match *self {
ConstTraitItem(ref associated_const) => associated_const.def_id,
Expand Down
7 changes: 2 additions & 5 deletions src/librustc_metadata/astencode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ use middle::ty::cast;
use middle::const_qualif::ConstQualif;
use middle::def::{self, Def};
use middle::def_id::DefId;
use middle::privacy::{AllPublic, LastMod};
use middle::region;
use middle::subst;
use middle::ty::{self, Ty};
Expand Down Expand Up @@ -254,7 +253,7 @@ trait def_id_encoder_helpers {
}

impl<S:serialize::Encoder> def_id_encoder_helpers for S
where <S as serialize::serialize::Encoder>::Error: Debug
where <S as serialize::Encoder>::Error: Debug
{
fn emit_def_id(&mut self, did: DefId) {
did.encode(self).unwrap()
Expand All @@ -268,7 +267,7 @@ trait def_id_decoder_helpers {
}

impl<D:serialize::Decoder> def_id_decoder_helpers for D
where <D as serialize::serialize::Decoder>::Error: Debug
where <D as serialize::Decoder>::Error: Debug
{
fn read_def_id(&mut self, dcx: &DecodeContext) -> DefId {
let did: DefId = Decodable::decode(self).unwrap();
Expand Down Expand Up @@ -1161,8 +1160,6 @@ fn decode_side_tables(dcx: &DecodeContext,
let def = decode_def(dcx, val_dsr);
dcx.tcx.def_map.borrow_mut().insert(id, def::PathResolution {
base_def: def,
// This doesn't matter cross-crate.
last_private: LastMod(AllPublic),
depth: 0
});
}
Expand Down
Loading