@@ -259,11 +259,11 @@ impl fmt::Display for ImplPolarity {
259259}
260260
261261#[ derive( Clone , Debug , PartialEq , Eq , Copy , Hash , Encodable , Decodable , HashStable ) ]
262- pub enum Visibility {
262+ pub enum Visibility < Id = LocalDefId > {
263263 /// Visible everywhere (including in other crates).
264264 Public ,
265265 /// Visible only in the given crate-local module.
266- Restricted ( DefId ) ,
266+ Restricted ( Id ) ,
267267}
268268
269269#[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash , HashStable , TyEncodable , TyDecodable ) ]
@@ -354,28 +354,45 @@ impl<'tcx> DefIdTree for TyCtxt<'tcx> {
354354 }
355355}
356356
357- impl Visibility {
358- /// Returns `true` if an item with this visibility is accessible from the given block.
359- pub fn is_accessible_from < T : DefIdTree > ( self , module : DefId , tree : T ) -> bool {
360- let restriction = match self {
361- // Public items are visible everywhere.
362- Visibility :: Public => return true ,
363- // Restricted items are visible in an arbitrary local module.
364- Visibility :: Restricted ( other) if other. krate != module. krate => return false ,
365- Visibility :: Restricted ( module) => module,
366- } ;
357+ impl < Id > Visibility < Id > {
358+ pub fn is_public ( self ) -> bool {
359+ matches ! ( self , Visibility :: Public )
360+ }
361+
362+ pub fn map_id < OutId > ( self , f : impl FnOnce ( Id ) -> OutId ) -> Visibility < OutId > {
363+ match self {
364+ Visibility :: Public => Visibility :: Public ,
365+ Visibility :: Restricted ( id) => Visibility :: Restricted ( f ( id) ) ,
366+ }
367+ }
368+ }
369+
370+ impl < Id : Into < DefId > > Visibility < Id > {
371+ pub fn to_def_id ( self ) -> Visibility < DefId > {
372+ self . map_id ( Into :: into)
373+ }
367374
368- tree. is_descendant_of ( module, restriction)
375+ /// Returns `true` if an item with this visibility is accessible from the given module.
376+ pub fn is_accessible_from ( self , module : impl Into < DefId > , tree : impl DefIdTree ) -> bool {
377+ match self {
378+ // Public items are visible everywhere.
379+ Visibility :: Public => true ,
380+ Visibility :: Restricted ( id) => tree. is_descendant_of ( module. into ( ) , id. into ( ) ) ,
381+ }
369382 }
370383
371384 /// Returns `true` if this visibility is at least as accessible as the given visibility
372- pub fn is_at_least < T : DefIdTree > ( self , vis : Visibility , tree : T ) -> bool {
373- let vis_restriction = match vis {
374- Visibility :: Public => return self == Visibility :: Public ,
375- Visibility :: Restricted ( module) => module,
376- } ;
385+ pub fn is_at_least ( self , vis : Visibility < impl Into < DefId > > , tree : impl DefIdTree ) -> bool {
386+ match vis {
387+ Visibility :: Public => self . is_public ( ) ,
388+ Visibility :: Restricted ( id) => self . is_accessible_from ( id, tree) ,
389+ }
390+ }
391+ }
377392
378- self . is_accessible_from ( vis_restriction, tree)
393+ impl Visibility < DefId > {
394+ pub fn expect_local ( self ) -> Visibility {
395+ self . map_id ( |id| id. expect_local ( ) )
379396 }
380397
381398 // Returns `true` if this item is visible anywhere in the local crate.
@@ -385,10 +402,6 @@ impl Visibility {
385402 Visibility :: Restricted ( def_id) => def_id. is_local ( ) ,
386403 }
387404 }
388-
389- pub fn is_public ( self ) -> bool {
390- matches ! ( self , Visibility :: Public )
391- }
392405}
393406
394407/// The crate variances map is computed during typeck and contains the
@@ -1790,7 +1803,7 @@ pub enum VariantDiscr {
17901803pub struct FieldDef {
17911804 pub did : DefId ,
17921805 pub name : Symbol ,
1793- pub vis : Visibility ,
1806+ pub vis : Visibility < DefId > ,
17941807}
17951808
17961809impl PartialEq for FieldDef {
0 commit comments