@@ -263,11 +263,11 @@ impl fmt::Display for ImplPolarity {
263
263
}
264
264
265
265
#[ derive( Clone , Debug , PartialEq , Eq , Copy , Hash , Encodable , Decodable , HashStable ) ]
266
- pub enum Visibility {
266
+ pub enum Visibility < Id = LocalDefId > {
267
267
/// Visible everywhere (including in other crates).
268
268
Public ,
269
269
/// Visible only in the given crate-local module.
270
- Restricted ( DefId ) ,
270
+ Restricted ( Id ) ,
271
271
}
272
272
273
273
#[ derive( Copy , Clone , Debug , PartialEq , Eq , Hash , HashStable , TyEncodable , TyDecodable ) ]
@@ -358,28 +358,45 @@ impl<'tcx> DefIdTree for TyCtxt<'tcx> {
358
358
}
359
359
}
360
360
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
+ }
371
378
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
+ }
373
386
}
374
387
375
388
/// 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
+ }
381
396
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 ( ) )
383
400
}
384
401
385
402
// Returns `true` if this item is visible anywhere in the local crate.
@@ -389,10 +406,6 @@ impl Visibility {
389
406
Visibility :: Restricted ( def_id) => def_id. is_local ( ) ,
390
407
}
391
408
}
392
-
393
- pub fn is_public ( self ) -> bool {
394
- matches ! ( self , Visibility :: Public )
395
- }
396
409
}
397
410
398
411
/// The crate variances map is computed during typeck and contains the
@@ -1861,7 +1874,7 @@ pub enum VariantDiscr {
1861
1874
pub struct FieldDef {
1862
1875
pub did : DefId ,
1863
1876
pub name : Symbol ,
1864
- pub vis : Visibility ,
1877
+ pub vis : Visibility < DefId > ,
1865
1878
}
1866
1879
1867
1880
impl PartialEq for FieldDef {
0 commit comments