@@ -1565,45 +1565,37 @@ impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
1565
1565
1566
1566
impl Clean < Item > for hir:: FieldDef < ' _ > {
1567
1567
fn clean ( & self , cx : & mut DocContext < ' _ > ) -> Item {
1568
- let what_rustc_thinks = Item :: from_hir_id_and_parts (
1569
- self . hir_id ,
1570
- Some ( self . ident . name ) ,
1571
- StructFieldItem ( self . ty . clean ( cx) ) ,
1572
- cx,
1573
- ) ;
1574
- // Don't show `pub` for fields on enum variants; they are always public
1575
- Item { visibility : self . vis . clean ( cx) , ..what_rustc_thinks }
1568
+ let def_id = cx. tcx . hir ( ) . local_def_id ( self . hir_id ) . to_def_id ( ) ;
1569
+ clean_field ( def_id, self . ident . name , self . ty . clean ( cx) , cx)
1576
1570
}
1577
1571
}
1578
1572
1579
1573
impl Clean < Item > for ty:: FieldDef {
1580
1574
fn clean ( & self , cx : & mut DocContext < ' _ > ) -> Item {
1581
- let what_rustc_thinks = Item :: from_def_id_and_parts (
1582
- self . did ,
1583
- Some ( self . ident . name ) ,
1584
- StructFieldItem ( cx. tcx . type_of ( self . did ) . clean ( cx) ) ,
1585
- cx,
1586
- ) ;
1587
- // Don't show `pub` for fields on enum variants; they are always public
1588
- Item { visibility : self . vis . clean ( cx) , ..what_rustc_thinks }
1575
+ clean_field ( self . did , self . ident . name , cx. tcx . type_of ( self . did ) . clean ( cx) , cx)
1589
1576
}
1590
1577
}
1591
1578
1592
- impl Clean < Visibility > for hir:: Visibility < ' _ > {
1593
- fn clean ( & self , cx : & mut DocContext < ' _ > ) -> Visibility {
1594
- match self . node {
1595
- hir:: VisibilityKind :: Public => Visibility :: Public ,
1596
- hir:: VisibilityKind :: Inherited => Visibility :: Inherited ,
1597
- hir:: VisibilityKind :: Crate ( _) => {
1598
- let krate = DefId :: local ( CRATE_DEF_INDEX ) ;
1599
- Visibility :: Restricted ( krate)
1600
- }
1601
- hir:: VisibilityKind :: Restricted { ref path, .. } => {
1602
- let path = path. clean ( cx) ;
1603
- let did = register_res ( cx, path. res ) ;
1604
- Visibility :: Restricted ( did)
1605
- }
1606
- }
1579
+ fn clean_field ( def_id : DefId , name : Symbol , ty : Type , cx : & mut DocContext < ' _ > ) -> Item {
1580
+ let what_rustc_thinks =
1581
+ Item :: from_def_id_and_parts ( def_id, Some ( name) , StructFieldItem ( ty) , cx) ;
1582
+ if is_field_vis_inherited ( cx. tcx , def_id) {
1583
+ // Variant fields inherit their enum's visibility.
1584
+ Item { visibility : Visibility :: Inherited , ..what_rustc_thinks }
1585
+ } else {
1586
+ what_rustc_thinks
1587
+ }
1588
+ }
1589
+
1590
+ fn is_field_vis_inherited ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> bool {
1591
+ let parent = tcx
1592
+ . parent ( def_id)
1593
+ . expect ( "is_field_vis_inherited can only be called on struct or variant fields" ) ;
1594
+ match tcx. def_kind ( parent) {
1595
+ DefKind :: Struct | DefKind :: Union => false ,
1596
+ DefKind :: Variant => true ,
1597
+ // FIXME: what about DefKind::Ctor?
1598
+ parent_kind => panic ! ( "unexpected parent kind: {:?}" , parent_kind) ,
1607
1599
}
1608
1600
}
1609
1601
@@ -1614,8 +1606,7 @@ impl Clean<Visibility> for ty::Visibility {
1614
1606
// NOTE: this is not quite right: `ty` uses `Invisible` to mean 'private',
1615
1607
// while rustdoc really does mean inherited. That means that for enum variants, such as
1616
1608
// `pub enum E { V }`, `V` will be marked as `Public` by `ty`, but as `Inherited` by rustdoc.
1617
- // This is the main reason `impl Clean for hir::Visibility` still exists; various parts of clean
1618
- // override `tcx.visibility` explicitly to make sure this distinction is captured.
1609
+ // Various parts of clean override `tcx.visibility` explicitly to make sure this distinction is captured.
1619
1610
ty:: Visibility :: Invisible => Visibility :: Inherited ,
1620
1611
ty:: Visibility :: Restricted ( module) => Visibility :: Restricted ( module) ,
1621
1612
}
@@ -1642,39 +1633,18 @@ impl Clean<Item> for ty::VariantDef {
1642
1633
fn clean ( & self , cx : & mut DocContext < ' _ > ) -> Item {
1643
1634
let kind = match self . ctor_kind {
1644
1635
CtorKind :: Const => Variant :: CLike ,
1645
- CtorKind :: Fn => Variant :: Tuple (
1646
- self . fields
1647
- . iter ( )
1648
- . map ( |field| {
1649
- let name = Some ( field. ident . name ) ;
1650
- let kind = StructFieldItem ( cx. tcx . type_of ( field. did ) . clean ( cx) ) ;
1651
- let what_rustc_thinks =
1652
- Item :: from_def_id_and_parts ( field. did , name, kind, cx) ;
1653
- // don't show `pub` for fields, which are always public
1654
- Item { visibility : Visibility :: Inherited , ..what_rustc_thinks }
1655
- } )
1656
- . collect ( ) ,
1657
- ) ,
1636
+ CtorKind :: Fn => {
1637
+ Variant :: Tuple ( self . fields . iter ( ) . map ( |field| field. clean ( cx) ) . collect ( ) )
1638
+ }
1658
1639
CtorKind :: Fictive => Variant :: Struct ( VariantStruct {
1659
1640
struct_type : CtorKind :: Fictive ,
1660
1641
fields_stripped : false ,
1661
- fields : self
1662
- . fields
1663
- . iter ( )
1664
- . map ( |field| {
1665
- let name = Some ( field. ident . name ) ;
1666
- let kind = StructFieldItem ( cx. tcx . type_of ( field. did ) . clean ( cx) ) ;
1667
- let what_rustc_thinks =
1668
- Item :: from_def_id_and_parts ( field. did , name, kind, cx) ;
1669
- // don't show `pub` for fields, which are always public
1670
- Item { visibility : Visibility :: Inherited , ..what_rustc_thinks }
1671
- } )
1672
- . collect ( ) ,
1642
+ fields : self . fields . iter ( ) . map ( |field| field. clean ( cx) ) . collect ( ) ,
1673
1643
} ) ,
1674
1644
} ;
1675
1645
let what_rustc_thinks =
1676
1646
Item :: from_def_id_and_parts ( self . def_id , Some ( self . ident . name ) , VariantItem ( kind) , cx) ;
1677
- // don't show `pub` for fields , which are always public
1647
+ // don't show `pub` for variants , which always inherit visibility
1678
1648
Item { visibility : Inherited , ..what_rustc_thinks }
1679
1649
}
1680
1650
}
@@ -1799,9 +1769,12 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
1799
1769
ItemKind :: Fn ( ref sig, ref generics, body_id) => {
1800
1770
clean_fn_or_proc_macro ( item, sig, generics, body_id, & mut name, cx)
1801
1771
}
1802
- ItemKind :: Macro ( ref macro_def) => MacroItem ( Macro {
1803
- source : display_macro_source ( cx, name, macro_def, def_id, item. vis ) ,
1804
- } ) ,
1772
+ ItemKind :: Macro ( ref macro_def) => {
1773
+ let ty_vis = cx. tcx . visibility ( def_id) . clean ( cx) ;
1774
+ MacroItem ( Macro {
1775
+ source : display_macro_source ( cx, name, macro_def, def_id, ty_vis) ,
1776
+ } )
1777
+ }
1805
1778
ItemKind :: Trait ( is_auto, unsafety, ref generics, bounds, item_ids) => {
1806
1779
let items = item_ids
1807
1780
. iter ( )
@@ -1888,7 +1861,8 @@ fn clean_extern_crate(
1888
1861
// this is the ID of the crate itself
1889
1862
let crate_def_id = DefId { krate : cnum, index : CRATE_DEF_INDEX } ;
1890
1863
let attrs = cx. tcx . hir ( ) . attrs ( krate. hir_id ( ) ) ;
1891
- let please_inline = cx. tcx . visibility ( krate. def_id ) . is_public ( )
1864
+ let ty_vis = cx. tcx . visibility ( krate. def_id ) ;
1865
+ let please_inline = ty_vis. is_public ( )
1892
1866
&& attrs. iter ( ) . any ( |a| {
1893
1867
a. has_name ( sym:: doc)
1894
1868
&& match a. meta_item_list ( ) {
@@ -1920,7 +1894,7 @@ fn clean_extern_crate(
1920
1894
name: Some ( name) ,
1921
1895
attrs: box attrs. clean( cx) ,
1922
1896
def_id: crate_def_id. into( ) ,
1923
- visibility: krate . vis . clean( cx) ,
1897
+ visibility: ty_vis . clean( cx) ,
1924
1898
kind: box ExternCrateItem { src: orig_name } ,
1925
1899
cfg: attrs. cfg( cx. tcx, & cx. cache. hidden_cfg) ,
1926
1900
} ]
0 commit comments