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.
This commit is contained in:
bors 2021-11-21 13:26:31 +00:00
commit 49d42325d0
3 changed files with 41 additions and 74 deletions

View File

@ -593,14 +593,9 @@ fn build_macro(
match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.sess()) {
LoadedMacro::MacroDef(item_def, _) => {
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
let vis = cx.tcx.visibility(import_def_id.unwrap_or(def_id)).clean(cx);
clean::MacroItem(clean::Macro {
source: utils::display_macro_source(
cx,
name,
def,
def_id,
cx.tcx.visibility(import_def_id.unwrap_or(def_id)),
),
source: utils::display_macro_source(cx, name, def, def_id, vis),
})
} else {
unreachable!()

View File

@ -1565,45 +1565,37 @@ impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
impl Clean<Item> for hir::FieldDef<'_> {
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
let what_rustc_thinks = Item::from_hir_id_and_parts(
self.hir_id,
Some(self.ident.name),
StructFieldItem(self.ty.clean(cx)),
cx,
);
// Don't show `pub` for fields on enum variants; they are always public
Item { visibility: self.vis.clean(cx), ..what_rustc_thinks }
let def_id = cx.tcx.hir().local_def_id(self.hir_id).to_def_id();
clean_field(def_id, self.ident.name, self.ty.clean(cx), cx)
}
}
impl Clean<Item> for ty::FieldDef {
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
let what_rustc_thinks = Item::from_def_id_and_parts(
self.did,
Some(self.ident.name),
StructFieldItem(cx.tcx.type_of(self.did).clean(cx)),
cx,
);
// Don't show `pub` for fields on enum variants; they are always public
Item { visibility: self.vis.clean(cx), ..what_rustc_thinks }
clean_field(self.did, self.ident.name, cx.tcx.type_of(self.did).clean(cx), cx)
}
}
impl Clean<Visibility> for hir::Visibility<'_> {
fn clean(&self, cx: &mut DocContext<'_>) -> Visibility {
match self.node {
hir::VisibilityKind::Public => Visibility::Public,
hir::VisibilityKind::Inherited => Visibility::Inherited,
hir::VisibilityKind::Crate(_) => {
let krate = DefId::local(CRATE_DEF_INDEX);
Visibility::Restricted(krate)
}
hir::VisibilityKind::Restricted { ref path, .. } => {
let path = path.clean(cx);
let did = register_res(cx, path.res);
Visibility::Restricted(did)
}
}
fn clean_field(def_id: DefId, name: Symbol, ty: Type, cx: &mut DocContext<'_>) -> Item {
let what_rustc_thinks =
Item::from_def_id_and_parts(def_id, Some(name), StructFieldItem(ty), cx);
if is_field_vis_inherited(cx.tcx, def_id) {
// Variant fields inherit their enum's visibility.
Item { visibility: Visibility::Inherited, ..what_rustc_thinks }
} else {
what_rustc_thinks
}
}
fn is_field_vis_inherited(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
let parent = tcx
.parent(def_id)
.expect("is_field_vis_inherited can only be called on struct or variant fields");
match tcx.def_kind(parent) {
DefKind::Struct | DefKind::Union => false,
DefKind::Variant => true,
// FIXME: what about DefKind::Ctor?
parent_kind => panic!("unexpected parent kind: {:?}", parent_kind),
}
}
@ -1614,8 +1606,7 @@ impl Clean<Visibility> for ty::Visibility {
// NOTE: this is not quite right: `ty` uses `Invisible` to mean 'private',
// while rustdoc really does mean inherited. That means that for enum variants, such as
// `pub enum E { V }`, `V` will be marked as `Public` by `ty`, but as `Inherited` by rustdoc.
// This is the main reason `impl Clean for hir::Visibility` still exists; various parts of clean
// override `tcx.visibility` explicitly to make sure this distinction is captured.
// Various parts of clean override `tcx.visibility` explicitly to make sure this distinction is captured.
ty::Visibility::Invisible => Visibility::Inherited,
ty::Visibility::Restricted(module) => Visibility::Restricted(module),
}
@ -1642,39 +1633,18 @@ impl Clean<Item> for ty::VariantDef {
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
let kind = match self.ctor_kind {
CtorKind::Const => Variant::CLike,
CtorKind::Fn => Variant::Tuple(
self.fields
.iter()
.map(|field| {
let name = Some(field.ident.name);
let kind = StructFieldItem(cx.tcx.type_of(field.did).clean(cx));
let what_rustc_thinks =
Item::from_def_id_and_parts(field.did, name, kind, cx);
// don't show `pub` for fields, which are always public
Item { visibility: Visibility::Inherited, ..what_rustc_thinks }
})
.collect(),
),
CtorKind::Fn => {
Variant::Tuple(self.fields.iter().map(|field| field.clean(cx)).collect())
}
CtorKind::Fictive => Variant::Struct(VariantStruct {
struct_type: CtorKind::Fictive,
fields_stripped: false,
fields: self
.fields
.iter()
.map(|field| {
let name = Some(field.ident.name);
let kind = StructFieldItem(cx.tcx.type_of(field.did).clean(cx));
let what_rustc_thinks =
Item::from_def_id_and_parts(field.did, name, kind, cx);
// don't show `pub` for fields, which are always public
Item { visibility: Visibility::Inherited, ..what_rustc_thinks }
})
.collect(),
fields: self.fields.iter().map(|field| field.clean(cx)).collect(),
}),
};
let what_rustc_thinks =
Item::from_def_id_and_parts(self.def_id, Some(self.ident.name), VariantItem(kind), cx);
// don't show `pub` for fields, which are always public
// don't show `pub` for variants, which always inherit visibility
Item { visibility: Inherited, ..what_rustc_thinks }
}
}
@ -1799,9 +1769,12 @@ impl Clean<Vec<Item>> for (&hir::Item<'_>, Option<Symbol>) {
ItemKind::Fn(ref sig, ref generics, body_id) => {
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
}
ItemKind::Macro(ref macro_def) => MacroItem(Macro {
source: display_macro_source(cx, name, macro_def, def_id, item.vis),
}),
ItemKind::Macro(ref macro_def) => {
let ty_vis = cx.tcx.visibility(def_id).clean(cx);
MacroItem(Macro {
source: display_macro_source(cx, name, macro_def, def_id, ty_vis),
})
}
ItemKind::Trait(is_auto, unsafety, ref generics, bounds, item_ids) => {
let items = item_ids
.iter()
@ -1888,7 +1861,8 @@ fn clean_extern_crate(
// this is the ID of the crate itself
let crate_def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
let attrs = cx.tcx.hir().attrs(krate.hir_id());
let please_inline = cx.tcx.visibility(krate.def_id).is_public()
let ty_vis = cx.tcx.visibility(krate.def_id);
let please_inline = ty_vis.is_public()
&& attrs.iter().any(|a| {
a.has_name(sym::doc)
&& match a.meta_item_list() {
@ -1920,7 +1894,7 @@ fn clean_extern_crate(
name: Some(name),
attrs: box attrs.clean(cx),
def_id: crate_def_id.into(),
visibility: krate.vis.clean(cx),
visibility: ty_vis.clean(cx),
kind: box ExternCrateItem { src: orig_name },
cfg: attrs.cfg(cx.tcx, &cx.cache.hidden_cfg),
}]

View File

@ -520,7 +520,7 @@ pub(super) fn display_macro_source(
name: Symbol,
def: &ast::MacroDef,
def_id: DefId,
vis: impl Clean<Visibility>,
vis: Visibility,
) -> String {
let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
// Extract the spans of all matchers. They represent the "interface" of the macro.
@ -529,8 +529,6 @@ pub(super) fn display_macro_source(
if def.macro_rules {
format!("macro_rules! {} {{\n{}}}", name, render_macro_arms(matchers, ";"))
} else {
let vis = vis.clean(cx);
if matchers.len() <= 1 {
format!(
"{}macro {}{} {{\n ...\n}}",