Skip to content

Commit 49d4232

Browse files
committed
Auto merge of #90844 - camelid:cleanup-vis, r=jyn514
rustdoc: Finish transition to computed visibility This finishes the transition to using computed visibility in rustdoc.
2 parents 3bfde2f + 64d6997 commit 49d4232

File tree

3 files changed

+41
-74
lines changed

3 files changed

+41
-74
lines changed

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

+2-7
Original file line numberDiff line numberDiff line change
@@ -593,14 +593,9 @@ fn build_macro(
593593
match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.sess()) {
594594
LoadedMacro::MacroDef(item_def, _) => {
595595
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
596+
let vis = cx.tcx.visibility(import_def_id.unwrap_or(def_id)).clean(cx);
596597
clean::MacroItem(clean::Macro {
597-
source: utils::display_macro_source(
598-
cx,
599-
name,
600-
def,
601-
def_id,
602-
cx.tcx.visibility(import_def_id.unwrap_or(def_id)),
603-
),
598+
source: utils::display_macro_source(cx, name, def, def_id, vis),
604599
})
605600
} else {
606601
unreachable!()

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

+38-64
Original file line numberDiff line numberDiff line change
@@ -1565,45 +1565,37 @@ impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
15651565

15661566
impl Clean<Item> for hir::FieldDef<'_> {
15671567
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)
15761570
}
15771571
}
15781572

15791573
impl Clean<Item> for ty::FieldDef {
15801574
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)
15891576
}
15901577
}
15911578

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),
16071599
}
16081600
}
16091601

@@ -1614,8 +1606,7 @@ impl Clean<Visibility> for ty::Visibility {
16141606
// NOTE: this is not quite right: `ty` uses `Invisible` to mean 'private',
16151607
// while rustdoc really does mean inherited. That means that for enum variants, such as
16161608
// `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.
16191610
ty::Visibility::Invisible => Visibility::Inherited,
16201611
ty::Visibility::Restricted(module) => Visibility::Restricted(module),
16211612
}
@@ -1642,39 +1633,18 @@ impl Clean<Item> for ty::VariantDef {
16421633
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
16431634
let kind = match self.ctor_kind {
16441635
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+
}
16581639
CtorKind::Fictive => Variant::Struct(VariantStruct {
16591640
struct_type: CtorKind::Fictive,
16601641
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(),
16731643
}),
16741644
};
16751645
let what_rustc_thinks =
16761646
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
16781648
Item { visibility: Inherited, ..what_rustc_thinks }
16791649
}
16801650
}
@@ -1799,9 +1769,12 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
17991769
ItemKind::Fn(ref sig, ref generics, body_id) => {
18001770
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
18011771
}
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+
}
18051778
ItemKind::Trait(is_auto, unsafety, ref generics, bounds, item_ids) => {
18061779
let items = item_ids
18071780
.iter()
@@ -1888,7 +1861,8 @@ fn clean_extern_crate(
18881861
// this is the ID of the crate itself
18891862
let crate_def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
18901863
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()
18921866
&& attrs.iter().any(|a| {
18931867
a.has_name(sym::doc)
18941868
&& match a.meta_item_list() {
@@ -1920,7 +1894,7 @@ fn clean_extern_crate(
19201894
name: Some(name),
19211895
attrs: box attrs.clean(cx),
19221896
def_id: crate_def_id.into(),
1923-
visibility: krate.vis.clean(cx),
1897+
visibility: ty_vis.clean(cx),
19241898
kind: box ExternCrateItem { src: orig_name },
19251899
cfg: attrs.cfg(cx.tcx, &cx.cache.hidden_cfg),
19261900
}]

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

+1-3
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ pub(super) fn display_macro_source(
520520
name: Symbol,
521521
def: &ast::MacroDef,
522522
def_id: DefId,
523-
vis: impl Clean<Visibility>,
523+
vis: Visibility,
524524
) -> String {
525525
let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
526526
// Extract the spans of all matchers. They represent the "interface" of the macro.
@@ -529,8 +529,6 @@ pub(super) fn display_macro_source(
529529
if def.macro_rules {
530530
format!("macro_rules! {} {{\n{}}}", name, render_macro_arms(matchers, ";"))
531531
} else {
532-
let vis = vis.clean(cx);
533-
534532
if matchers.len() <= 1 {
535533
format!(
536534
"{}macro {}{} {{\n ...\n}}",

0 commit comments

Comments
 (0)