Skip to content

Commit 64d6997

Browse files
committed
rustdoc: Cleanup visibility cleaning some more
* Remove outdated comment * Remove duplicated code * Extract helper function
1 parent 94ae60a commit 64d6997

File tree

1 file changed

+31
-52
lines changed

1 file changed

+31
-52
lines changed

Diff for: src/librustdoc/clean/mod.rs

+31-52
Original file line numberDiff line numberDiff line change
@@ -1561,35 +1561,36 @@ impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
15611561
impl Clean<Item> for hir::FieldDef<'_> {
15621562
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
15631563
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)
15801565
}
15811566
}
15821567

15831568
impl Clean<Item> for ty::FieldDef {
15841569
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),
15931594
}
15941595
}
15951596

@@ -1600,8 +1601,7 @@ impl Clean<Visibility> for ty::Visibility {
16001601
// NOTE: this is not quite right: `ty` uses `Invisible` to mean 'private',
16011602
// while rustdoc really does mean inherited. That means that for enum variants, such as
16021603
// `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.
16051605
ty::Visibility::Invisible => Visibility::Inherited,
16061606
ty::Visibility::Restricted(module) => Visibility::Restricted(module),
16071607
}
@@ -1628,39 +1628,18 @@ impl Clean<Item> for ty::VariantDef {
16281628
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
16291629
let kind = match self.ctor_kind {
16301630
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+
}
16441634
CtorKind::Fictive => Variant::Struct(VariantStruct {
16451635
struct_type: CtorKind::Fictive,
16461636
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(),
16591638
}),
16601639
};
16611640
let what_rustc_thinks =
16621641
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
16641643
Item { visibility: Inherited, ..what_rustc_thinks }
16651644
}
16661645
}

0 commit comments

Comments
 (0)