@@ -46,9 +46,10 @@ use rustc_index::vec::IndexVec;
4646use rustc_metadata:: creader:: { CStore , CrateLoader } ;
4747use rustc_middle:: hir:: exports:: ExportMap ;
4848use rustc_middle:: middle:: cstore:: { CrateStore , MetadataLoaderDyn } ;
49+ use rustc_middle:: middle:: privacy:: AccessLevel ;
4950use rustc_middle:: span_bug;
5051use rustc_middle:: ty:: query:: Providers ;
51- use rustc_middle:: ty:: { self , DefIdTree , MainDefinition , ResolverOutputs } ;
52+ use rustc_middle:: ty:: { self , DefIdTree , MainDefinition , ResolverOutputs , Visibility } ;
5253use rustc_session:: lint;
5354use rustc_session:: lint:: { BuiltinLintDiagnostics , LintBuffer } ;
5455use rustc_session:: Session ;
@@ -1032,6 +1033,8 @@ pub struct Resolver<'a> {
10321033 legacy_const_generic_args : FxHashMap < DefId , Option < Vec < usize > > > ,
10331034
10341035 main_def : Option < MainDefinition > ,
1036+
1037+ node_privacy : FxHashMap < DefId , AccessLevel >
10351038}
10361039
10371040/// Nothing really interesting here; it just provides memory for the rest of the crate.
@@ -1393,6 +1396,8 @@ impl<'a> Resolver<'a> {
13931396 trait_impl_items : Default :: default ( ) ,
13941397 legacy_const_generic_args : Default :: default ( ) ,
13951398 main_def : Default :: default ( ) ,
1399+
1400+ node_privacy : Default :: default ( ) ,
13961401 } ;
13971402
13981403 let root_parent_scope = ParentScope :: module ( graph_root, & resolver) ;
@@ -1435,10 +1440,12 @@ impl<'a> Resolver<'a> {
14351440 let maybe_unused_extern_crates = self . maybe_unused_extern_crates ;
14361441 let glob_map = self . glob_map ;
14371442 let main_def = self . main_def ;
1443+ let access_levels = self . node_privacy ;
14381444 ResolverOutputs {
14391445 definitions,
14401446 cstore : Box :: new ( self . crate_loader . into_cstore ( ) ) ,
14411447 visibilities,
1448+ access_levels,
14421449 extern_crate_map,
14431450 export_map,
14441451 glob_map,
@@ -1456,6 +1463,7 @@ impl<'a> Resolver<'a> {
14561463 pub fn clone_outputs ( & self ) -> ResolverOutputs {
14571464 ResolverOutputs {
14581465 definitions : self . definitions . clone ( ) ,
1466+ access_levels : self . node_privacy . clone ( ) ,
14591467 cstore : Box :: new ( self . cstore ( ) . clone ( ) ) ,
14601468 visibilities : self . visibilities . clone ( ) ,
14611469 extern_crate_map : self . extern_crate_map . clone ( ) ,
@@ -1512,6 +1520,7 @@ impl<'a> Resolver<'a> {
15121520 pub fn resolve_crate ( & mut self , krate : & Crate ) {
15131521 self . session . time ( "resolve_crate" , || {
15141522 self . session . time ( "finalize_imports" , || ImportResolver { r : self } . finalize_imports ( ) ) ;
1523+ self . session . time ( "prepare_privacy" , || self . prepare_privacy ( ) ) ;
15151524 self . session . time ( "finalize_macro_resolutions" , || self . finalize_macro_resolutions ( ) ) ;
15161525 self . session . time ( "late_resolve_crate" , || self . late_resolve_crate ( krate) ) ;
15171526 self . session . time ( "resolve_main" , || self . resolve_main ( ) ) ;
@@ -1521,6 +1530,45 @@ impl<'a> Resolver<'a> {
15211530 } ) ;
15221531 }
15231532
1533+ fn prepare_privacy ( & mut self ) {
1534+ let root = self . graph_root ( ) ;
1535+
1536+ if let Some ( def_id) = root. def_id ( ) {
1537+ if let Some ( exports) = self . export_map . get ( & def_id. expect_local ( ) ) . cloned ( ) {
1538+ tracing:: trace!( "exports={:?}" , exports) ;
1539+ let public_exports = exports. iter ( ) . filter ( |ex| ex. vis == Visibility :: Public ) . collect :: < Vec < _ > > ( ) ;
1540+ for export in public_exports. into_iter ( ) {
1541+ self . per_ns ( |this, ns| {
1542+ let new_key = this. new_key ( export. ident , ns) ;
1543+ let name_res = this. resolution ( root, new_key) ;
1544+ if let Some ( binding) = name_res. borrow ( ) . binding ( ) {
1545+ this. recursive_define_access_level ( binding, AccessLevel :: Public , 30 ) ;
1546+ }
1547+ } ) ;
1548+ }
1549+ }
1550+ }
1551+
1552+ tracing:: info!( "node_privacy: {:#?}" , self . node_privacy) ;
1553+ }
1554+
1555+ fn recursive_define_access_level ( & mut self , binding : & NameBinding < ' a > , access_level : AccessLevel , max_recurse : usize ) {
1556+ // Is this useful in case of very very veryyyyyy deep nesting?
1557+ if max_recurse == 0 {
1558+ return ;
1559+ }
1560+
1561+ if let NameBindingKind :: Import { binding, import, .. } = binding. kind {
1562+ let def_id = self . opt_local_def_id ( import. id ) . map ( |local_def_id| local_def_id. to_def_id ( ) ) ;
1563+ if let Some ( def_id) = def_id {
1564+ tracing:: trace!( "binding found! import.id={:?} def_id={:?}" , import. id, def_id) ;
1565+ self . node_privacy . insert ( def_id, access_level) ;
1566+ }
1567+
1568+ self . recursive_define_access_level ( binding, AccessLevel :: Exported , max_recurse - 1 ) ;
1569+ }
1570+ }
1571+
15241572 pub fn traits_in_scope (
15251573 & mut self ,
15261574 current_trait : Option < Module < ' a > > ,
0 commit comments