@@ -242,22 +242,27 @@ 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 non-glob binding for this name, if it is known to exist.
246
+ pub non_glob_binding : Option < NameBinding < ' ra > > ,
247
+ /// The glob binding for this name, if it is known to exist.
248
+ pub glob_binding : Option < NameBinding < ' ra > > ,
248
249
}
249
250
250
251
impl < ' ra > NameResolution < ' ra > {
251
252
/// Returns the binding for the name if it is known or None if it not known.
252
253
pub ( crate ) fn binding ( & self ) -> Option < NameBinding < ' ra > > {
253
- self . binding . and_then ( |binding| {
254
+ self . best_binding ( ) . and_then ( |binding| {
254
255
if !binding. is_glob_import ( ) || self . single_imports . is_empty ( ) {
255
256
Some ( binding)
256
257
} else {
257
258
None
258
259
}
259
260
} )
260
261
}
262
+
263
+ pub ( crate ) fn best_binding ( & self ) -> Option < NameBinding < ' ra > > {
264
+ self . non_glob_binding . or ( self . glob_binding )
265
+ }
261
266
}
262
267
263
268
/// An error that may be transformed into a diagnostic later. Used to combine multiple unresolved
@@ -338,77 +343,83 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
338
343
self . check_reserved_macro_name ( key. ident , res) ;
339
344
self . set_binding_parent_module ( binding, module) ;
340
345
self . update_resolution ( module, key, warn_ambiguity, |this, resolution| {
341
- if let Some ( old_binding) = resolution. binding {
346
+ if let Some ( old_binding) = resolution. best_binding ( ) {
342
347
if res == Res :: Err && old_binding. res ( ) != Res :: Err {
343
348
// Do not override real bindings with `Res::Err`s from error recovery.
344
349
return Ok ( ( ) ) ;
345
350
}
346
351
match ( old_binding. is_glob_import ( ) , binding. is_glob_import ( ) ) {
347
352
( true , true ) => {
353
+ let ( glob_binding, old_glob_binding) = ( binding, old_binding) ;
348
354
// FIXME: remove `!binding.is_ambiguity_recursive()` after delete the warning ambiguity.
349
355
if !binding. is_ambiguity_recursive ( )
350
356
&& let NameBindingKind :: Import { import : old_import, .. } =
351
- old_binding . kind
352
- && let NameBindingKind :: Import { import, .. } = binding . kind
357
+ old_glob_binding . kind
358
+ && let NameBindingKind :: Import { import, .. } = glob_binding . kind
353
359
&& old_import == import
354
360
{
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
+ // When imported from the same glob-import statement, we should replace
362
+ // `old_glob_binding` with `glob_binding`, regardless of whether
363
+ // they have the same resolution or not .
364
+ resolution. glob_binding = Some ( glob_binding ) ;
365
+ } else if res != old_glob_binding . res ( ) {
366
+ resolution. glob_binding = Some ( this. new_ambiguity_binding (
361
367
AmbiguityKind :: GlobVsGlob ,
362
- old_binding ,
363
- binding ,
368
+ old_glob_binding ,
369
+ glob_binding ,
364
370
warn_ambiguity,
365
371
) ) ;
366
372
} else if !old_binding. vis . is_at_least ( binding. vis , this. tcx ) {
367
373
// We are glob-importing the same item but with greater visibility.
368
- resolution. binding = Some ( binding ) ;
374
+ resolution. glob_binding = Some ( glob_binding ) ;
369
375
} else if binding. is_ambiguity_recursive ( ) {
370
- resolution. binding = Some ( this. new_warn_ambiguity_binding ( binding) ) ;
376
+ resolution. glob_binding =
377
+ Some ( this. new_warn_ambiguity_binding ( glob_binding) ) ;
371
378
}
372
379
}
373
380
( old_glob @ true , false ) | ( old_glob @ false , true ) => {
374
- let ( glob_binding, nonglob_binding ) =
381
+ let ( glob_binding, non_glob_binding ) =
375
382
if old_glob { ( old_binding, binding) } else { ( binding, old_binding) } ;
376
383
if key. ns == MacroNS
377
- && nonglob_binding . expansion != LocalExpnId :: ROOT
378
- && glob_binding. res ( ) != nonglob_binding . res ( )
384
+ && non_glob_binding . expansion != LocalExpnId :: ROOT
385
+ && glob_binding. res ( ) != non_glob_binding . res ( )
379
386
{
380
- resolution. binding = Some ( this. new_ambiguity_binding (
387
+ resolution. non_glob_binding = Some ( this. new_ambiguity_binding (
381
388
AmbiguityKind :: GlobVsExpanded ,
382
- nonglob_binding ,
389
+ non_glob_binding ,
383
390
glob_binding,
384
391
false ,
385
392
) ) ;
386
393
} else {
387
- resolution. binding = Some ( nonglob_binding ) ;
394
+ resolution. non_glob_binding = Some ( non_glob_binding ) ;
388
395
}
389
396
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 (
397
+ if let Some ( old_glob_binding ) = resolution. glob_binding {
398
+ assert ! ( old_glob_binding . is_glob_import( ) ) ;
399
+ if glob_binding. res ( ) != old_glob_binding . res ( ) {
400
+ resolution. glob_binding = Some ( this. new_ambiguity_binding (
394
401
AmbiguityKind :: GlobVsGlob ,
395
- old_shadowed_glob ,
402
+ old_glob_binding ,
396
403
glob_binding,
397
404
false ,
398
405
) ) ;
399
- } else if !old_shadowed_glob . vis . is_at_least ( binding. vis , this. tcx ) {
400
- resolution. shadowed_glob = Some ( glob_binding) ;
406
+ } else if !old_glob_binding . vis . is_at_least ( binding. vis , this. tcx ) {
407
+ resolution. glob_binding = Some ( glob_binding) ;
401
408
}
402
409
} else {
403
- resolution. shadowed_glob = Some ( glob_binding) ;
410
+ resolution. glob_binding = Some ( glob_binding) ;
404
411
}
405
412
}
406
413
( false , false ) => {
407
414
return Err ( old_binding) ;
408
415
}
409
416
}
410
417
} else {
411
- resolution. binding = Some ( binding) ;
418
+ if binding. is_glob_import ( ) {
419
+ resolution. glob_binding = Some ( binding) ;
420
+ } else {
421
+ resolution. non_glob_binding = Some ( binding) ;
422
+ }
412
423
}
413
424
414
425
Ok ( ( ) )
@@ -628,7 +639,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
628
639
for ( key, resolution) in self . resolutions ( * module) . borrow ( ) . iter ( ) {
629
640
let resolution = resolution. borrow ( ) ;
630
641
631
- let Some ( binding) = resolution. binding else { continue } ;
642
+ let Some ( binding) = resolution. best_binding ( ) else { continue } ;
632
643
633
644
if let NameBindingKind :: Import { import, .. } = binding. kind
634
645
&& let Some ( ( amb_binding, _) ) = binding. ambiguity
@@ -648,7 +659,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
648
659
) ;
649
660
}
650
661
651
- if let Some ( glob_binding) = resolution. shadowed_glob {
662
+ if let Some ( glob_binding) = resolution. glob_binding
663
+ && resolution. non_glob_binding . is_some ( )
664
+ {
652
665
if binding. res ( ) != Res :: Err
653
666
&& glob_binding. res ( ) != Res :: Err
654
667
&& let NameBindingKind :: Import { import : glob_import, .. } =
@@ -1179,7 +1192,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1179
1192
return None ;
1180
1193
} // Never suggest the same name
1181
1194
match * resolution. borrow ( ) {
1182
- NameResolution { binding : Some ( name_binding) , .. } => {
1195
+ ref resolution
1196
+ if let Some ( name_binding) = resolution. best_binding ( ) =>
1197
+ {
1183
1198
match name_binding. kind {
1184
1199
NameBindingKind :: Import { binding, .. } => {
1185
1200
match binding. kind {
0 commit comments