@@ -5,7 +5,8 @@ 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:: { bug, span_bug} ;
8+ use rustc_hir:: def_id:: DefId ;
9+ use rustc_middle:: { bug, span_bug, ty} ;
910use rustc_session:: lint:: builtin:: PROC_MACRO_DERIVE_RESOLUTION_FALLBACK ;
1011use rustc_session:: parse:: feature_err;
1112use rustc_span:: hygiene:: { ExpnId , ExpnKind , LocalExpnId , MacroKind , SyntaxContext } ;
@@ -465,13 +466,34 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
465466 Ok ( ( binding, flags) )
466467 if sub_namespace_match ( binding. macro_kinds ( ) , macro_kind) =>
467468 {
469+ if !matches ! ( scope, Scope :: MacroUsePrelude ) {
470+ let adjusted_module = if let Scope :: Module ( module, _) = scope
471+ && !matches ! ( scope_set, ScopeSet :: ModuleAndExternPrelude ( ..) )
472+ {
473+ module
474+ } else {
475+ parent_scope. module
476+ } ;
477+ if !this. is_accessible_from ( binding. vis , adjusted_module) {
478+ this. dcx ( )
479+ . struct_span_err ( binding. span , "binding" )
480+ . with_span_label ( adjusted_module. span , "module" )
481+ . emit ( ) ;
482+ }
483+ }
484+
468485 // Below we report various ambiguity errors.
469486 // We do not need to report them if we are either in speculative resolution,
470487 // or in late resolution when everything is already imported and expanded
471488 // and no ambiguities exist.
472- if matches ! ( finalize, None | Some ( Finalize { stage: Stage :: Late , .. } ) ) {
473- return ControlFlow :: Break ( Ok ( binding) ) ;
474- }
489+ let ( significant_visibility, import_vis) = match finalize {
490+ None | Some ( Finalize { stage : Stage :: Late , .. } ) => {
491+ return ControlFlow :: Break ( Ok ( binding) ) ;
492+ }
493+ Some ( Finalize { significant_visibility, import_vis, .. } ) => {
494+ ( significant_visibility, import_vis)
495+ }
496+ } ;
475497
476498 if let Some ( ( innermost_binding, innermost_flags) ) = innermost_result {
477499 // Found another solution, if the first one was "weak", report an error.
@@ -482,6 +504,8 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
482504 innermost_binding,
483505 flags,
484506 innermost_flags,
507+ significant_visibility,
508+ import_vis,
485509 extern_prelude_item_binding,
486510 extern_prelude_flag_binding,
487511 ) {
@@ -730,11 +754,21 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
730754 innermost_binding : NameBinding < ' ra > ,
731755 flags : Flags ,
732756 innermost_flags : Flags ,
757+ significant_visibility : bool ,
758+ import_vis : Option < ty:: Visibility > ,
733759 extern_prelude_item_binding : Option < NameBinding < ' ra > > ,
734760 extern_prelude_flag_binding : Option < NameBinding < ' ra > > ,
735761 ) -> bool {
736762 let ( res, innermost_res) = ( binding. res ( ) , innermost_binding. res ( ) ) ;
737- if res == innermost_res {
763+
764+ let vis_min = |v1 : ty:: Visibility < DefId > , v2 : ty:: Visibility < DefId > | {
765+ if v1. is_at_least ( v2, self . tcx ) { v2 } else { v1 }
766+ } ;
767+ let bad_vis = significant_visibility
768+ && vis_min ( binding. vis , import_vis. unwrap ( ) . to_def_id ( ) )
769+ != vis_min ( innermost_binding. vis , import_vis. unwrap ( ) . to_def_id ( ) ) ;
770+
771+ if res == innermost_res && !bad_vis {
738772 return false ;
739773 }
740774
@@ -1207,7 +1241,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
12071241 && shadowing == Shadowing :: Restricted
12081242 && finalize. stage == Stage :: Early
12091243 && binding. expansion != LocalExpnId :: ROOT
1210- && binding. res ( ) != shadowed_glob. res ( )
1244+ && ( binding. res ( ) != shadowed_glob. res ( ) || finalize . significant_visibility )
12111245 {
12121246 self . ambiguity_errors . push ( AmbiguityError {
12131247 kind : AmbiguityKind :: GlobVsExpanded ,
0 commit comments