@@ -16,7 +16,7 @@ use rustc_hir::Attribute;
1616use rustc_hir:: attrs:: AttributeKind ;
1717use rustc_hir:: attrs:: diagnostic:: { CustomDiagnostic , Directive , FormatArgs } ;
1818use rustc_hir:: def:: { self , DefKind , PartialRes } ;
19- use rustc_hir:: def_id:: { DefId , LocalDefIdMap } ;
19+ use rustc_hir:: def_id:: { DefId , LocalDefId , LocalDefIdMap } ;
2020use rustc_middle:: metadata:: { AmbigModChild , ModChild , Reexport } ;
2121use rustc_middle:: span_bug;
2222use rustc_middle:: ty:: { TyCtxt , Visibility } ;
@@ -90,17 +90,20 @@ pub(crate) enum ImportKind<'ra> {
9090 /// If this is the import for `foo::bar::a`, we would have the ID of the `UseTree`
9191 /// for `a` in this field.
9292 id : NodeId ,
93+ def_id : LocalDefId ,
9394 } ,
9495 Glob {
9596 // The visibility of the greatest re-export.
9697 // n.b. `max_vis` is only used in `finalize_import` to check for re-export errors.
9798 max_vis : CmCell < Option < Visibility > > ,
9899 id : NodeId ,
100+ def_id : LocalDefId ,
99101 } ,
100102 ExternCrate {
101103 source : Option < Symbol > ,
102104 target : Ident ,
103105 id : NodeId ,
106+ def_id : LocalDefId ,
104107 } ,
105108 MacroUse {
106109 /// A field has been added indicating whether it should be reported as a lint,
@@ -116,7 +119,7 @@ impl<'ra> std::fmt::Debug for ImportKind<'ra> {
116119 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
117120 use ImportKind :: * ;
118121 match self {
119- Single { source, target, decls, nested, id, .. } => f
122+ Single { source, target, decls, nested, id, def_id } => f
120123 . debug_struct ( "Single" )
121124 . field ( "source" , source)
122125 . field ( "target" , target)
@@ -127,15 +130,20 @@ impl<'ra> std::fmt::Debug for ImportKind<'ra> {
127130 )
128131 . field ( "nested" , nested)
129132 . field ( "id" , id)
133+ . field ( "def_id" , def_id)
130134 . finish ( ) ,
131- Glob { max_vis, id } => {
132- f. debug_struct ( "Glob" ) . field ( "max_vis" , max_vis) . field ( "id" , id) . finish ( )
133- }
134- ExternCrate { source, target, id } => f
135+ Glob { max_vis, id, def_id } => f
136+ . debug_struct ( "Glob" )
137+ . field ( "max_vis" , max_vis)
138+ . field ( "id" , id)
139+ . field ( "def_id" , def_id)
140+ . finish ( ) ,
141+ ExternCrate { source, target, id, def_id } => f
135142 . debug_struct ( "ExternCrate" )
136143 . field ( "source" , source)
137144 . field ( "target" , target)
138145 . field ( "id" , id)
146+ . field ( "def_id" , def_id)
139147 . finish ( ) ,
140148 MacroUse { warn_private } => {
141149 f. debug_struct ( "MacroUse" ) . field ( "warn_private" , warn_private) . finish ( )
@@ -260,12 +268,20 @@ impl<'ra> ImportData<'ra> {
260268 }
261269 }
262270
263- pub ( crate ) fn simplify ( & self , r : & Resolver < ' _ , ' _ > ) -> Reexport {
264- let to_def_id = |id| r. local_def_id ( id) . to_def_id ( ) ;
271+ pub ( crate ) fn def_id ( & self ) -> Option < LocalDefId > {
272+ match self . kind {
273+ ImportKind :: Single { def_id, .. }
274+ | ImportKind :: Glob { def_id, .. }
275+ | ImportKind :: ExternCrate { def_id, .. } => Some ( def_id) ,
276+ ImportKind :: MacroUse { .. } | ImportKind :: MacroExport => None ,
277+ }
278+ }
279+
280+ pub ( crate ) fn simplify ( & self ) -> Reexport {
265281 match self . kind {
266- ImportKind :: Single { id , .. } => Reexport :: Single ( to_def_id ( id ) ) ,
267- ImportKind :: Glob { id , .. } => Reexport :: Glob ( to_def_id ( id ) ) ,
268- ImportKind :: ExternCrate { id , .. } => Reexport :: ExternCrate ( to_def_id ( id ) ) ,
282+ ImportKind :: Single { def_id , .. } => Reexport :: Single ( def_id . to_def_id ( ) ) ,
283+ ImportKind :: Glob { def_id , .. } => Reexport :: Glob ( def_id . to_def_id ( ) ) ,
284+ ImportKind :: ExternCrate { def_id , .. } => Reexport :: ExternCrate ( def_id . to_def_id ( ) ) ,
269285 ImportKind :: MacroUse { .. } => Reexport :: MacroUse ,
270286 ImportKind :: MacroExport => Reexport :: MacroExport ,
271287 }
@@ -340,13 +356,16 @@ struct UnresolvedImportError {
340356
341357// Reexports of the form `pub use foo as bar;` where `foo` is `extern crate foo;`
342358// are permitted for backward-compatibility under a deprecation lint.
343- fn pub_use_of_private_extern_crate_hack ( import : ImportSummary , decl : Decl < ' _ > ) -> Option < NodeId > {
359+ fn pub_use_of_private_extern_crate_hack (
360+ import : ImportSummary ,
361+ decl : Decl < ' _ > ,
362+ ) -> Option < LocalDefId > {
344363 match ( import. is_single , decl. kind ) {
345364 ( true , DeclKind :: Import { import : decl_import, .. } )
346- if let ImportKind :: ExternCrate { id , .. } = decl_import. kind
365+ if let ImportKind :: ExternCrate { def_id , .. } = decl_import. kind
347366 && import. vis . is_public ( ) =>
348367 {
349- Some ( id )
368+ Some ( def_id )
350369 }
351370 _ => None ,
352371 }
@@ -845,8 +864,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
845864 if binding. res ( ) != Res :: Err
846865 && glob_decl. res ( ) != Res :: Err
847866 && let DeclKind :: Import { import : glob_import, .. } = glob_decl. kind
848- && let Some ( glob_import_id) = glob_import. id ( )
849- && let glob_import_def_id = self . local_def_id ( glob_import_id)
867+ && let Some ( glob_import_def_id) = glob_import. def_id ( )
850868 && self . effective_visibilities . is_exported ( glob_import_def_id)
851869 && glob_decl. vis ( ) . is_public ( )
852870 && !binding. vis ( ) . is_public ( )
@@ -875,7 +893,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
875893
876894 if let DeclKind :: Import { import, .. } = binding. kind
877895 && let Some ( binding_id) = import. id ( )
878- && let import_def_id = self . local_def_id ( binding_id )
896+ && let import_def_id = import . def_id ( ) . unwrap ( )
879897 && self . effective_visibilities . is_exported ( import_def_id)
880898 && let Res :: Def ( reexported_kind, reexported_def_id) = binding. res ( )
881899 && !matches ! ( reexported_kind, DefKind :: Ctor ( ..) )
@@ -1267,7 +1285,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12671285
12681286 let ( ident, target, bindings, import_id) = match import. kind {
12691287 ImportKind :: Single { source, target, ref decls, id, .. } => ( source, target, decls, id) ,
1270- ImportKind :: Glob { ref max_vis, id } => {
1288+ ImportKind :: Glob { ref max_vis, id, def_id } => {
12711289 if import. module_path . len ( ) <= 1 {
12721290 // HACK(eddyb) `lint_if_path_starts_with_module` needs at least
12731291 // 2 segments, so the `resolve_path` above won't trigger it.
@@ -1294,7 +1312,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12941312 if let Some ( max_vis) = max_vis. get ( )
12951313 && import. vis . greater_than ( max_vis, self . tcx )
12961314 {
1297- let def_id = self . local_def_id ( id) ;
12981315 self . lint_buffer . buffer_lint (
12991316 UNUSED_IMPORTS ,
13001317 id,
@@ -1614,7 +1631,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16141631 if let Some ( extern_crate_id) = pub_use_of_private_extern_crate_hack ( import. summary ( ) , decl)
16151632 {
16161633 let ImportKind :: Single { id, .. } = import. kind else { unreachable ! ( ) } ;
1617- let sugg = self . tcx . source_span ( self . local_def_id ( extern_crate_id) ) . shrink_to_lo ( ) ;
1634+ let sugg = self . tcx . source_span ( extern_crate_id) . shrink_to_lo ( ) ;
16181635 let diagnostic = crate :: errors:: PrivateExternCrateReexport { ident, sugg } ;
16191636 return Some ( BufferedEarlyLint {
16201637 lint_id : LintId :: of ( PUB_USE_OF_PRIVATE_EXTERN_CRATE ) ,
@@ -1656,7 +1673,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16561673
16571674 pub ( crate ) fn check_for_redundant_imports ( & mut self , import : Import < ' ra > ) -> bool {
16581675 // This function is only called for single imports.
1659- let ImportKind :: Single { source, target, ref decls, id, .. } = import. kind else {
1676+ let ImportKind :: Single { source, target, ref decls, id, def_id , .. } = import. kind else {
16601677 unreachable ! ( )
16611678 } ;
16621679
@@ -1675,7 +1692,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
16751692 // Skip if the import is public or was used through non scope-based resolution,
16761693 // e.g. through a module-relative path.
16771694 if self . import_use_map . get ( & import) == Some ( & Used :: Other )
1678- || self . effective_visibilities . is_exported ( self . local_def_id ( id ) )
1695+ || self . effective_visibilities . is_exported ( def_id )
16791696 {
16801697 return false ;
16811698 }
@@ -1829,23 +1846,23 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
18291846 let mut children = Vec :: new ( ) ;
18301847 let mut ambig_children = Vec :: new ( ) ;
18311848
1832- module. to_module ( ) . for_each_child ( self , |this , ident, orig_ident_span, _, binding| {
1849+ module. to_module ( ) . for_each_child ( self , |_this , ident, orig_ident_span, _, binding| {
18331850 let res = binding. res ( ) . expect_non_local ( ) ;
18341851 if res != def:: Res :: Err {
18351852 let ident = ident. orig ( orig_ident_span) ;
18361853 let child =
18371854 |reexport_chain| ModChild { ident, res, vis : binding. vis ( ) , reexport_chain } ;
18381855 if let Some ( ( ambig_binding1, ambig_binding2) ) = binding. descent_to_ambiguity ( ) {
1839- let main = child ( ambig_binding1. reexport_chain ( this ) ) ;
1856+ let main = child ( ambig_binding1. reexport_chain ( ) ) ;
18401857 let second = ModChild {
18411858 ident,
18421859 res : ambig_binding2. res ( ) . expect_non_local ( ) ,
18431860 vis : ambig_binding2. vis ( ) ,
1844- reexport_chain : ambig_binding2. reexport_chain ( this ) ,
1861+ reexport_chain : ambig_binding2. reexport_chain ( ) ,
18451862 } ;
18461863 ambig_children. push ( AmbigModChild { main, second } )
18471864 } else {
1848- children. push ( child ( binding. reexport_chain ( this ) ) ) ;
1865+ children. push ( child ( binding. reexport_chain ( ) ) ) ;
18491866 }
18501867 }
18511868 } ) ;
0 commit comments