@@ -25,7 +25,7 @@ use rustc_span::{Ident, Span, Symbol, kw, sym};
2525use smallvec:: SmallVec ;
2626use tracing:: debug;
2727
28- use crate :: Namespace :: * ;
28+ use crate :: Namespace :: { self , * } ;
2929use crate :: diagnostics:: { DiagMode , Suggestion , import_candidates} ;
3030use crate :: errors:: {
3131 CannotBeReexportedCratePublic , CannotBeReexportedCratePublicNS , CannotBeReexportedPrivate ,
@@ -338,13 +338,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
338338 pub ( crate ) fn try_define (
339339 & mut self ,
340340 module : Module < ' ra > ,
341- key : BindingKey ,
341+ ident : Ident ,
342+ ns : Namespace ,
342343 binding : NameBinding < ' ra > ,
343344 warn_ambiguity : bool ,
344345 ) -> Result < ( ) , NameBinding < ' ra > > {
345346 let res = binding. res ( ) ;
346- self . check_reserved_macro_name ( key . ident , res) ;
347+ self . check_reserved_macro_name ( ident, res) ;
347348 self . set_binding_parent_module ( binding, module) ;
349+ // Even if underscore names cannot be looked up, we still need to add them to modules,
350+ // because they can be fetched by glob imports from those modules, and bring traits
351+ // into scope both directly and through glob imports.
352+ let key = BindingKey :: new_disambiguated ( ident, ns, || {
353+ module. underscore_disambiguator . update ( |d| d + 1 ) ;
354+ module. underscore_disambiguator . get ( )
355+ } ) ;
348356 self . update_resolution ( module, key, warn_ambiguity, |this, resolution| {
349357 if let Some ( old_binding) = resolution. best_binding ( ) {
350358 if res == Res :: Err && old_binding. res ( ) != Res :: Err {
@@ -383,7 +391,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
383391 ( old_glob @ true , false ) | ( old_glob @ false , true ) => {
384392 let ( glob_binding, non_glob_binding) =
385393 if old_glob { ( old_binding, binding) } else { ( binding, old_binding) } ;
386- if key . ns == MacroNS
394+ if ns == MacroNS
387395 && non_glob_binding. expansion != LocalExpnId :: ROOT
388396 && glob_binding. res ( ) != non_glob_binding. res ( )
389397 {
@@ -489,10 +497,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
489497 } ;
490498 if self . is_accessible_from ( binding. vis , scope) {
491499 let imported_binding = self . import ( binding, * import) ;
492- let key = BindingKey { ident, ..key } ;
493500 let _ = self . try_define (
494501 import. parent_scope . module ,
495- key,
502+ ident,
503+ key. ns ,
496504 imported_binding,
497505 warn_ambiguity,
498506 ) ;
@@ -514,11 +522,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
514522 let dummy_binding = self . dummy_binding ;
515523 let dummy_binding = self . import ( dummy_binding, import) ;
516524 self . per_ns ( |this, ns| {
517- let key = BindingKey :: new ( target, ns) ;
518- let _ = this. try_define ( import. parent_scope . module , key, dummy_binding, false ) ;
519- this. update_resolution ( import. parent_scope . module , key, false , |_, resolution| {
520- resolution. single_imports . swap_remove ( & import) ;
521- } )
525+ let module = import. parent_scope . module ;
526+ let _ = this. try_define ( module, target, ns, dummy_binding, false ) ;
527+ // Don't remove underscores from `single_imports`, they were never added.
528+ if target. name != kw:: Underscore {
529+ let key = BindingKey :: new ( target, ns) ;
530+ this. update_resolution ( module, key, false , |_, resolution| {
531+ resolution. single_imports . swap_remove ( & import) ;
532+ } )
533+ }
522534 } ) ;
523535 self . record_use ( target, dummy_binding, Used :: Other ) ;
524536 } else if import. imported_module . get ( ) . is_none ( ) {
@@ -895,7 +907,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
895907 PendingBinding :: Ready ( Some ( imported_binding) )
896908 }
897909 Err ( Determinacy :: Determined ) => {
898- // Don't update the resolution for underscores, because it was never added.
910+ // Don't remove underscores from `single_imports`, they were never added.
899911 if target. name != kw:: Underscore {
900912 let key = BindingKey :: new ( target, ns) ;
901913 this. update_resolution ( parent, key, false , |_, resolution| {
@@ -1510,7 +1522,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
15101522 . is_some_and ( |binding| binding. warn_ambiguity_recursive ( ) ) ;
15111523 let _ = self . try_define (
15121524 import. parent_scope . module ,
1513- key,
1525+ key. ident ,
1526+ key. ns ,
15141527 imported_binding,
15151528 warn_ambiguity,
15161529 ) ;
0 commit comments