@@ -25,7 +25,7 @@ use rustc_span::{Ident, Span, Symbol, kw, sym};
25
25
use smallvec:: SmallVec ;
26
26
use tracing:: debug;
27
27
28
- use crate :: Namespace :: * ;
28
+ use crate :: Namespace :: { self , * } ;
29
29
use crate :: diagnostics:: { DiagMode , Suggestion , import_candidates} ;
30
30
use crate :: errors:: {
31
31
CannotBeReexportedCratePublic , CannotBeReexportedCratePublicNS , CannotBeReexportedPrivate ,
@@ -338,13 +338,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
338
338
pub ( crate ) fn try_define (
339
339
& mut self ,
340
340
module : Module < ' ra > ,
341
- key : BindingKey ,
341
+ ident : Ident ,
342
+ ns : Namespace ,
342
343
binding : NameBinding < ' ra > ,
343
344
warn_ambiguity : bool ,
344
345
) -> Result < ( ) , NameBinding < ' ra > > {
345
346
let res = binding. res ( ) ;
346
- self . check_reserved_macro_name ( key . ident , res) ;
347
+ self . check_reserved_macro_name ( ident, res) ;
347
348
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
+ } ) ;
348
356
self . update_resolution ( module, key, warn_ambiguity, |this, resolution| {
349
357
if let Some ( old_binding) = resolution. best_binding ( ) {
350
358
if res == Res :: Err && old_binding. res ( ) != Res :: Err {
@@ -383,7 +391,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
383
391
( old_glob @ true , false ) | ( old_glob @ false , true ) => {
384
392
let ( glob_binding, non_glob_binding) =
385
393
if old_glob { ( old_binding, binding) } else { ( binding, old_binding) } ;
386
- if key . ns == MacroNS
394
+ if ns == MacroNS
387
395
&& non_glob_binding. expansion != LocalExpnId :: ROOT
388
396
&& glob_binding. res ( ) != non_glob_binding. res ( )
389
397
{
@@ -489,10 +497,10 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
489
497
} ;
490
498
if self . is_accessible_from ( binding. vis , scope) {
491
499
let imported_binding = self . import ( binding, * import) ;
492
- let key = BindingKey { ident, ..key } ;
493
500
let _ = self . try_define (
494
501
import. parent_scope . module ,
495
- key,
502
+ ident,
503
+ key. ns ,
496
504
imported_binding,
497
505
warn_ambiguity,
498
506
) ;
@@ -514,11 +522,15 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
514
522
let dummy_binding = self . dummy_binding ;
515
523
let dummy_binding = self . import ( dummy_binding, import) ;
516
524
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
+ }
522
534
} ) ;
523
535
self . record_use ( target, dummy_binding, Used :: Other ) ;
524
536
} else if import. imported_module . get ( ) . is_none ( ) {
@@ -895,7 +907,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
895
907
PendingBinding :: Ready ( Some ( imported_binding) )
896
908
}
897
909
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.
899
911
if target. name != kw:: Underscore {
900
912
let key = BindingKey :: new ( target, ns) ;
901
913
this. update_resolution ( parent, key, false , |_, resolution| {
@@ -1510,7 +1522,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1510
1522
. is_some_and ( |binding| binding. warn_ambiguity_recursive ( ) ) ;
1511
1523
let _ = self . try_define (
1512
1524
import. parent_scope . module ,
1513
- key,
1525
+ key. ident ,
1526
+ key. ns ,
1514
1527
imported_binding,
1515
1528
warn_ambiguity,
1516
1529
) ;
0 commit comments