@@ -300,10 +300,10 @@ fn remove_same_import<'ra>(d1: Decl<'ra>, d2: Decl<'ra>) -> (Decl<'ra>, Decl<'ra
300300 if let DeclKind :: Import { import : import1, source_decl : d1_next } = d1. kind
301301 && let DeclKind :: Import { import : import2, source_decl : d2_next } = d2. kind
302302 && import1 == import2
303- && d1. ambiguity == d2. ambiguity
303+ && d1. warn_ambiguity . get ( ) == d2. warn_ambiguity . get ( )
304304 {
305- assert ! ( d1. ambiguity. is_none ( ) ) ;
306- assert_eq ! ( d1 . warn_ambiguity . get ( ) , d2 . warn_ambiguity. get( ) ) ;
305+ assert_eq ! ( d1. ambiguity. get ( ) , d2 . ambiguity . get ( ) ) ;
306+ assert ! ( !d1 . warn_ambiguity. get( ) ) ;
307307 assert_eq ! ( d1. expansion, d2. expansion) ;
308308 assert_eq ! ( d1. span, d2. span) ;
309309 assert_eq ! ( d1. vis( ) , d2. vis( ) ) ;
@@ -335,7 +335,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
335335
336336 self . arenas . alloc_decl ( DeclData {
337337 kind : DeclKind :: Import { source_decl : decl, import } ,
338- ambiguity : None ,
338+ ambiguity : CmCell :: new ( None ) ,
339339 warn_ambiguity : CmCell :: new ( false ) ,
340340 span : import. span ,
341341 vis : CmCell :: new ( vis) ,
@@ -359,9 +359,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
359359 // all these overwrites will be re-fetched by glob imports importing
360360 // from that module without generating new ambiguities.
361361 // - A glob decl is overwritten by a non-glob decl arriving later.
362- // - A glob decl is overwritten by an ambiguous glob decl .
363- // FIXME: avoid this by putting `DeclData::ambiguity` under a
364- // cell and updating it in place .
362+ // - A glob decl is overwritten by its clone after setting ambiguity in it .
363+ // FIXME: avoid this by removing `warn_ambiguity`, or by triggering glob re-fetch
364+ // with the same decl in some way .
365365 // - A glob decl is overwritten by a glob decl re-fetching an
366366 // overwritten decl from other module (the recursive case).
367367 // Here we are detecting all such re-fetches and overwrite old decls
@@ -372,21 +372,34 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
372372 if deep_decl != glob_decl {
373373 // Some import layers have been removed, need to overwrite.
374374 assert_ne ! ( old_deep_decl, old_glob_decl) ;
375- assert_ne ! ( old_deep_decl, deep_decl) ;
376- assert ! ( old_deep_decl. is_glob_import( ) ) ;
375+ // FIXME: reenable the asserts when `warn_ambiguity` is removed (#149195).
376+ // assert_ne!(old_deep_decl, deep_decl);
377+ // assert!(old_deep_decl.is_glob_import());
378+ assert ! ( !deep_decl. is_glob_import( ) ) ;
377379 if glob_decl. is_ambiguity_recursive ( ) {
378380 glob_decl. warn_ambiguity . set_unchecked ( true ) ;
379381 }
380382 glob_decl
381383 } else if glob_decl. res ( ) != old_glob_decl. res ( ) {
382- self . new_decl_with_ambiguity ( old_glob_decl, glob_decl, warn_ambiguity)
384+ old_glob_decl. ambiguity . set_unchecked ( Some ( glob_decl) ) ;
385+ old_glob_decl. warn_ambiguity . set_unchecked ( warn_ambiguity) ;
386+ if warn_ambiguity {
387+ old_glob_decl
388+ } else {
389+ // Need a fresh decl so other glob imports importing it could re-fetch it
390+ // and set their own `warn_ambiguity` to true.
391+ // FIXME: remove this when `warn_ambiguity` is removed (#149195).
392+ self . arenas . alloc_decl ( ( * old_glob_decl) . clone ( ) )
393+ }
383394 } else if !old_glob_decl. vis ( ) . is_at_least ( glob_decl. vis ( ) , self . tcx ) {
384395 // We are glob-importing the same item but with greater visibility.
385396 old_glob_decl. vis . set_unchecked ( glob_decl. vis ( ) ) ;
386397 old_glob_decl
387398 } else if glob_decl. is_ambiguity_recursive ( ) && !old_glob_decl. is_ambiguity_recursive ( ) {
388399 // Overwriting a non-ambiguous glob import with an ambiguous glob import.
389- self . new_decl_with_ambiguity ( old_glob_decl, glob_decl, true )
400+ old_glob_decl. ambiguity . set_unchecked ( Some ( glob_decl) ) ;
401+ old_glob_decl. warn_ambiguity . set_unchecked ( true ) ;
402+ old_glob_decl
390403 } else {
391404 old_glob_decl
392405 }
@@ -415,6 +428,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
415428 self . update_local_resolution ( module, key, warn_ambiguity, |this, resolution| {
416429 if let Some ( old_decl) = resolution. best_decl ( ) {
417430 assert_ne ! ( decl, old_decl) ;
431+ assert ! ( !decl. warn_ambiguity. get( ) ) ;
418432 if res == Res :: Err && old_decl. res ( ) != Res :: Err {
419433 // Do not override real declarations with `Res::Err`s from error recovery.
420434 return Ok ( ( ) ) ;
@@ -453,19 +467,6 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
453467 } )
454468 }
455469
456- fn new_decl_with_ambiguity (
457- & self ,
458- primary_decl : Decl < ' ra > ,
459- secondary_decl : Decl < ' ra > ,
460- warn_ambiguity : bool ,
461- ) -> Decl < ' ra > {
462- let ambiguity = Some ( secondary_decl) ;
463- let warn_ambiguity = CmCell :: new ( warn_ambiguity) ;
464- let vis = primary_decl. vis . clone ( ) ;
465- let data = DeclData { ambiguity, warn_ambiguity, vis, ..* primary_decl } ;
466- self . arenas . alloc_decl ( data)
467- }
468-
469470 // Use `f` to mutate the resolution of the name in the module.
470471 // If the resolution becomes a success, define it in the module's glob importers.
471472 fn update_local_resolution < T , F > (
@@ -671,7 +672,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
671672 let Some ( binding) = resolution. best_decl ( ) else { continue } ;
672673
673674 if let DeclKind :: Import { import, .. } = binding. kind
674- && let Some ( amb_binding) = binding. ambiguity
675+ && let Some ( amb_binding) = binding. ambiguity . get ( )
675676 && binding. res ( ) != Res :: Err
676677 && exported_ambiguities. contains ( & binding)
677678 {
0 commit comments