@@ -244,22 +244,27 @@ pub(crate) struct NameResolution<'ra> {
244
244
/// Single imports that may define the name in the namespace.
245
245
/// Imports are arena-allocated, so it's ok to use pointers as keys.
246
246
pub single_imports : FxIndexSet < Import < ' ra > > ,
247
- /// The least shadowable known binding for this name, or None if there are no known bindings.
248
- pub binding : Option < NameBinding < ' ra > > ,
249
- pub shadowed_glob : Option < NameBinding < ' ra > > ,
247
+ /// The non-glob binding for this name, if it is known to exist.
248
+ pub non_glob_binding : Option < NameBinding < ' ra > > ,
249
+ /// The glob binding for this name, if it is known to exist.
250
+ pub glob_binding : Option < NameBinding < ' ra > > ,
250
251
}
251
252
252
253
impl < ' ra > NameResolution < ' ra > {
253
254
/// Returns the binding for the name if it is known or None if it not known.
254
255
pub ( crate ) fn binding ( & self ) -> Option < NameBinding < ' ra > > {
255
- self . binding . and_then ( |binding| {
256
+ self . best_binding ( ) . and_then ( |binding| {
256
257
if !binding. is_glob_import ( ) || self . single_imports . is_empty ( ) {
257
258
Some ( binding)
258
259
} else {
259
260
None
260
261
}
261
262
} )
262
263
}
264
+
265
+ pub ( crate ) fn best_binding ( & self ) -> Option < NameBinding < ' ra > > {
266
+ self . non_glob_binding . or ( self . glob_binding )
267
+ }
263
268
}
264
269
265
270
/// An error that may be transformed into a diagnostic later. Used to combine multiple unresolved
@@ -340,77 +345,83 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
340
345
self . check_reserved_macro_name ( key. ident , res) ;
341
346
self . set_binding_parent_module ( binding, module) ;
342
347
self . update_resolution ( module, key, warn_ambiguity, |this, resolution| {
343
- if let Some ( old_binding) = resolution. binding {
348
+ if let Some ( old_binding) = resolution. best_binding ( ) {
344
349
if res == Res :: Err && old_binding. res ( ) != Res :: Err {
345
350
// Do not override real bindings with `Res::Err`s from error recovery.
346
351
return Ok ( ( ) ) ;
347
352
}
348
353
match ( old_binding. is_glob_import ( ) , binding. is_glob_import ( ) ) {
349
354
( true , true ) => {
355
+ let ( glob_binding, old_glob_binding) = ( binding, old_binding) ;
350
356
// FIXME: remove `!binding.is_ambiguity_recursive()` after delete the warning ambiguity.
351
357
if !binding. is_ambiguity_recursive ( )
352
358
&& let NameBindingKind :: Import { import : old_import, .. } =
353
- old_binding . kind
354
- && let NameBindingKind :: Import { import, .. } = binding . kind
359
+ old_glob_binding . kind
360
+ && let NameBindingKind :: Import { import, .. } = glob_binding . kind
355
361
&& old_import == import
356
362
{
357
- // We should replace the `old_binding` with `binding` regardless
358
- // of whether they has same resolution or not when they are
359
- // imported from the same glob-import statement .
360
- resolution. binding = Some ( binding ) ;
361
- } else if res != old_binding . res ( ) {
362
- resolution. binding = Some ( this. new_ambiguity_binding (
363
+ // When imported from the same glob-import statement, we should replace
364
+ // `old_glob_binding` with `glob_binding`, regardless of whether
365
+ // they have the same resolution or not .
366
+ resolution. glob_binding = Some ( glob_binding ) ;
367
+ } else if res != old_glob_binding . res ( ) {
368
+ resolution. glob_binding = Some ( this. new_ambiguity_binding (
363
369
AmbiguityKind :: GlobVsGlob ,
364
- old_binding ,
365
- binding ,
370
+ old_glob_binding ,
371
+ glob_binding ,
366
372
warn_ambiguity,
367
373
) ) ;
368
374
} else if !old_binding. vis . is_at_least ( binding. vis , this. tcx ) {
369
375
// We are glob-importing the same item but with greater visibility.
370
- resolution. binding = Some ( binding ) ;
376
+ resolution. glob_binding = Some ( glob_binding ) ;
371
377
} else if binding. is_ambiguity_recursive ( ) {
372
- resolution. binding = Some ( this. new_warn_ambiguity_binding ( binding) ) ;
378
+ resolution. glob_binding =
379
+ Some ( this. new_warn_ambiguity_binding ( glob_binding) ) ;
373
380
}
374
381
}
375
382
( old_glob @ true , false ) | ( old_glob @ false , true ) => {
376
- let ( glob_binding, nonglob_binding ) =
383
+ let ( glob_binding, non_glob_binding ) =
377
384
if old_glob { ( old_binding, binding) } else { ( binding, old_binding) } ;
378
385
if key. ns == MacroNS
379
- && nonglob_binding . expansion != LocalExpnId :: ROOT
380
- && glob_binding. res ( ) != nonglob_binding . res ( )
386
+ && non_glob_binding . expansion != LocalExpnId :: ROOT
387
+ && glob_binding. res ( ) != non_glob_binding . res ( )
381
388
{
382
- resolution. binding = Some ( this. new_ambiguity_binding (
389
+ resolution. non_glob_binding = Some ( this. new_ambiguity_binding (
383
390
AmbiguityKind :: GlobVsExpanded ,
384
- nonglob_binding ,
391
+ non_glob_binding ,
385
392
glob_binding,
386
393
false ,
387
394
) ) ;
388
395
} else {
389
- resolution. binding = Some ( nonglob_binding ) ;
396
+ resolution. non_glob_binding = Some ( non_glob_binding ) ;
390
397
}
391
398
392
- if let Some ( old_shadowed_glob ) = resolution. shadowed_glob {
393
- assert ! ( old_shadowed_glob . is_glob_import( ) ) ;
394
- if glob_binding. res ( ) != old_shadowed_glob . res ( ) {
395
- resolution. shadowed_glob = Some ( this. new_ambiguity_binding (
399
+ if let Some ( old_glob_binding ) = resolution. glob_binding {
400
+ assert ! ( old_glob_binding . is_glob_import( ) ) ;
401
+ if glob_binding. res ( ) != old_glob_binding . res ( ) {
402
+ resolution. glob_binding = Some ( this. new_ambiguity_binding (
396
403
AmbiguityKind :: GlobVsGlob ,
397
- old_shadowed_glob ,
404
+ old_glob_binding ,
398
405
glob_binding,
399
406
false ,
400
407
) ) ;
401
- } else if !old_shadowed_glob . vis . is_at_least ( binding. vis , this. tcx ) {
402
- resolution. shadowed_glob = Some ( glob_binding) ;
408
+ } else if !old_glob_binding . vis . is_at_least ( binding. vis , this. tcx ) {
409
+ resolution. glob_binding = Some ( glob_binding) ;
403
410
}
404
411
} else {
405
- resolution. shadowed_glob = Some ( glob_binding) ;
412
+ resolution. glob_binding = Some ( glob_binding) ;
406
413
}
407
414
}
408
415
( false , false ) => {
409
416
return Err ( old_binding) ;
410
417
}
411
418
}
412
419
} else {
413
- resolution. binding = Some ( binding) ;
420
+ if binding. is_glob_import ( ) {
421
+ resolution. glob_binding = Some ( binding) ;
422
+ } else {
423
+ resolution. non_glob_binding = Some ( binding) ;
424
+ }
414
425
}
415
426
416
427
Ok ( ( ) )
@@ -631,7 +642,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
631
642
for ( key, resolution) in self . resolutions ( * module) . borrow ( ) . iter ( ) {
632
643
let resolution = resolution. borrow ( ) ;
633
644
634
- let Some ( binding) = resolution. binding else { continue } ;
645
+ let Some ( binding) = resolution. best_binding ( ) else { continue } ;
635
646
636
647
if let NameBindingKind :: Import { import, .. } = binding. kind
637
648
&& let Some ( ( amb_binding, _) ) = binding. ambiguity
@@ -651,7 +662,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
651
662
) ;
652
663
}
653
664
654
- if let Some ( glob_binding) = resolution. shadowed_glob {
665
+ if let Some ( glob_binding) = resolution. glob_binding
666
+ && resolution. non_glob_binding . is_some ( )
667
+ {
655
668
if binding. res ( ) != Res :: Err
656
669
&& glob_binding. res ( ) != Res :: Err
657
670
&& let NameBindingKind :: Import { import : glob_import, .. } =
@@ -1172,7 +1185,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
1172
1185
return None ;
1173
1186
} // Never suggest the same name
1174
1187
match * resolution. borrow ( ) {
1175
- NameResolution { binding : Some ( name_binding) , .. } => {
1188
+ ref resolution
1189
+ if let Some ( name_binding) = resolution. best_binding ( ) =>
1190
+ {
1176
1191
match name_binding. kind {
1177
1192
NameBindingKind :: Import { binding, .. } => {
1178
1193
match binding. kind {
0 commit comments