@@ -22,7 +22,7 @@ use rustc_span::edit_distance::find_best_match_for_name;
22
22
use rustc_span:: hygiene:: LocalExpnId ;
23
23
use rustc_span:: { Ident , Span , Symbol , kw, sym} ;
24
24
use smallvec:: SmallVec ;
25
- use tracing:: debug;
25
+ use tracing:: { debug, instrument } ;
26
26
27
27
use crate :: Determinacy :: { self , * } ;
28
28
use crate :: Namespace :: * ;
@@ -242,21 +242,20 @@ pub(crate) struct NameResolution<'ra> {
242
242
/// Single imports that may define the name in the namespace.
243
243
/// Imports are arena-allocated, so it's ok to use pointers as keys.
244
244
pub single_imports : FxIndexSet < Import < ' ra > > ,
245
- /// The least shadowable known binding for this name, or None if there are no known bindings.
246
- pub binding : Option < NameBinding < ' ra > > ,
247
- pub shadowed_glob : Option < NameBinding < ' ra > > ,
245
+ /// The least shadowable known non-glob binding for this name, or None if there are no known bindings.
246
+ pub non_glob_binding : Option < NameBinding < ' ra > > ,
247
+ pub glob_binding : Option < NameBinding < ' ra > > ,
248
248
}
249
249
250
250
impl < ' ra > NameResolution < ' ra > {
251
251
/// Returns the binding for the name if it is known or None if it not known.
252
252
pub ( crate ) fn binding ( & self ) -> Option < NameBinding < ' ra > > {
253
- self . binding . and_then ( |binding| {
254
- if !binding. is_glob_import ( ) || self . single_imports . is_empty ( ) {
255
- Some ( binding)
256
- } else {
257
- None
258
- }
259
- } )
253
+ self . non_glob_binding
254
+ . or_else ( || if self . single_imports . is_empty ( ) { self . glob_binding } else { None } )
255
+ }
256
+
257
+ pub ( crate ) fn late_binding ( & self ) -> Option < NameBinding < ' ra > > {
258
+ self . non_glob_binding . or_else ( || self . glob_binding )
260
259
}
261
260
}
262
261
@@ -338,77 +337,79 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
338
337
self . check_reserved_macro_name ( key. ident , res) ;
339
338
self . set_binding_parent_module ( binding, module) ;
340
339
self . update_resolution ( module, key, warn_ambiguity, |this, resolution| {
341
- if let Some ( old_binding ) = resolution. binding {
342
- if res == Res :: Err && old_binding . res ( ) != Res :: Err {
340
+ if let Some ( old_non_glob_binding ) = resolution. non_glob_binding {
341
+ if res == Res :: Err && old_non_glob_binding . res ( ) != Res :: Err {
343
342
// Do not override real bindings with `Res::Err`s from error recovery.
344
343
return Ok ( ( ) ) ;
345
344
}
346
- match ( old_binding. is_glob_import ( ) , binding. is_glob_import ( ) ) {
347
- ( true , true ) => {
348
- // FIXME: remove `!binding.is_ambiguity_recursive()` after delete the warning ambiguity.
349
- if !binding. is_ambiguity_recursive ( )
350
- && let NameBindingKind :: Import { import : old_import, .. } =
351
- old_binding. kind
352
- && let NameBindingKind :: Import { import, .. } = binding. kind
353
- && old_import == import
354
- {
355
- // We should replace the `old_binding` with `binding` regardless
356
- // of whether they has same resolution or not when they are
357
- // imported from the same glob-import statement.
358
- resolution. binding = Some ( binding) ;
359
- } else if res != old_binding. res ( ) {
360
- resolution. binding = Some ( this. new_ambiguity_binding (
361
- AmbiguityKind :: GlobVsGlob ,
362
- old_binding,
363
- binding,
364
- warn_ambiguity,
365
- ) ) ;
366
- } else if !old_binding. vis . is_at_least ( binding. vis , this. tcx ) {
367
- // We are glob-importing the same item but with greater visibility.
368
- resolution. binding = Some ( binding) ;
369
- } else if binding. is_ambiguity_recursive ( ) {
370
- resolution. binding = Some ( this. new_warn_ambiguity_binding ( binding) ) ;
371
- }
345
+
346
+ if binding. is_glob_import ( ) {
347
+ let ( glob_binding, nonglob_binding) = ( binding, old_non_glob_binding) ;
348
+
349
+ if key. ns == MacroNS
350
+ && nonglob_binding. expansion != LocalExpnId :: ROOT
351
+ && glob_binding. res ( ) != nonglob_binding. res ( )
352
+ {
353
+ resolution. non_glob_binding = Some ( this. new_ambiguity_binding (
354
+ AmbiguityKind :: GlobVsExpanded ,
355
+ nonglob_binding,
356
+ glob_binding,
357
+ false ,
358
+ ) ) ;
372
359
}
373
- ( old_glob @ true , false ) | ( old_glob @ false , true ) => {
374
- let ( glob_binding, nonglob_binding) =
375
- if old_glob { ( old_binding, binding) } else { ( binding, old_binding) } ;
376
- if key. ns == MacroNS
377
- && nonglob_binding. expansion != LocalExpnId :: ROOT
378
- && glob_binding. res ( ) != nonglob_binding. res ( )
379
- {
380
- resolution. binding = Some ( this. new_ambiguity_binding (
381
- AmbiguityKind :: GlobVsExpanded ,
382
- nonglob_binding,
360
+
361
+ if let Some ( old_glob) = resolution. glob_binding {
362
+ if glob_binding. res ( ) != old_glob. res ( ) {
363
+ resolution. glob_binding = Some ( this. new_ambiguity_binding (
364
+ AmbiguityKind :: GlobVsGlob ,
365
+ old_glob,
383
366
glob_binding,
384
- false ,
367
+ warn_ambiguity ,
385
368
) ) ;
386
- } else {
387
- resolution. binding = Some ( nonglob_binding) ;
388
- }
389
-
390
- if let Some ( old_shadowed_glob) = resolution. shadowed_glob {
391
- assert ! ( old_shadowed_glob. is_glob_import( ) ) ;
392
- if glob_binding. res ( ) != old_shadowed_glob. res ( ) {
393
- resolution. shadowed_glob = Some ( this. new_ambiguity_binding (
394
- AmbiguityKind :: GlobVsGlob ,
395
- old_shadowed_glob,
396
- glob_binding,
397
- false ,
398
- ) ) ;
399
- } else if !old_shadowed_glob. vis . is_at_least ( binding. vis , this. tcx ) {
400
- resolution. shadowed_glob = Some ( glob_binding) ;
401
- }
402
- } else {
403
- resolution. shadowed_glob = Some ( glob_binding) ;
369
+ } else if !old_glob. vis . is_at_least ( glob_binding. vis , this. tcx ) {
370
+ resolution. glob_binding = Some ( glob_binding) ;
404
371
}
372
+ } else {
373
+ resolution. glob_binding = Some ( glob_binding) ;
405
374
}
406
- ( false , false ) => {
407
- return Err ( old_binding) ;
375
+ } else {
376
+ return Err ( old_non_glob_binding) ;
377
+ }
378
+ } else if let Some ( old_glob_binding) = resolution. glob_binding {
379
+ if binding. is_glob_import ( ) {
380
+ // FIXME: remove `!binding.is_ambiguity_recursive()` after delete the warning ambiguity.
381
+ if !binding. is_ambiguity_recursive ( )
382
+ && let NameBindingKind :: Import { import : old_import, .. } =
383
+ old_glob_binding. kind
384
+ && let NameBindingKind :: Import { import, .. } = binding. kind
385
+ && old_import == import
386
+ {
387
+ // We should replace the `old_binding` with `binding` regardless
388
+ // of whether they has same resolution or not when they are
389
+ // imported from the same glob-import statement.
390
+ resolution. glob_binding = Some ( binding) ;
391
+ } else if res != old_glob_binding. res ( ) {
392
+ resolution. glob_binding = Some ( this. new_ambiguity_binding (
393
+ AmbiguityKind :: GlobVsGlob ,
394
+ old_glob_binding,
395
+ binding,
396
+ warn_ambiguity,
397
+ ) ) ;
398
+ } else if !old_glob_binding. vis . is_at_least ( binding. vis , this. tcx ) {
399
+ // We are glob-importing the same item but with greater visibility.
400
+ resolution. glob_binding = Some ( binding) ;
401
+ } else if binding. is_ambiguity_recursive ( ) {
402
+ resolution. glob_binding = Some ( this. new_warn_ambiguity_binding ( binding) ) ;
408
403
}
404
+ } else {
405
+ resolution. non_glob_binding = Some ( binding) ;
409
406
}
410
407
} else {
411
- resolution. binding = Some ( binding) ;
408
+ if binding. is_glob_import ( ) {
409
+ resolution. glob_binding = Some ( binding) ;
410
+ } else {
411
+ resolution. non_glob_binding = Some ( binding) ;
412
+ }
412
413
}
413
414
414
415
Ok ( ( ) )
@@ -620,15 +621,17 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
620
621
}
621
622
}
622
623
624
+ #[ instrument( skip( self ) , level = "debug" ) ]
623
625
pub ( crate ) fn check_hidden_glob_reexports (
624
626
& mut self ,
625
627
exported_ambiguities : FxHashSet < NameBinding < ' ra > > ,
626
628
) {
627
629
for module in self . arenas . local_modules ( ) . iter ( ) {
628
630
for ( key, resolution) in self . resolutions ( * module) . borrow ( ) . iter ( ) {
629
631
let resolution = resolution. borrow ( ) ;
632
+ debug ! ( ?resolution) ;
630
633
631
- let Some ( binding) = resolution. binding else { continue } ;
634
+ let Some ( binding) = resolution. late_binding ( ) else { continue } ;
632
635
633
636
if let NameBindingKind :: Import { import, .. } = binding. kind
634
637
&& let Some ( ( amb_binding, _) ) = binding. ambiguity
@@ -648,7 +651,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
648
651
) ;
649
652
}
650
653
651
- if let Some ( glob_binding) = resolution. shadowed_glob {
654
+ if let Some ( glob_binding) = resolution. glob_binding {
652
655
if binding. res ( ) != Res :: Err
653
656
&& glob_binding. res ( ) != Res :: Err
654
657
&& let NameBindingKind :: Import { import : glob_import, .. } =
@@ -1179,7 +1182,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1179
1182
return None ;
1180
1183
} // Never suggest the same name
1181
1184
match * resolution. borrow ( ) {
1182
- NameResolution { binding : Some ( name_binding) , .. } => {
1185
+ NameResolution { non_glob_binding : Some ( name_binding) , .. } => {
1183
1186
match name_binding. kind {
1184
1187
NameBindingKind :: Import { binding, .. } => {
1185
1188
match binding. kind {
0 commit comments