11
11
//! A pass that checks to make sure private fields and methods aren't used
12
12
//! outside their scopes. This pass will also generate a set of exported items
13
13
//! which are available for use externally when compiled as a library.
14
+ pub use self :: PrivateDep :: * ;
15
+ pub use self :: ImportUse :: * ;
16
+ pub use self :: LastPrivate :: * ;
14
17
use self :: PrivacyResult :: * ;
15
18
use self :: FieldName :: * ;
16
19
17
20
use std:: mem:: replace;
18
21
19
22
use metadata:: csearch;
20
- use middle:: { def, resolve } ;
23
+ use middle:: def;
21
24
use middle:: ty:: { mod, Ty } ;
22
25
use middle:: ty:: { MethodCall , MethodMap , MethodOrigin , MethodParam , MethodTypeParam } ;
23
26
use middle:: ty:: { MethodStatic , MethodStaticUnboxedClosure , MethodObject , MethodTraitObject } ;
24
- use util:: nodemap:: { NodeMap , NodeSet } ;
27
+ use util:: nodemap:: { DefIdSet , NodeMap , NodeSet } ;
25
28
26
29
use syntax:: { ast, ast_map} ;
27
30
use syntax:: ast_util:: { is_local, local_def, PostExpansionMethod } ;
@@ -34,11 +37,54 @@ type Context<'a, 'tcx> = (&'a MethodMap<'tcx>, &'a def::ExportMap);
34
37
/// A set of AST nodes exported by the crate.
35
38
pub type ExportedItems = NodeSet ;
36
39
40
+ /// A set containing all exported definitions from external crates.
41
+ /// The set does not contain any entries from local crates.
42
+ pub type ExternalExports = DefIdSet ;
43
+
37
44
/// A set of AST nodes that are fully public in the crate. This map is used for
38
45
/// documentation purposes (reexporting a private struct inlines the doc,
39
46
/// reexporting a public struct doesn't inline the doc).
40
47
pub type PublicItems = NodeSet ;
41
48
49
+ // FIXME: dox
50
+ pub type LastPrivateMap = NodeMap < LastPrivate > ;
51
+
52
+ #[ deriving( Copy , Show ) ]
53
+ pub enum LastPrivate {
54
+ LastMod ( PrivateDep ) ,
55
+ // `use` directives (imports) can refer to two separate definitions in the
56
+ // type and value namespaces. We record here the last private node for each
57
+ // and whether the import is in fact used for each.
58
+ // If the Option<PrivateDep> fields are None, it means there is no definition
59
+ // in that namespace.
60
+ LastImport { value_priv : Option < PrivateDep > ,
61
+ value_used : ImportUse ,
62
+ type_priv : Option < PrivateDep > ,
63
+ type_used : ImportUse } ,
64
+ }
65
+
66
+ #[ deriving( Copy , Show ) ]
67
+ pub enum PrivateDep {
68
+ AllPublic ,
69
+ DependsOn ( ast:: DefId ) ,
70
+ }
71
+
72
+ // How an import is used.
73
+ #[ deriving( Copy , PartialEq , Show ) ]
74
+ pub enum ImportUse {
75
+ Unused , // The import is not used.
76
+ Used , // The import is used.
77
+ }
78
+
79
+ impl LastPrivate {
80
+ pub fn or ( self , other : LastPrivate ) -> LastPrivate {
81
+ match ( self , other) {
82
+ ( me, LastMod ( AllPublic ) ) => me,
83
+ ( _, other) => other,
84
+ }
85
+ }
86
+ }
87
+
42
88
/// Result of a checking operation - None => no errors were found. Some => an
43
89
/// error and contains the span and message for reporting that error and
44
90
/// optionally the same for a note about the error.
@@ -362,8 +408,8 @@ struct PrivacyVisitor<'a, 'tcx: 'a> {
362
408
curitem : ast:: NodeId ,
363
409
in_foreign : bool ,
364
410
parents : NodeMap < ast:: NodeId > ,
365
- external_exports : resolve :: ExternalExports ,
366
- last_private_map : resolve :: LastPrivateMap ,
411
+ external_exports : ExternalExports ,
412
+ last_private_map : LastPrivateMap ,
367
413
}
368
414
369
415
enum PrivacyResult {
@@ -719,25 +765,25 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
719
765
} ;
720
766
721
767
match self . last_private_map [ path_id] {
722
- resolve :: LastMod ( resolve :: AllPublic ) => { } ,
723
- resolve :: LastMod ( resolve :: DependsOn ( def) ) => {
768
+ LastMod ( AllPublic ) => { } ,
769
+ LastMod ( DependsOn ( def) ) => {
724
770
self . report_error ( ck_public ( def) ) ;
725
771
} ,
726
- resolve :: LastImport { value_priv,
727
- value_used : check_value,
728
- type_priv,
729
- type_used : check_type} => {
772
+ LastImport { value_priv,
773
+ value_used : check_value,
774
+ type_priv,
775
+ type_used : check_type } => {
730
776
// This dance with found_error is because we don't want to report
731
777
// a privacy error twice for the same directive.
732
778
let found_error = match ( type_priv, check_type) {
733
- ( Some ( resolve :: DependsOn ( def) ) , resolve :: Used ) => {
779
+ ( Some ( DependsOn ( def) ) , Used ) => {
734
780
!self . report_error ( ck_public ( def) )
735
781
} ,
736
782
_ => false ,
737
783
} ;
738
784
if !found_error {
739
785
match ( value_priv, check_value) {
740
- ( Some ( resolve :: DependsOn ( def) ) , resolve :: Used ) => {
786
+ ( Some ( DependsOn ( def) ) , Used ) => {
741
787
self . report_error ( ck_public ( def) ) ;
742
788
} ,
743
789
_ => { } ,
@@ -749,24 +795,24 @@ impl<'a, 'tcx> PrivacyVisitor<'a, 'tcx> {
749
795
// be illegal. We only report one error, even if it is
750
796
// illegal to import from both namespaces.
751
797
match ( value_priv, check_value, type_priv, check_type) {
752
- ( Some ( p) , resolve :: Unused , None , _) |
753
- ( None , _, Some ( p) , resolve :: Unused ) => {
798
+ ( Some ( p) , Unused , None , _) |
799
+ ( None , _, Some ( p) , Unused ) => {
754
800
let p = match p {
755
- resolve :: AllPublic => None ,
756
- resolve :: DependsOn ( def) => ck_public ( def) ,
801
+ AllPublic => None ,
802
+ DependsOn ( def) => ck_public ( def) ,
757
803
} ;
758
804
if p. is_some ( ) {
759
805
self . report_error ( p) ;
760
806
}
761
807
} ,
762
- ( Some ( v) , resolve :: Unused , Some ( t) , resolve :: Unused ) => {
808
+ ( Some ( v) , Unused , Some ( t) , Unused ) => {
763
809
let v = match v {
764
- resolve :: AllPublic => None ,
765
- resolve :: DependsOn ( def) => ck_public ( def) ,
810
+ AllPublic => None ,
811
+ DependsOn ( def) => ck_public ( def) ,
766
812
} ;
767
813
let t = match t {
768
- resolve :: AllPublic => None ,
769
- resolve :: DependsOn ( def) => ck_public ( def) ,
814
+ AllPublic => None ,
815
+ DependsOn ( def) => ck_public ( def) ,
770
816
} ;
771
817
if let ( Some ( _) , Some ( t) ) = ( v, t) {
772
818
self . report_error ( Some ( t) ) ;
@@ -1521,8 +1567,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for VisiblePrivateTypesVisitor<'a, 'tcx> {
1521
1567
1522
1568
pub fn check_crate ( tcx : & ty:: ctxt ,
1523
1569
export_map : & def:: ExportMap ,
1524
- external_exports : resolve :: ExternalExports ,
1525
- last_private_map : resolve :: LastPrivateMap )
1570
+ external_exports : ExternalExports ,
1571
+ last_private_map : LastPrivateMap )
1526
1572
-> ( ExportedItems , PublicItems ) {
1527
1573
let krate = tcx. map . krate ( ) ;
1528
1574
0 commit comments