@@ -259,11 +259,11 @@ impl fmt::Display for ImplPolarity {
259
259
}
260
260
261
261
#[ derive( Clone , Debug , PartialEq , Eq , Copy , Hash , Encodable , Decodable , HashStable ) ]
262
- pub enum Visibility {
262
+ pub enum Visibility < Id = LocalDefId > {
263
263
/// Visible everywhere (including in other crates).
264
264
Public ,
265
265
/// Visible only in the given crate-local module.
266
- Restricted ( DefId ) ,
266
+ Restricted ( Id ) ,
267
267
}
268
268
269
269
#[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash , HashStable , TyEncodable , TyDecodable ) ]
@@ -354,28 +354,45 @@ impl<'tcx> DefIdTree for TyCtxt<'tcx> {
354
354
}
355
355
}
356
356
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
+ }
367
374
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
+ }
369
382
}
370
383
371
384
/// 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
+ }
377
392
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 ( ) )
379
396
}
380
397
381
398
// Returns `true` if this item is visible anywhere in the local crate.
@@ -385,10 +402,6 @@ impl Visibility {
385
402
Visibility :: Restricted ( def_id) => def_id. is_local ( ) ,
386
403
}
387
404
}
388
-
389
- pub fn is_public ( self ) -> bool {
390
- matches ! ( self , Visibility :: Public )
391
- }
392
405
}
393
406
394
407
/// The crate variances map is computed during typeck and contains the
@@ -1790,7 +1803,7 @@ pub enum VariantDiscr {
1790
1803
pub struct FieldDef {
1791
1804
pub did : DefId ,
1792
1805
pub name : Symbol ,
1793
- pub vis : Visibility ,
1806
+ pub vis : Visibility < DefId > ,
1794
1807
}
1795
1808
1796
1809
impl PartialEq for FieldDef {
0 commit comments