@@ -1561,35 +1561,36 @@ impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
1561
1561
impl Clean < Item > for hir:: FieldDef < ' _ > {
1562
1562
fn clean ( & self , cx : & mut DocContext < ' _ > ) -> Item {
1563
1563
let def_id = cx. tcx . hir ( ) . local_def_id ( self . hir_id ) . to_def_id ( ) ;
1564
- let what_rustc_thinks = Item :: from_def_id_and_parts (
1565
- def_id,
1566
- Some ( self . ident . name ) ,
1567
- StructFieldItem ( self . ty . clean ( cx) ) ,
1568
- cx,
1569
- ) ;
1570
- let parent = cx. tcx . parent ( def_id) . unwrap ( ) ;
1571
- match cx. tcx . def_kind ( parent) {
1572
- DefKind :: Struct | DefKind :: Union => what_rustc_thinks,
1573
- DefKind :: Variant => {
1574
- // Variant fields inherit their enum's visibility.
1575
- Item { visibility : Visibility :: Inherited , ..what_rustc_thinks }
1576
- }
1577
- // FIXME: what about DefKind::Ctor?
1578
- parent_kind => panic ! ( "unexpected parent kind: {:?}" , parent_kind) ,
1579
- }
1564
+ clean_field ( def_id, self . ident . name , self . ty . clean ( cx) , cx)
1580
1565
}
1581
1566
}
1582
1567
1583
1568
impl Clean < Item > for ty:: FieldDef {
1584
1569
fn clean ( & self , cx : & mut DocContext < ' _ > ) -> Item {
1585
- let what_rustc_thinks = Item :: from_def_id_and_parts (
1586
- self . did ,
1587
- Some ( self . ident . name ) ,
1588
- StructFieldItem ( cx. tcx . type_of ( self . did ) . clean ( cx) ) ,
1589
- cx,
1590
- ) ;
1591
- // Don't show `pub` for fields on enum variants; they are always public
1592
- Item { visibility : self . vis . clean ( cx) , ..what_rustc_thinks }
1570
+ clean_field ( self . did , self . ident . name , cx. tcx . type_of ( self . did ) . clean ( cx) , cx)
1571
+ }
1572
+ }
1573
+
1574
+ fn clean_field ( def_id : DefId , name : Symbol , ty : Type , cx : & mut DocContext < ' _ > ) -> Item {
1575
+ let what_rustc_thinks =
1576
+ Item :: from_def_id_and_parts ( def_id, Some ( name) , StructFieldItem ( ty) , cx) ;
1577
+ if is_field_vis_inherited ( cx. tcx , def_id) {
1578
+ // Variant fields inherit their enum's visibility.
1579
+ Item { visibility : Visibility :: Inherited , ..what_rustc_thinks }
1580
+ } else {
1581
+ what_rustc_thinks
1582
+ }
1583
+ }
1584
+
1585
+ fn is_field_vis_inherited ( tcx : TyCtxt < ' _ > , def_id : DefId ) -> bool {
1586
+ let parent = tcx
1587
+ . parent ( def_id)
1588
+ . expect ( "is_field_vis_inherited can only be called on struct or variant fields" ) ;
1589
+ match tcx. def_kind ( parent) {
1590
+ DefKind :: Struct | DefKind :: Union => false ,
1591
+ DefKind :: Variant => true ,
1592
+ // FIXME: what about DefKind::Ctor?
1593
+ parent_kind => panic ! ( "unexpected parent kind: {:?}" , parent_kind) ,
1593
1594
}
1594
1595
}
1595
1596
@@ -1600,8 +1601,7 @@ impl Clean<Visibility> for ty::Visibility {
1600
1601
// NOTE: this is not quite right: `ty` uses `Invisible` to mean 'private',
1601
1602
// while rustdoc really does mean inherited. That means that for enum variants, such as
1602
1603
// `pub enum E { V }`, `V` will be marked as `Public` by `ty`, but as `Inherited` by rustdoc.
1603
- // This is the main reason `impl Clean for hir::Visibility` still exists; various parts of clean
1604
- // override `tcx.visibility` explicitly to make sure this distinction is captured.
1604
+ // Various parts of clean override `tcx.visibility` explicitly to make sure this distinction is captured.
1605
1605
ty:: Visibility :: Invisible => Visibility :: Inherited ,
1606
1606
ty:: Visibility :: Restricted ( module) => Visibility :: Restricted ( module) ,
1607
1607
}
@@ -1628,39 +1628,18 @@ impl Clean<Item> for ty::VariantDef {
1628
1628
fn clean ( & self , cx : & mut DocContext < ' _ > ) -> Item {
1629
1629
let kind = match self . ctor_kind {
1630
1630
CtorKind :: Const => Variant :: CLike ,
1631
- CtorKind :: Fn => Variant :: Tuple (
1632
- self . fields
1633
- . iter ( )
1634
- . map ( |field| {
1635
- let name = Some ( field. ident . name ) ;
1636
- let kind = StructFieldItem ( cx. tcx . type_of ( field. did ) . clean ( cx) ) ;
1637
- let what_rustc_thinks =
1638
- Item :: from_def_id_and_parts ( field. did , name, kind, cx) ;
1639
- // don't show `pub` for fields, which are always public
1640
- Item { visibility : Visibility :: Inherited , ..what_rustc_thinks }
1641
- } )
1642
- . collect ( ) ,
1643
- ) ,
1631
+ CtorKind :: Fn => {
1632
+ Variant :: Tuple ( self . fields . iter ( ) . map ( |field| field. clean ( cx) ) . collect ( ) )
1633
+ }
1644
1634
CtorKind :: Fictive => Variant :: Struct ( VariantStruct {
1645
1635
struct_type : CtorKind :: Fictive ,
1646
1636
fields_stripped : false ,
1647
- fields : self
1648
- . fields
1649
- . iter ( )
1650
- . map ( |field| {
1651
- let name = Some ( field. ident . name ) ;
1652
- let kind = StructFieldItem ( cx. tcx . type_of ( field. did ) . clean ( cx) ) ;
1653
- let what_rustc_thinks =
1654
- Item :: from_def_id_and_parts ( field. did , name, kind, cx) ;
1655
- // don't show `pub` for fields, which are always public
1656
- Item { visibility : Visibility :: Inherited , ..what_rustc_thinks }
1657
- } )
1658
- . collect ( ) ,
1637
+ fields : self . fields . iter ( ) . map ( |field| field. clean ( cx) ) . collect ( ) ,
1659
1638
} ) ,
1660
1639
} ;
1661
1640
let what_rustc_thinks =
1662
1641
Item :: from_def_id_and_parts ( self . def_id , Some ( self . ident . name ) , VariantItem ( kind) , cx) ;
1663
- // don't show `pub` for fields , which are always public
1642
+ // don't show `pub` for variants , which always inherit visibility
1664
1643
Item { visibility : Inherited , ..what_rustc_thinks }
1665
1644
}
1666
1645
}
0 commit comments