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:
commit
49d42325d0
3 changed files with 41 additions and 74 deletions
|
@ -593,14 +593,9 @@ fn build_macro(
|
||||||
match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.sess()) {
|
match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.sess()) {
|
||||||
LoadedMacro::MacroDef(item_def, _) => {
|
LoadedMacro::MacroDef(item_def, _) => {
|
||||||
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
|
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 {
|
clean::MacroItem(clean::Macro {
|
||||||
source: utils::display_macro_source(
|
source: utils::display_macro_source(cx, name, def, def_id, vis),
|
||||||
cx,
|
|
||||||
name,
|
|
||||||
def,
|
|
||||||
def_id,
|
|
||||||
cx.tcx.visibility(import_def_id.unwrap_or(def_id)),
|
|
||||||
),
|
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
unreachable!()
|
unreachable!()
|
||||||
|
|
|
@ -1565,45 +1565,37 @@ impl<'tcx> Clean<Constant> for ty::Const<'tcx> {
|
||||||
|
|
||||||
impl Clean<Item> for hir::FieldDef<'_> {
|
impl Clean<Item> for hir::FieldDef<'_> {
|
||||||
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
||||||
let what_rustc_thinks = Item::from_hir_id_and_parts(
|
let def_id = cx.tcx.hir().local_def_id(self.hir_id).to_def_id();
|
||||||
self.hir_id,
|
clean_field(def_id, self.ident.name, self.ty.clean(cx), cx)
|
||||||
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 }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clean<Item> for ty::FieldDef {
|
impl Clean<Item> for ty::FieldDef {
|
||||||
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
||||||
let what_rustc_thinks = Item::from_def_id_and_parts(
|
clean_field(self.did, self.ident.name, cx.tcx.type_of(self.did).clean(cx), cx)
|
||||||
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 }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Clean<Visibility> for hir::Visibility<'_> {
|
fn clean_field(def_id: DefId, name: Symbol, ty: Type, cx: &mut DocContext<'_>) -> Item {
|
||||||
fn clean(&self, cx: &mut DocContext<'_>) -> Visibility {
|
let what_rustc_thinks =
|
||||||
match self.node {
|
Item::from_def_id_and_parts(def_id, Some(name), StructFieldItem(ty), cx);
|
||||||
hir::VisibilityKind::Public => Visibility::Public,
|
if is_field_vis_inherited(cx.tcx, def_id) {
|
||||||
hir::VisibilityKind::Inherited => Visibility::Inherited,
|
// Variant fields inherit their enum's visibility.
|
||||||
hir::VisibilityKind::Crate(_) => {
|
Item { visibility: Visibility::Inherited, ..what_rustc_thinks }
|
||||||
let krate = DefId::local(CRATE_DEF_INDEX);
|
} else {
|
||||||
Visibility::Restricted(krate)
|
what_rustc_thinks
|
||||||
}
|
|
||||||
hir::VisibilityKind::Restricted { ref path, .. } => {
|
|
||||||
let path = path.clean(cx);
|
|
||||||
let did = register_res(cx, path.res);
|
|
||||||
Visibility::Restricted(did)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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',
|
// 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
|
// 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.
|
// `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
|
// Various parts of clean override `tcx.visibility` explicitly to make sure this distinction is captured.
|
||||||
// override `tcx.visibility` explicitly to make sure this distinction is captured.
|
|
||||||
ty::Visibility::Invisible => Visibility::Inherited,
|
ty::Visibility::Invisible => Visibility::Inherited,
|
||||||
ty::Visibility::Restricted(module) => Visibility::Restricted(module),
|
ty::Visibility::Restricted(module) => Visibility::Restricted(module),
|
||||||
}
|
}
|
||||||
|
@ -1642,39 +1633,18 @@ impl Clean<Item> for ty::VariantDef {
|
||||||
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
fn clean(&self, cx: &mut DocContext<'_>) -> Item {
|
||||||
let kind = match self.ctor_kind {
|
let kind = match self.ctor_kind {
|
||||||
CtorKind::Const => Variant::CLike,
|
CtorKind::Const => Variant::CLike,
|
||||||
CtorKind::Fn => Variant::Tuple(
|
CtorKind::Fn => {
|
||||||
self.fields
|
Variant::Tuple(self.fields.iter().map(|field| field.clean(cx)).collect())
|
||||||
.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::Fictive => Variant::Struct(VariantStruct {
|
CtorKind::Fictive => Variant::Struct(VariantStruct {
|
||||||
struct_type: CtorKind::Fictive,
|
struct_type: CtorKind::Fictive,
|
||||||
fields_stripped: false,
|
fields_stripped: false,
|
||||||
fields: self
|
fields: self.fields.iter().map(|field| field.clean(cx)).collect(),
|
||||||
.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(),
|
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
let what_rustc_thinks =
|
let what_rustc_thinks =
|
||||||
Item::from_def_id_and_parts(self.def_id, Some(self.ident.name), VariantItem(kind), cx);
|
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 }
|
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) => {
|
ItemKind::Fn(ref sig, ref generics, body_id) => {
|
||||||
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
|
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
|
||||||
}
|
}
|
||||||
ItemKind::Macro(ref macro_def) => MacroItem(Macro {
|
ItemKind::Macro(ref macro_def) => {
|
||||||
source: display_macro_source(cx, name, macro_def, def_id, item.vis),
|
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) => {
|
ItemKind::Trait(is_auto, unsafety, ref generics, bounds, item_ids) => {
|
||||||
let items = item_ids
|
let items = item_ids
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -1888,7 +1861,8 @@ fn clean_extern_crate(
|
||||||
// this is the ID of the crate itself
|
// this is the ID of the crate itself
|
||||||
let crate_def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
|
let crate_def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
|
||||||
let attrs = cx.tcx.hir().attrs(krate.hir_id());
|
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| {
|
&& attrs.iter().any(|a| {
|
||||||
a.has_name(sym::doc)
|
a.has_name(sym::doc)
|
||||||
&& match a.meta_item_list() {
|
&& match a.meta_item_list() {
|
||||||
|
@ -1920,7 +1894,7 @@ fn clean_extern_crate(
|
||||||
name: Some(name),
|
name: Some(name),
|
||||||
attrs: box attrs.clean(cx),
|
attrs: box attrs.clean(cx),
|
||||||
def_id: crate_def_id.into(),
|
def_id: crate_def_id.into(),
|
||||||
visibility: krate.vis.clean(cx),
|
visibility: ty_vis.clean(cx),
|
||||||
kind: box ExternCrateItem { src: orig_name },
|
kind: box ExternCrateItem { src: orig_name },
|
||||||
cfg: attrs.cfg(cx.tcx, &cx.cache.hidden_cfg),
|
cfg: attrs.cfg(cx.tcx, &cx.cache.hidden_cfg),
|
||||||
}]
|
}]
|
||||||
|
|
|
@ -520,7 +520,7 @@ pub(super) fn display_macro_source(
|
||||||
name: Symbol,
|
name: Symbol,
|
||||||
def: &ast::MacroDef,
|
def: &ast::MacroDef,
|
||||||
def_id: DefId,
|
def_id: DefId,
|
||||||
vis: impl Clean<Visibility>,
|
vis: Visibility,
|
||||||
) -> String {
|
) -> String {
|
||||||
let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
|
let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
|
||||||
// Extract the spans of all matchers. They represent the "interface" of the macro.
|
// 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 {
|
if def.macro_rules {
|
||||||
format!("macro_rules! {} {{\n{}}}", name, render_macro_arms(matchers, ";"))
|
format!("macro_rules! {} {{\n{}}}", name, render_macro_arms(matchers, ";"))
|
||||||
} else {
|
} else {
|
||||||
let vis = vis.clean(cx);
|
|
||||||
|
|
||||||
if matchers.len() <= 1 {
|
if matchers.len() <= 1 {
|
||||||
format!(
|
format!(
|
||||||
"{}macro {}{} {{\n ...\n}}",
|
"{}macro {}{} {{\n ...\n}}",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue