@@ -452,6 +452,8 @@ pub(crate) enum PathSource<'a, 'ast, 'ra> {
452452 DefineOpaques ,
453453 /// Resolving a macro
454454 Macro ,
455+ /// Paths for module or crate root. Used for restrictions.
456+ Module ,
455457}
456458
457459impl PathSource < ' _ , ' _ , ' _ > {
@@ -460,7 +462,8 @@ impl PathSource<'_, '_, '_> {
460462 PathSource :: Type
461463 | PathSource :: Trait ( _)
462464 | PathSource :: Struct ( _)
463- | PathSource :: DefineOpaques => TypeNS ,
465+ | PathSource :: DefineOpaques
466+ | PathSource :: Module => TypeNS ,
464467 PathSource :: Expr ( ..)
465468 | PathSource :: Pat
466469 | PathSource :: TupleStruct ( ..)
@@ -485,7 +488,8 @@ impl PathSource<'_, '_, '_> {
485488 | PathSource :: DefineOpaques
486489 | PathSource :: Delegation
487490 | PathSource :: PreciseCapturingArg ( ..)
488- | PathSource :: Macro => false ,
491+ | PathSource :: Macro
492+ | PathSource :: Module => false ,
489493 }
490494 }
491495
@@ -528,6 +532,7 @@ impl PathSource<'_, '_, '_> {
528532 PathSource :: ReturnTypeNotation | PathSource :: Delegation => "function" ,
529533 PathSource :: PreciseCapturingArg ( ..) => "type or const parameter" ,
530534 PathSource :: Macro => "macro" ,
535+ PathSource :: Module => "module" ,
531536 }
532537 }
533538
@@ -626,6 +631,7 @@ impl PathSource<'_, '_, '_> {
626631 ) ,
627632 PathSource :: PreciseCapturingArg ( MacroNS ) => false ,
628633 PathSource :: Macro => matches ! ( res, Res :: Def ( DefKind :: Macro ( _) , _) ) ,
634+ PathSource :: Module => matches ! ( res, Res :: Def ( DefKind :: Mod , _) ) ,
629635 }
630636 }
631637
@@ -646,6 +652,12 @@ impl PathSource<'_, '_, '_> {
646652 ( PathSource :: PreciseCapturingArg ( ..) , true ) => E0799 ,
647653 ( PathSource :: PreciseCapturingArg ( ..) , false ) => E0800 ,
648654 ( PathSource :: Macro , _) => E0425 ,
655+ // FIXME: There is no dedicated error code for this case yet.
656+ // E0577 already covers the same situation for visibilities,
657+ // so we reuse it here for now. It may make sense to generalize
658+ // it for restrictions in the future.
659+ ( PathSource :: Module , true ) => E0577 ,
660+ ( PathSource :: Module , false ) => E0433 ,
649661 }
650662 }
651663}
@@ -2174,7 +2186,8 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
21742186 | PathSource :: Type
21752187 | PathSource :: PreciseCapturingArg ( ..)
21762188 | PathSource :: ReturnTypeNotation
2177- | PathSource :: Macro => false ,
2189+ | PathSource :: Macro
2190+ | PathSource :: Module => false ,
21782191 PathSource :: Expr ( ..)
21792192 | PathSource :: Pat
21802193 | PathSource :: Struct ( _)
@@ -2800,7 +2813,10 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
28002813 self . diag_metadata . current_impl_items = None ;
28012814 }
28022815
2803- ItemKind :: Trait ( box Trait { generics, bounds, items, .. } ) => {
2816+ ItemKind :: Trait ( box Trait { generics, bounds, items, impl_restriction, .. } ) => {
2817+ // resolve paths for `impl` restrictions
2818+ self . resolve_impl_restriction_path ( impl_restriction) ;
2819+
28042820 // Create a new rib for the trait-wide type parameters.
28052821 self . with_generic_param_rib (
28062822 & generics. params ,
@@ -4389,6 +4405,25 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
43894405 }
43904406 }
43914407
4408+ fn resolve_impl_restriction_path ( & mut self , restriction : & ' ast ast:: ImplRestriction ) {
4409+ match & restriction. kind {
4410+ ast:: RestrictionKind :: Unrestricted => ( ) ,
4411+ ast:: RestrictionKind :: Restricted { path, id, shorthand : _ } => {
4412+ self . smart_resolve_path ( * id, & None , path, PathSource :: Module ) ;
4413+ if let Some ( res) = self . r . partial_res_map [ & id] . full_res ( )
4414+ && let Some ( def_id) = res. opt_def_id ( )
4415+ {
4416+ if !self . r . is_accessible_from (
4417+ Visibility :: Restricted ( def_id) ,
4418+ self . parent_scope . module ,
4419+ ) {
4420+ self . r . dcx ( ) . create_err ( errors:: RestrictionAncestorOnly ( path. span ) ) . emit ( ) ;
4421+ }
4422+ }
4423+ }
4424+ }
4425+ }
4426+
43924427 // High-level and context dependent path resolution routine.
43934428 // Resolves the path and records the resolution into definition map.
43944429 // If resolution fails tries several techniques to find likely
0 commit comments