@@ -5,6 +5,7 @@ use Namespace::*;
55use rustc_ast:: { self as ast, NodeId } ;
66use rustc_errors:: ErrorGuaranteed ;
77use rustc_hir:: def:: { DefKind , MacroKinds , Namespace , NonMacroAttrKind , PartialRes , PerNS } ;
8+ use rustc_middle:: ty:: Visibility ;
89use rustc_middle:: { bug, span_bug} ;
910use rustc_session:: lint:: builtin:: PROC_MACRO_DERIVE_RESOLUTION_FALLBACK ;
1011use rustc_session:: parse:: feature_err;
@@ -469,9 +470,12 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
469470 // We do not need to report them if we are either in speculative resolution,
470471 // or in late resolution when everything is already imported and expanded
471472 // and no ambiguities exist.
472- if matches ! ( finalize, None | Some ( Finalize { stage: Stage :: Late , .. } ) ) {
473- return ControlFlow :: Break ( Ok ( binding) ) ;
474- }
473+ let import_vis = match finalize {
474+ None | Some ( Finalize { stage : Stage :: Late , .. } ) => {
475+ return ControlFlow :: Break ( Ok ( binding) ) ;
476+ }
477+ Some ( Finalize { import_vis, .. } ) => import_vis,
478+ } ;
475479
476480 if let Some ( ( innermost_binding, innermost_flags) ) = innermost_result {
477481 // Found another solution, if the first one was "weak", report an error.
@@ -482,6 +486,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
482486 innermost_binding,
483487 flags,
484488 innermost_flags,
489+ import_vis,
485490 extern_prelude_item_binding,
486491 extern_prelude_flag_binding,
487492 ) {
@@ -730,11 +735,13 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
730735 innermost_binding : NameBinding < ' ra > ,
731736 flags : Flags ,
732737 innermost_flags : Flags ,
738+ import_vis : Option < Visibility > ,
733739 extern_prelude_item_binding : Option < NameBinding < ' ra > > ,
734740 extern_prelude_flag_binding : Option < NameBinding < ' ra > > ,
735741 ) -> bool {
736742 let ( res, innermost_res) = ( binding. res ( ) , innermost_binding. res ( ) ) ;
737- if res == innermost_res {
743+ let ambig_vis = self . ambig_vis ( import_vis, binding, innermost_binding) ;
744+ if res == innermost_res && ambig_vis. is_none ( ) {
738745 return false ;
739746 }
740747
@@ -793,10 +800,11 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
793800 } ;
794801 self . ambiguity_errors . push ( AmbiguityError {
795802 kind,
803+ ambig_vis,
796804 ident : orig_ident,
797805 b1 : innermost_binding,
798806 b2 : binding,
799- warning : false ,
807+ warning : ambig_vis . is_some ( ) ,
800808 misc1 : misc ( innermost_flags) ,
801809 misc2 : misc ( flags) ,
802810 } ) ;
@@ -1207,17 +1215,20 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12071215 && shadowing == Shadowing :: Restricted
12081216 && finalize. stage == Stage :: Early
12091217 && binding. expansion != LocalExpnId :: ROOT
1210- && binding. res ( ) != shadowed_glob. res ( )
12111218 {
1212- self . ambiguity_errors . push ( AmbiguityError {
1213- kind : AmbiguityKind :: GlobVsExpanded ,
1214- ident,
1215- b1 : binding,
1216- b2 : shadowed_glob,
1217- warning : false ,
1218- misc1 : AmbiguityErrorMisc :: None ,
1219- misc2 : AmbiguityErrorMisc :: None ,
1220- } ) ;
1219+ let ambig_vis = self . ambig_vis ( finalize. import_vis , shadowed_glob, binding) ;
1220+ if shadowed_glob. res ( ) != binding. res ( ) || ambig_vis. is_some ( ) {
1221+ self . ambiguity_errors . push ( AmbiguityError {
1222+ kind : AmbiguityKind :: GlobVsExpanded ,
1223+ ambig_vis,
1224+ ident,
1225+ b1 : binding,
1226+ b2 : shadowed_glob,
1227+ warning : ambig_vis. is_some ( ) ,
1228+ misc1 : AmbiguityErrorMisc :: None ,
1229+ misc2 : AmbiguityErrorMisc :: None ,
1230+ } ) ;
1231+ }
12211232 }
12221233
12231234 if shadowing == Shadowing :: Unrestricted
@@ -1324,6 +1335,23 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
13241335 false
13251336 }
13261337
1338+ fn ambig_vis (
1339+ & self ,
1340+ import_vis : Option < Visibility > ,
1341+ binding : NameBinding < ' ra > ,
1342+ innermost_binding : NameBinding < ' ra > ,
1343+ ) -> Option < ( Visibility , Visibility ) > {
1344+ match import_vis {
1345+ Some ( import_vis) if binding. res ( ) == innermost_binding. res ( ) => {
1346+ let min =
1347+ |b : NameBinding < ' _ > | b. vis . min ( import_vis. to_def_id ( ) , self . tcx ) . expect_local ( ) ;
1348+ let ( min1, min2) = ( min ( binding) , min ( innermost_binding) ) ;
1349+ ( min1 != min2) . then_some ( ( min1, min2) )
1350+ }
1351+ _ => None ,
1352+ }
1353+ }
1354+
13271355 /// Validate a local resolution (from ribs).
13281356 #[ instrument( level = "debug" , skip( self , all_ribs) ) ]
13291357 fn validate_res_from_ribs (
0 commit comments