@@ -263,11 +263,11 @@ impl fmt::Display for ImplPolarity {
263263}
264264
265265#[ derive( Clone , Debug , PartialEq , Eq , Copy , Hash , Encodable , Decodable , HashStable ) ]
266- pub enum Visibility {
266+ pub enum Visibility < Id = LocalDefId > {
267267 /// Visible everywhere (including in other crates).
268268 Public ,
269269 /// Visible only in the given crate-local module.
270- Restricted ( DefId ) ,
270+ Restricted ( Id ) ,
271271}
272272
273273#[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash , HashStable , TyEncodable , TyDecodable ) ]
@@ -358,28 +358,45 @@ impl<'tcx> DefIdTree for TyCtxt<'tcx> {
358358 }
359359}
360360
361- impl Visibility {
362- /// Returns `true` if an item with this visibility is accessible from the given block.
363- pub fn is_accessible_from < T : DefIdTree > ( self , module : DefId , tree : T ) -> bool {
364- let restriction = match self {
365- // Public items are visible everywhere.
366- Visibility :: Public => return true ,
367- // Restricted items are visible in an arbitrary local module.
368- Visibility :: Restricted ( other) if other. krate != module. krate => return false ,
369- Visibility :: Restricted ( module) => module,
370- } ;
361+ impl < Id > Visibility < Id > {
362+ pub fn is_public ( self ) -> bool {
363+ matches ! ( self , Visibility :: Public )
364+ }
365+
366+ pub fn map_id < OutId > ( self , f : impl FnOnce ( Id ) -> OutId ) -> Visibility < OutId > {
367+ match self {
368+ Visibility :: Public => Visibility :: Public ,
369+ Visibility :: Restricted ( id) => Visibility :: Restricted ( f ( id) ) ,
370+ }
371+ }
372+ }
373+
374+ impl < Id : Into < DefId > > Visibility < Id > {
375+ pub fn to_def_id ( self ) -> Visibility < DefId > {
376+ self . map_id ( Into :: into)
377+ }
371378
372- tree. is_descendant_of ( module, restriction)
379+ /// Returns `true` if an item with this visibility is accessible from the given module.
380+ pub fn is_accessible_from ( self , module : impl Into < DefId > , tree : impl DefIdTree ) -> bool {
381+ match self {
382+ // Public items are visible everywhere.
383+ Visibility :: Public => true ,
384+ Visibility :: Restricted ( id) => tree. is_descendant_of ( module. into ( ) , id. into ( ) ) ,
385+ }
373386 }
374387
375388 /// Returns `true` if this visibility is at least as accessible as the given visibility
376- pub fn is_at_least < T : DefIdTree > ( self , vis : Visibility , tree : T ) -> bool {
377- let vis_restriction = match vis {
378- Visibility :: Public => return self == Visibility :: Public ,
379- Visibility :: Restricted ( module) => module,
380- } ;
389+ pub fn is_at_least ( self , vis : Visibility < impl Into < DefId > > , tree : impl DefIdTree ) -> bool {
390+ match vis {
391+ Visibility :: Public => self . is_public ( ) ,
392+ Visibility :: Restricted ( id) => self . is_accessible_from ( id, tree) ,
393+ }
394+ }
395+ }
381396
382- self . is_accessible_from ( vis_restriction, tree)
397+ impl Visibility < DefId > {
398+ pub fn expect_local ( self ) -> Visibility {
399+ self . map_id ( |id| id. expect_local ( ) )
383400 }
384401
385402 // Returns `true` if this item is visible anywhere in the local crate.
@@ -389,10 +406,6 @@ impl Visibility {
389406 Visibility :: Restricted ( def_id) => def_id. is_local ( ) ,
390407 }
391408 }
392-
393- pub fn is_public ( self ) -> bool {
394- matches ! ( self , Visibility :: Public )
395- }
396409}
397410
398411/// The crate variances map is computed during typeck and contains the
@@ -1861,7 +1874,7 @@ pub enum VariantDiscr {
18611874pub struct FieldDef {
18621875 pub did : DefId ,
18631876 pub name : Symbol ,
1864- pub vis : Visibility ,
1877+ pub vis : Visibility < DefId > ,
18651878}
18661879
18671880impl PartialEq for FieldDef {
0 commit comments