@@ -7,7 +7,8 @@ use rustc_ast::EnumDef;
7
7
use rustc_data_structures:: intern:: Interned ;
8
8
use rustc_hir:: def_id:: LocalDefId ;
9
9
use rustc_hir:: def_id:: CRATE_DEF_ID ;
10
- use rustc_middle:: middle:: privacy:: { EffectiveVisibilities , EffectiveVisibility , Level } ;
10
+ use rustc_middle:: middle:: privacy:: { EffectiveVisibilities , EffectiveVisibility } ;
11
+ use rustc_middle:: middle:: privacy:: { IntoDefIdTree , Level } ;
11
12
use rustc_middle:: ty:: Visibility ;
12
13
13
14
type ImportId < ' a > = Interned < ' a , NameBinding < ' a > > ;
@@ -29,20 +30,35 @@ impl ParentId<'_> {
29
30
30
31
pub struct EffectiveVisibilitiesVisitor < ' r , ' a > {
31
32
r : & ' r mut Resolver < ' a > ,
33
+ def_effective_visibilities : EffectiveVisibilities ,
32
34
/// While walking import chains we need to track effective visibilities per-binding, and def id
33
35
/// keys in `Resolver::effective_visibilities` are not enough for that, because multiple
34
36
/// bindings can correspond to a single def id in imports. So we keep a separate table.
35
37
import_effective_visibilities : EffectiveVisibilities < ImportId < ' a > > ,
36
38
changed : bool ,
37
39
}
38
40
41
+ impl Resolver < ' _ > {
42
+ fn nearest_normal_mod ( & mut self , def_id : LocalDefId ) -> LocalDefId {
43
+ self . get_nearest_non_block_module ( def_id. to_def_id ( ) ) . nearest_parent_mod ( ) . expect_local ( )
44
+ }
45
+ }
46
+
47
+ impl < ' a , ' b > IntoDefIdTree for & ' b mut Resolver < ' a > {
48
+ type Tree = & ' b Resolver < ' a > ;
49
+ fn tree ( self ) -> Self :: Tree {
50
+ self
51
+ }
52
+ }
53
+
39
54
impl < ' r , ' a > EffectiveVisibilitiesVisitor < ' r , ' a > {
40
55
/// Fills the `Resolver::effective_visibilities` table with public & exported items
41
56
/// For now, this doesn't resolve macros (FIXME) and cannot resolve Impl, as we
42
57
/// need access to a TyCtxt for that.
43
58
pub fn compute_effective_visibilities < ' c > ( r : & ' r mut Resolver < ' a > , krate : & ' c Crate ) {
44
59
let mut visitor = EffectiveVisibilitiesVisitor {
45
60
r,
61
+ def_effective_visibilities : Default :: default ( ) ,
46
62
import_effective_visibilities : Default :: default ( ) ,
47
63
changed : false ,
48
64
} ;
@@ -54,6 +70,7 @@ impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> {
54
70
visitor. changed = false ;
55
71
visit:: walk_crate ( & mut visitor, krate) ;
56
72
}
73
+ visitor. r . effective_visibilities = visitor. def_effective_visibilities ;
57
74
58
75
// Update visibilities for import def ids. These are not used during the
59
76
// `EffectiveVisibilitiesVisitor` pass, because we have more detailed binding-based
@@ -90,10 +107,6 @@ impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> {
90
107
info ! ( "resolve::effective_visibilities: {:#?}" , r. effective_visibilities) ;
91
108
}
92
109
93
- fn nearest_normal_mod ( & mut self , def_id : LocalDefId ) -> LocalDefId {
94
- self . r . get_nearest_non_block_module ( def_id. to_def_id ( ) ) . nearest_parent_mod ( ) . expect_local ( )
95
- }
96
-
97
110
/// Update effective visibilities of bindings in the given module,
98
111
/// including their whole reexport chains.
99
112
fn set_bindings_effective_visibilities ( & mut self , module_id : LocalDefId ) {
@@ -124,7 +137,7 @@ impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> {
124
137
125
138
fn effective_vis ( & self , parent_id : ParentId < ' a > ) -> Option < EffectiveVisibility > {
126
139
match parent_id {
127
- ParentId :: Def ( def_id) => self . r . effective_visibilities . effective_vis ( def_id) ,
140
+ ParentId :: Def ( def_id) => self . def_effective_visibilities . effective_vis ( def_id) ,
128
141
ParentId :: Import ( binding) => self . import_effective_visibilities . effective_vis ( binding) ,
129
142
}
130
143
. copied ( )
@@ -150,7 +163,7 @@ impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> {
150
163
let default_vis = Visibility :: Restricted (
151
164
import
152
165
. id ( )
153
- . map ( |id| self . nearest_normal_mod ( self . r . local_def_id ( id) ) )
166
+ . map ( |id| self . r . nearest_normal_mod ( self . r . local_def_id ( id) ) )
154
167
. unwrap_or ( CRATE_DEF_ID ) ,
155
168
) ;
156
169
if self . is_noop_update ( parent_id, nominal_vis, default_vis) {
@@ -159,25 +172,25 @@ impl<'r, 'a> EffectiveVisibilitiesVisitor<'r, 'a> {
159
172
self . changed |= self . import_effective_visibilities . update (
160
173
binding,
161
174
nominal_vis,
162
- default_vis,
175
+ |r| ( default_vis, r ) ,
163
176
self . effective_vis ( parent_id) ,
164
177
parent_id. level ( ) ,
165
- ResolverTree ( & self . r . definitions , & self . r . crate_loader ) ,
178
+ & mut * self . r ,
166
179
) ;
167
180
}
168
181
169
182
fn update_def ( & mut self , def_id : LocalDefId , nominal_vis : Visibility , parent_id : ParentId < ' a > ) {
170
- let default_vis = Visibility :: Restricted ( self . nearest_normal_mod ( def_id) ) ;
183
+ let default_vis = Visibility :: Restricted ( self . r . nearest_normal_mod ( def_id) ) ;
171
184
if self . is_noop_update ( parent_id, nominal_vis, default_vis) {
172
185
return ;
173
186
}
174
- self . changed |= self . r . effective_visibilities . update (
187
+ self . changed |= self . def_effective_visibilities . update (
175
188
def_id,
176
189
nominal_vis,
177
- if def_id == CRATE_DEF_ID { Visibility :: Public } else { default_vis } ,
190
+ |r| ( if def_id == CRATE_DEF_ID { Visibility :: Public } else { default_vis } , r ) ,
178
191
self . effective_vis ( parent_id) ,
179
192
parent_id. level ( ) ,
180
- ResolverTree ( & self . r . definitions , & self . r . crate_loader ) ,
193
+ & mut * self . r ,
181
194
) ;
182
195
}
183
196
0 commit comments