@@ -11,7 +11,7 @@ use rustc_ast::NodeId;
11
11
use rustc_hir:: def_id:: LocalDefId ;
12
12
use rustc_hir:: def_id:: CRATE_DEF_ID ;
13
13
use rustc_middle:: middle:: privacy:: AccessLevel ;
14
- use rustc_middle:: ty:: DefIdTree ;
14
+ use rustc_middle:: ty:: { DefIdTree , Visibility } ;
15
15
use rustc_span:: sym;
16
16
17
17
pub struct AccessLevelsVisitor < ' r , ' a > {
@@ -26,6 +26,11 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
26
26
pub fn compute_access_levels < ' c > ( r : & ' r mut Resolver < ' a > , krate : & ' c Crate ) {
27
27
let mut visitor = AccessLevelsVisitor { r, changed : false } ;
28
28
29
+ /*
30
+ let crate_effective_vis = EffectiveVisibility::default();
31
+ crate_effective_vis.update(Visibility::Public, AccessLevel::Public, visitor.r);
32
+ visitor.r.access_levels.set_effective_vis(CRATE_DEF_ID, crate_effective_vis);
33
+ */
29
34
visitor. set_access_level_def_id ( CRATE_DEF_ID , Some ( AccessLevel :: Public ) ) ;
30
35
visitor. set_bindings_access_level ( CRATE_DEF_ID ) ;
31
36
@@ -47,9 +52,6 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
47
52
fn set_bindings_access_level ( & mut self , module_id : LocalDefId ) {
48
53
assert ! ( self . r. module_map. contains_key( &&module_id. to_def_id( ) ) ) ;
49
54
let module_level = self . r . access_levels . get_access_level ( module_id) ;
50
- if !module_level. is_some ( ) {
51
- return ;
52
- }
53
55
// Set the given binding access level to `AccessLevel::Public` and
54
56
// sets the rest of the `use` chain to `AccessLevel::Exported` until
55
57
// we hit the actual exported item.
@@ -72,8 +74,8 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
72
74
let module = self . r . get_module ( module_id. to_def_id ( ) ) . unwrap ( ) ;
73
75
let resolutions = self . r . resolutions ( module) ;
74
76
75
- for ( .. , name_resolution) in resolutions. borrow ( ) . iter ( ) {
76
- if let Some ( binding) = name_resolution. borrow ( ) . binding ( ) && binding. vis . is_public ( ) && !binding. is_ambiguity ( ) {
77
+ for ( key , name_resolution) in resolutions. borrow ( ) . iter ( ) {
78
+ if let Some ( binding) = name_resolution. borrow ( ) . binding ( ) && binding. vis . is_public ( ) && !binding. is_ambiguity ( ) && module_level . is_some ( ) {
77
79
let access_level = match binding. is_import ( ) {
78
80
true => {
79
81
set_import_binding_access_level ( self , binding, module_level) ;
@@ -85,6 +87,51 @@ impl<'r, 'a> AccessLevelsVisitor<'r, 'a> {
85
87
self . set_access_level_def_id ( def_id, access_level) ;
86
88
}
87
89
}
90
+
91
+ if let Some ( binding) = name_resolution. borrow ( ) . binding ( ) {
92
+ println ! ( "ident: {}" , key. ident. as_str( ) ) ;
93
+ if let Some ( def_id) = binding. res ( ) . opt_def_id ( ) . and_then ( |id| id. as_local ( ) ) {
94
+ let tag = match binding. is_import ( ) {
95
+ true => AccessLevel :: Exported ,
96
+ false => AccessLevel :: Public ,
97
+ } ;
98
+ let vis = match binding. vis {
99
+ Visibility :: Public => Visibility :: Public ,
100
+ Visibility :: Restricted ( id) => Visibility :: Restricted ( id. expect_local ( ) )
101
+ } ;
102
+ self . update_effective_vis ( def_id, vis, module_id, tag) ;
103
+ } ;
104
+ }
105
+ }
106
+ }
107
+
108
+ // fn init_crate_effective_vis(&mut self) {
109
+
110
+ // }
111
+
112
+ fn update_effective_vis (
113
+ & mut self ,
114
+ current_id : LocalDefId ,
115
+ current_vis : Visibility ,
116
+ module_id : LocalDefId ,
117
+ tag : AccessLevel ,
118
+ ) {
119
+ if let Some ( inherited_effective_vis) = self . r . access_levels . get_effective_vis ( module_id) {
120
+ println ! ( "tag: {:?}" , tag) ;
121
+ println ! ( "inherited effective vis: {:?}" , inherited_effective_vis) ;
122
+ let mut current_effective_vis = self . r . access_levels . get_effective_vis ( current_id) . copied ( ) . unwrap_or_default ( ) ;
123
+ let nearest_available_vis = inherited_effective_vis. nearest_available ( tag) . unwrap ( ) ;
124
+ println ! ( "nearest available vis: {:?}" , nearest_available_vis) ;
125
+ let calculated_effective_vis = match current_vis {
126
+ Visibility :: Public => nearest_available_vis,
127
+ Visibility :: Restricted ( _) => {
128
+ if current_vis. is_at_least ( nearest_available_vis, & * self . r ) { nearest_available_vis} else { current_vis}
129
+ }
130
+ } ;
131
+ println ! ( "calculated effective vis: {:?}" , calculated_effective_vis) ;
132
+ current_effective_vis. update ( calculated_effective_vis, tag, & * self . r ) ;
133
+ println ! ( "updated effective vis: {:?}" , current_effective_vis) ;
134
+ self . r . access_levels . set_effective_vis ( current_id, current_effective_vis) ;
88
135
}
89
136
}
90
137
0 commit comments