rustc: middle: move some types from resolve to privacy.

This commit is contained in:
Eduard Burtescu 2014-12-19 00:26:54 +02:00
parent 5d1257a760
commit a74a050c44
2 changed files with 70 additions and 69 deletions

View File

@ -11,17 +11,20 @@
//! A pass that checks to make sure private fields and methods aren't used
//! 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 self::PrivacyResult::*;
use self::FieldName::*;
use std::mem::replace;
use metadata::csearch;
use middle::{def, resolve};
use middle::def;
use middle::ty::{mod, Ty};
use middle::ty::{MethodCall, MethodMap, MethodOrigin, MethodParam, MethodTypeParam};
use middle::ty::{MethodStatic, MethodStaticUnboxedClosure, MethodObject, MethodTraitObject};
use util::nodemap::{NodeMap, NodeSet};
use util::nodemap::{DefIdSet, NodeMap, NodeSet};
use syntax::{ast, ast_map};
use syntax::ast_util::{is_local, local_def, PostExpansionMethod};
@ -34,11 +37,54 @@ type Context<'a, 'tcx> = (&'a MethodMap<'tcx>, &'a def::ExportMap);
/// A set of AST nodes exported by the crate.
pub type ExportedItems = NodeSet;
/// A set containing all exported definitions from external crates.
/// The set does not contain any entries from local crates.
pub type ExternalExports = DefIdSet;
/// A set of AST nodes that are fully public in the crate. This map is used for
/// documentation purposes (reexporting a private struct inlines the doc,
/// reexporting a public struct doesn't inline the doc).
pub type PublicItems = NodeSet;
// FIXME: dox
pub type LastPrivateMap = NodeMap<LastPrivate>;
#[deriving(Copy, Show)]
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},
}
#[deriving(Copy, Show)]
pub enum PrivateDep {
AllPublic,
DependsOn(ast::DefId),
}
// How an import is used.
#[deriving(Copy, PartialEq, Show)]
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,
}
}
}
/// Result of a checking operation - None => no errors were found. Some => an
/// error and contains the span and message for reporting that error and
/// optionally the same for a note about the error.
@ -362,8 +408,8 @@ struct PrivacyVisitor<'a, 'tcx: 'a> {
curitem: ast::NodeId,
in_foreign: bool,
parents: NodeMap<ast::NodeId>,
external_exports: resolve::ExternalExports,
last_private_map: resolve::LastPrivateMap,
external_exports: ExternalExports,
last_private_map: LastPrivateMap,
}
enum PrivacyResult {
@ -719,25 +765,25 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
};
match self.last_private_map[path_id] {
resolve::LastMod(resolve::AllPublic) => {},
resolve::LastMod(resolve::DependsOn(def)) => {
LastMod(AllPublic) => {},
LastMod(DependsOn(def)) => {
self.report_error(ck_public(def));
},
resolve::LastImport{value_priv,
value_used: check_value,
type_priv,
type_used: check_type} => {
LastImport { value_priv,
value_used: check_value,
type_priv,
type_used: check_type } => {
// This dance with found_error is because we don't want to report
// a privacy error twice for the same directive.
let found_error = match (type_priv, check_type) {
(Some(resolve::DependsOn(def)), resolve::Used) => {
(Some(DependsOn(def)), Used) => {
!self.report_error(ck_public(def))
},
_ => false,
};
if !found_error {
match (value_priv, check_value) {
(Some(resolve::DependsOn(def)), resolve::Used) => {
(Some(DependsOn(def)), Used) => {
self.report_error(ck_public(def));
},
_ => {},
@ -749,24 +795,24 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
// be illegal. We only report one error, even if it is
// illegal to import from both namespaces.
match (value_priv, check_value, type_priv, check_type) {
(Some(p), resolve::Unused, None, _) |
(None, _, Some(p), resolve::Unused) => {
(Some(p), Unused, None, _) |
(None, _, Some(p), Unused) => {
let p = match p {
resolve::AllPublic => None,
resolve::DependsOn(def) => ck_public(def),
AllPublic => None,
DependsOn(def) => ck_public(def),
};
if p.is_some() {
self.report_error(p);
}
},
(Some(v), resolve::Unused, Some(t), resolve::Unused) => {
(Some(v), Unused, Some(t), Unused) => {
let v = match v {
resolve::AllPublic => None,
resolve::DependsOn(def) => ck_public(def),
AllPublic => None,
DependsOn(def) => ck_public(def),
};
let t = match t {
resolve::AllPublic => None,
resolve::DependsOn(def) => ck_public(def),
AllPublic => None,
DependsOn(def) => ck_public(def),
};
if let (Some(_), Some(t)) = (v, t) {
self.report_error(Some(t));
@ -1521,8 +1567,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
pub fn check_crate(tcx: &ty::ctxt,
export_map: &def::ExportMap,
external_exports: resolve::ExternalExports,
last_private_map: resolve::LastPrivateMap)
external_exports: ExternalExports,
last_private_map: LastPrivateMap)
-> (ExportedItems, PublicItems) {
let krate = tcx.map.krate();

View File

@ -8,9 +8,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.
pub use self::PrivateDep::*;
pub use self::ImportUse::*;
pub use self::LastPrivate::*;
use self::PatternBindingMode::*;
use self::Namespace::*;
use self::NamespaceError::*;
@ -40,6 +37,7 @@ use metadata::decoder::{DefLike, DlDef, DlField, DlImpl};
use middle::def::*;
use middle::lang_items::LanguageItems;
use middle::pat_util::pat_bindings;
use middle::privacy::*;
use middle::subst::{ParamSpace, FnSpace, TypeSpace};
use middle::ty::{CaptureModeMap, Freevar, FreevarMap, TraitMap};
use util::nodemap::{NodeMap, NodeSet, DefIdSet, FnvHashMap};
@ -91,49 +89,6 @@ struct BindingInfo {
// Map from the name in a pattern to its binding mode.
type BindingMap = HashMap<Name, BindingInfo>;
// This set contains all exported definitions from external crates. The set does
// not contain any entries from local crates.
pub type ExternalExports = DefIdSet;
// FIXME: dox
pub type LastPrivateMap = NodeMap<LastPrivate>;
#[deriving(Copy, Show)]
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},
}
#[deriving(Copy, Show)]
pub enum PrivateDep {
AllPublic,
DependsOn(DefId),
}
// How an import is used.
#[deriving(Copy, PartialEq, Show)]
pub enum ImportUse {
Unused, // The import is not used.
Used, // The import is used.
}
impl LastPrivate {
fn or(self, other: LastPrivate) -> LastPrivate {
match (self, other) {
(me, LastMod(AllPublic)) => me,
(_, other) => other,
}
}
}
#[deriving(Copy, PartialEq)]
enum PatternBindingMode {
RefutableMode,