@@ -52,8 +52,8 @@ pub enum ImportKind<'a> {
52
52
} ,
53
53
Glob {
54
54
is_prelude : bool ,
55
- max_vis : Cell < ty:: Visibility > , // The visibility of the greatest re-export.
56
- // n.b. `max_vis` is only used in `finalize_import` to check for re-export errors.
55
+ max_vis : Cell < Option < ty:: Visibility > > , // The visibility of the greatest re-export.
56
+ // n.b. `max_vis` is only used in `finalize_import` to check for re-export errors.
57
57
} ,
58
58
ExternCrate {
59
59
source : Option < Symbol > ,
@@ -144,7 +144,7 @@ pub(crate) struct Import<'a> {
144
144
pub module_path : Vec < Segment > ,
145
145
/// The resolution of `module_path`.
146
146
pub imported_module : Cell < Option < ModuleOrUniformRoot < ' a > > > ,
147
- pub vis : Cell < ty:: Visibility > ,
147
+ pub vis : Cell < Option < ty:: Visibility > > ,
148
148
pub used : Cell < bool > ,
149
149
}
150
150
@@ -159,6 +159,10 @@ impl<'a> Import<'a> {
159
159
_ => false ,
160
160
}
161
161
}
162
+
163
+ pub ( crate ) fn expect_vis ( & self ) -> ty:: Visibility {
164
+ self . vis . get ( ) . expect ( "encountered cleared import visibility" )
165
+ }
162
166
}
163
167
164
168
/// Records information about the resolution of a name in a namespace of a module.
@@ -199,7 +203,7 @@ fn pub_use_of_private_extern_crate_hack(import: &Import<'_>, binding: &NameBindi
199
203
import : Import { kind : ImportKind :: ExternCrate { .. } , .. } ,
200
204
..
201
205
} ,
202
- ) => import. vis . get ( ) . is_public ( ) ,
206
+ ) => import. expect_vis ( ) . is_public ( ) ,
203
207
_ => false ,
204
208
}
205
209
}
@@ -212,17 +216,20 @@ impl<'a> Resolver<'a> {
212
216
binding : & ' a NameBinding < ' a > ,
213
217
import : & ' a Import < ' a > ,
214
218
) -> & ' a NameBinding < ' a > {
215
- let vis = if binding. vis . is_at_least ( import. vis . get ( ) , self )
219
+ let import_vis = import. expect_vis ( ) ;
220
+ let vis = if binding. vis . is_at_least ( import_vis, self )
216
221
|| pub_use_of_private_extern_crate_hack ( import, binding)
217
222
{
218
- import . vis . get ( )
223
+ import_vis
219
224
} else {
220
225
binding. vis
221
226
} ;
222
227
223
228
if let ImportKind :: Glob { ref max_vis, .. } = import. kind {
224
- if vis == import. vis . get ( ) || vis. is_at_least ( max_vis. get ( ) , self ) {
225
- max_vis. set ( vis)
229
+ if vis == import_vis
230
+ || max_vis. get ( ) . map_or ( true , |max_vis| vis. is_at_least ( max_vis, self ) )
231
+ {
232
+ max_vis. set ( Some ( vis) )
226
233
}
227
234
}
228
235
@@ -536,7 +543,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
536
543
} else {
537
544
// For better failure detection, pretend that the import will
538
545
// not define any names while resolving its module path.
539
- let orig_vis = import. vis . replace ( ty :: Visibility :: Invisible ) ;
546
+ let orig_vis = import. vis . take ( ) ;
540
547
let path_res =
541
548
self . r . maybe_resolve_path ( & import. module_path , None , & import. parent_scope ) ;
542
549
import. vis . set ( orig_vis) ;
@@ -571,7 +578,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
571
578
if let Err ( Undetermined ) = source_bindings[ ns] . get ( ) {
572
579
// For better failure detection, pretend that the import will
573
580
// not define any names while resolving its module path.
574
- let orig_vis = import. vis . replace ( ty :: Visibility :: Invisible ) ;
581
+ let orig_vis = import. vis . take ( ) ;
575
582
let binding = this. resolve_ident_in_module (
576
583
module,
577
584
source,
@@ -620,7 +627,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
620
627
/// Optionally returns an unresolved import error. This error is buffered and used to
621
628
/// consolidate multiple unresolved import errors into a single diagnostic.
622
629
fn finalize_import ( & mut self , import : & ' b Import < ' b > ) -> Option < UnresolvedImportError > {
623
- let orig_vis = import. vis . replace ( ty :: Visibility :: Invisible ) ;
630
+ let orig_vis = import. vis . take ( ) ;
624
631
let ignore_binding = match & import. kind {
625
632
ImportKind :: Single { target_bindings, .. } => target_bindings[ TypeNS ] . get ( ) ,
626
633
_ => None ,
@@ -727,9 +734,9 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
727
734
} ) ;
728
735
}
729
736
}
730
- if !is_prelude &&
731
- max_vis . get ( ) != ty :: Visibility :: Invisible && // Allow empty globs.
732
- !max_vis. get ( ) . is_at_least ( import. vis . get ( ) , & * self . r )
737
+ if !is_prelude
738
+ && let Some ( max_vis ) = max_vis . get ( )
739
+ && !max_vis. is_at_least ( import. expect_vis ( ) , & * self . r )
733
740
{
734
741
let msg = "glob import doesn't reexport anything because no candidate is public enough" ;
735
742
self . r . lint_buffer . buffer_lint ( UNUSED_IMPORTS , import. id , import. span , msg) ;
@@ -742,7 +749,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
742
749
let mut all_ns_err = true ;
743
750
self . r . per_ns ( |this, ns| {
744
751
if !type_ns_only || ns == TypeNS {
745
- let orig_vis = import. vis . replace ( ty :: Visibility :: Invisible ) ;
752
+ let orig_vis = import. vis . take ( ) ;
746
753
let binding = this. resolve_ident_in_module (
747
754
module,
748
755
ident,
@@ -906,8 +913,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
906
913
let mut crate_private_reexport = false ;
907
914
self . r . per_ns ( |this, ns| {
908
915
if let Ok ( binding) = source_bindings[ ns] . get ( ) {
909
- let vis = import. vis . get ( ) ;
910
- if !binding. vis . is_at_least ( vis, & * this) {
916
+ if !binding. vis . is_at_least ( import. expect_vis ( ) , & * this) {
911
917
reexport_error = Some ( ( ns, binding) ) ;
912
918
if let ty:: Visibility :: Restricted ( binding_def_id) = binding. vis {
913
919
if binding_def_id. is_top_level_module ( ) {
0 commit comments