rustc: middle: move some types from resolve to privacy.
This commit is contained in:
parent
5d1257a760
commit
a74a050c44
|
@ -11,17 +11,20 @@
|
||||||
//! A pass that checks to make sure private fields and methods aren't used
|
//! 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
|
//! outside their scopes. This pass will also generate a set of exported items
|
||||||
//! which are available for use externally when compiled as a library.
|
//! 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::PrivacyResult::*;
|
||||||
use self::FieldName::*;
|
use self::FieldName::*;
|
||||||
|
|
||||||
use std::mem::replace;
|
use std::mem::replace;
|
||||||
|
|
||||||
use metadata::csearch;
|
use metadata::csearch;
|
||||||
use middle::{def, resolve};
|
use middle::def;
|
||||||
use middle::ty::{mod, Ty};
|
use middle::ty::{mod, Ty};
|
||||||
use middle::ty::{MethodCall, MethodMap, MethodOrigin, MethodParam, MethodTypeParam};
|
use middle::ty::{MethodCall, MethodMap, MethodOrigin, MethodParam, MethodTypeParam};
|
||||||
use middle::ty::{MethodStatic, MethodStaticUnboxedClosure, MethodObject, MethodTraitObject};
|
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, ast_map};
|
||||||
use syntax::ast_util::{is_local, local_def, PostExpansionMethod};
|
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.
|
/// A set of AST nodes exported by the crate.
|
||||||
pub type ExportedItems = NodeSet;
|
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
|
/// 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,
|
/// documentation purposes (reexporting a private struct inlines the doc,
|
||||||
/// reexporting a public struct doesn't inline the doc).
|
/// reexporting a public struct doesn't inline the doc).
|
||||||
pub type PublicItems = NodeSet;
|
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
|
/// Result of a checking operation - None => no errors were found. Some => an
|
||||||
/// error and contains the span and message for reporting that error and
|
/// error and contains the span and message for reporting that error and
|
||||||
/// optionally the same for a note about the error.
|
/// optionally the same for a note about the error.
|
||||||
|
@ -362,8 +408,8 @@ struct PrivacyVisitor<'a, 'tcx: 'a> {
|
||||||
curitem: ast::NodeId,
|
curitem: ast::NodeId,
|
||||||
in_foreign: bool,
|
in_foreign: bool,
|
||||||
parents: NodeMap<ast::NodeId>,
|
parents: NodeMap<ast::NodeId>,
|
||||||
external_exports: resolve::ExternalExports,
|
external_exports: ExternalExports,
|
||||||
last_private_map: resolve::LastPrivateMap,
|
last_private_map: LastPrivateMap,
|
||||||
}
|
}
|
||||||
|
|
||||||
enum PrivacyResult {
|
enum PrivacyResult {
|
||||||
|
@ -719,25 +765,25 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
|
||||||
};
|
};
|
||||||
|
|
||||||
match self.last_private_map[path_id] {
|
match self.last_private_map[path_id] {
|
||||||
resolve::LastMod(resolve::AllPublic) => {},
|
LastMod(AllPublic) => {},
|
||||||
resolve::LastMod(resolve::DependsOn(def)) => {
|
LastMod(DependsOn(def)) => {
|
||||||
self.report_error(ck_public(def));
|
self.report_error(ck_public(def));
|
||||||
},
|
},
|
||||||
resolve::LastImport{value_priv,
|
LastImport { value_priv,
|
||||||
value_used: check_value,
|
value_used: check_value,
|
||||||
type_priv,
|
type_priv,
|
||||||
type_used: check_type } => {
|
type_used: check_type } => {
|
||||||
// This dance with found_error is because we don't want to report
|
// This dance with found_error is because we don't want to report
|
||||||
// a privacy error twice for the same directive.
|
// a privacy error twice for the same directive.
|
||||||
let found_error = match (type_priv, check_type) {
|
let found_error = match (type_priv, check_type) {
|
||||||
(Some(resolve::DependsOn(def)), resolve::Used) => {
|
(Some(DependsOn(def)), Used) => {
|
||||||
!self.report_error(ck_public(def))
|
!self.report_error(ck_public(def))
|
||||||
},
|
},
|
||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
if !found_error {
|
if !found_error {
|
||||||
match (value_priv, check_value) {
|
match (value_priv, check_value) {
|
||||||
(Some(resolve::DependsOn(def)), resolve::Used) => {
|
(Some(DependsOn(def)), Used) => {
|
||||||
self.report_error(ck_public(def));
|
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
|
// be illegal. We only report one error, even if it is
|
||||||
// illegal to import from both namespaces.
|
// illegal to import from both namespaces.
|
||||||
match (value_priv, check_value, type_priv, check_type) {
|
match (value_priv, check_value, type_priv, check_type) {
|
||||||
(Some(p), resolve::Unused, None, _) |
|
(Some(p), Unused, None, _) |
|
||||||
(None, _, Some(p), resolve::Unused) => {
|
(None, _, Some(p), Unused) => {
|
||||||
let p = match p {
|
let p = match p {
|
||||||
resolve::AllPublic => None,
|
AllPublic => None,
|
||||||
resolve::DependsOn(def) => ck_public(def),
|
DependsOn(def) => ck_public(def),
|
||||||
};
|
};
|
||||||
if p.is_some() {
|
if p.is_some() {
|
||||||
self.report_error(p);
|
self.report_error(p);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
(Some(v), resolve::Unused, Some(t), resolve::Unused) => {
|
(Some(v), Unused, Some(t), Unused) => {
|
||||||
let v = match v {
|
let v = match v {
|
||||||
resolve::AllPublic => None,
|
AllPublic => None,
|
||||||
resolve::DependsOn(def) => ck_public(def),
|
DependsOn(def) => ck_public(def),
|
||||||
};
|
};
|
||||||
let t = match t {
|
let t = match t {
|
||||||
resolve::AllPublic => None,
|
AllPublic => None,
|
||||||
resolve::DependsOn(def) => ck_public(def),
|
DependsOn(def) => ck_public(def),
|
||||||
};
|
};
|
||||||
if let (Some(_), Some(t)) = (v, t) {
|
if let (Some(_), Some(t)) = (v, t) {
|
||||||
self.report_error(Some(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,
|
pub fn check_crate(tcx: &ty::ctxt,
|
||||||
export_map: &def::ExportMap,
|
export_map: &def::ExportMap,
|
||||||
external_exports: resolve::ExternalExports,
|
external_exports: ExternalExports,
|
||||||
last_private_map: resolve::LastPrivateMap)
|
last_private_map: LastPrivateMap)
|
||||||
-> (ExportedItems, PublicItems) {
|
-> (ExportedItems, PublicItems) {
|
||||||
let krate = tcx.map.krate();
|
let krate = tcx.map.krate();
|
||||||
|
|
||||||
|
|
|
@ -8,9 +8,6 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
pub use self::PrivateDep::*;
|
|
||||||
pub use self::ImportUse::*;
|
|
||||||
pub use self::LastPrivate::*;
|
|
||||||
use self::PatternBindingMode::*;
|
use self::PatternBindingMode::*;
|
||||||
use self::Namespace::*;
|
use self::Namespace::*;
|
||||||
use self::NamespaceError::*;
|
use self::NamespaceError::*;
|
||||||
|
@ -40,6 +37,7 @@ use metadata::decoder::{DefLike, DlDef, DlField, DlImpl};
|
||||||
use middle::def::*;
|
use middle::def::*;
|
||||||
use middle::lang_items::LanguageItems;
|
use middle::lang_items::LanguageItems;
|
||||||
use middle::pat_util::pat_bindings;
|
use middle::pat_util::pat_bindings;
|
||||||
|
use middle::privacy::*;
|
||||||
use middle::subst::{ParamSpace, FnSpace, TypeSpace};
|
use middle::subst::{ParamSpace, FnSpace, TypeSpace};
|
||||||
use middle::ty::{CaptureModeMap, Freevar, FreevarMap, TraitMap};
|
use middle::ty::{CaptureModeMap, Freevar, FreevarMap, TraitMap};
|
||||||
use util::nodemap::{NodeMap, NodeSet, DefIdSet, FnvHashMap};
|
use util::nodemap::{NodeMap, NodeSet, DefIdSet, FnvHashMap};
|
||||||
|
@ -91,49 +89,6 @@ struct BindingInfo {
|
||||||
// Map from the name in a pattern to its binding mode.
|
// Map from the name in a pattern to its binding mode.
|
||||||
type BindingMap = HashMap<Name, BindingInfo>;
|
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)]
|
#[deriving(Copy, PartialEq)]
|
||||||
enum PatternBindingMode {
|
enum PatternBindingMode {
|
||||||
RefutableMode,
|
RefutableMode,
|
||||||
|
|
Loading…
Reference in a new issue