Skip to content

Commit df32694

Browse files
committedNov 3, 2022
Remove rustdoc clean::Visibility type
1 parent 432b1a4 commit df32694

File tree

9 files changed

+129
-153
lines changed

9 files changed

+129
-153
lines changed
 

‎src/librustdoc/clean/inline.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ use rustc_span::symbol::{kw, sym, Symbol};
1919
use crate::clean::{
2020
self, clean_fn_decl_from_did_and_sig, clean_generics, clean_impl_item, clean_middle_assoc_item,
2121
clean_middle_field, clean_middle_ty, clean_trait_ref_with_bindings, clean_ty,
22-
clean_ty_generics, clean_variant_def, clean_visibility, utils, Attributes, AttributesExt,
23-
ImplKind, ItemId, Type,
22+
clean_ty_generics, clean_variant_def, utils, Attributes, AttributesExt, ImplKind, ItemId, Type,
2423
};
2524
use crate::core::DocContext;
2625
use crate::formats::item_type::ItemType;
@@ -654,7 +653,7 @@ fn build_macro(
654653
match CStore::from_tcx(cx.tcx).load_macro_untracked(def_id, cx.sess()) {
655654
LoadedMacro::MacroDef(item_def, _) => {
656655
if let ast::ItemKind::MacroDef(ref def) = item_def.kind {
657-
let vis = clean_visibility(cx.tcx.visibility(import_def_id.unwrap_or(def_id)));
656+
let vis = cx.tcx.visibility(import_def_id.unwrap_or(def_id));
658657
clean::MacroItem(clean::Macro {
659658
source: utils::display_macro_source(cx, name, def, def_id, vis),
660659
})

‎src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,13 +1799,6 @@ pub(crate) fn clean_field_with_def_id(
17991799
Item::from_def_id_and_parts(def_id, Some(name), StructFieldItem(ty), cx)
18001800
}
18011801

1802-
pub(crate) fn clean_visibility(vis: ty::Visibility<DefId>) -> Visibility {
1803-
match vis {
1804-
ty::Visibility::Public => Visibility::Public,
1805-
ty::Visibility::Restricted(module) => Visibility::Restricted(module),
1806-
}
1807-
}
1808-
18091802
pub(crate) fn clean_variant_def<'tcx>(variant: &ty::VariantDef, cx: &mut DocContext<'tcx>) -> Item {
18101803
let kind = match variant.ctor_kind {
18111804
CtorKind::Const => Variant::CLike(match variant.discr {
@@ -1962,7 +1955,7 @@ fn clean_maybe_renamed_item<'tcx>(
19621955
clean_fn_or_proc_macro(item, sig, generics, body_id, &mut name, cx)
19631956
}
19641957
ItemKind::Macro(ref macro_def, _) => {
1965-
let ty_vis = clean_visibility(cx.tcx.visibility(def_id));
1958+
let ty_vis = cx.tcx.visibility(def_id);
19661959
MacroItem(Macro {
19671960
source: display_macro_source(cx, name, macro_def, def_id, ty_vis),
19681961
})

‎src/librustdoc/clean/types.rs

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_hir::{BodyId, Mutability};
2424
use rustc_hir_analysis::check::intrinsic::intrinsic_operation_unsafety;
2525
use rustc_index::vec::IndexVec;
2626
use rustc_middle::ty::fast_reject::SimplifiedType;
27-
use rustc_middle::ty::{self, DefIdTree, TyCtxt};
27+
use rustc_middle::ty::{self, DefIdTree, TyCtxt, Visibility};
2828
use rustc_session::Session;
2929
use rustc_span::hygiene::MacroKind;
3030
use rustc_span::source_map::DUMMY_SP;
@@ -34,7 +34,6 @@ use rustc_target::abi::VariantIdx;
3434
use rustc_target::spec::abi::Abi;
3535

3636
use crate::clean::cfg::Cfg;
37-
use crate::clean::clean_visibility;
3837
use crate::clean::external_path;
3938
use crate::clean::inline::{self, print_inlined_const};
4039
use crate::clean::utils::{is_literal_expr, print_const_expr, print_evaluated_const};
@@ -51,7 +50,6 @@ pub(crate) use self::Type::{
5150
Array, BareFunction, BorrowedRef, DynTrait, Generic, ImplTrait, Infer, Primitive, QPath,
5251
RawPointer, Slice, Tuple,
5352
};
54-
pub(crate) use self::Visibility::{Inherited, Public};
5553

5654
#[cfg(test)]
5755
mod tests;
@@ -706,26 +704,28 @@ impl Item {
706704
Some(header)
707705
}
708706

709-
pub(crate) fn visibility(&self, tcx: TyCtxt<'_>) -> Visibility {
707+
/// Returns the visibility of the current item. If the visibility is "inherited", then `None`
708+
/// is returned.
709+
pub(crate) fn visibility(&self, tcx: TyCtxt<'_>) -> Option<Visibility<DefId>> {
710710
let def_id = match self.item_id {
711711
// Anything but DefId *shouldn't* matter, but return a reasonable value anyway.
712-
ItemId::Auto { .. } | ItemId::Blanket { .. } => return Visibility::Inherited,
712+
ItemId::Auto { .. } | ItemId::Blanket { .. } => return None,
713713
// Primitives and Keywords are written in the source code as private modules.
714714
// The modules need to be private so that nobody actually uses them, but the
715715
// keywords and primitives that they are documenting are public.
716-
ItemId::Primitive(..) => return Visibility::Public,
716+
ItemId::Primitive(..) => return Some(Visibility::Public),
717717
ItemId::DefId(def_id) => def_id,
718718
};
719719

720720
match *self.kind {
721721
// Explication on `ItemId::Primitive` just above.
722-
ItemKind::KeywordItem | ItemKind::PrimitiveItem(_) => return Visibility::Public,
722+
ItemKind::KeywordItem | ItemKind::PrimitiveItem(_) => return Some(Visibility::Public),
723723
// Variant fields inherit their enum's visibility.
724724
StructFieldItem(..) if is_field_vis_inherited(tcx, def_id) => {
725-
return Visibility::Inherited;
725+
return None;
726726
}
727727
// Variants always inherit visibility
728-
VariantItem(..) => return Visibility::Inherited,
728+
VariantItem(..) => return None,
729729
// Trait items inherit the trait's visibility
730730
AssocConstItem(..) | TyAssocConstItem(..) | AssocTypeItem(..) | TyAssocTypeItem(..)
731731
| TyMethodItem(..) | MethodItem(..) => {
@@ -739,7 +739,7 @@ impl Item {
739739
}
740740
};
741741
if is_trait_item {
742-
return Visibility::Inherited;
742+
return None;
743743
}
744744
}
745745
_ => {}
@@ -748,7 +748,7 @@ impl Item {
748748
Some(inlined) => inlined,
749749
None => def_id,
750750
};
751-
clean_visibility(tcx.visibility(def_id))
751+
Some(tcx.visibility(def_id))
752752
}
753753
}
754754

@@ -2078,24 +2078,6 @@ impl From<hir::PrimTy> for PrimitiveType {
20782078
}
20792079
}
20802080

2081-
#[derive(Copy, Clone, Debug)]
2082-
pub(crate) enum Visibility {
2083-
/// `pub`
2084-
Public,
2085-
/// Visibility inherited from parent.
2086-
///
2087-
/// For example, this is the visibility of private items and of enum variants.
2088-
Inherited,
2089-
/// `pub(crate)`, `pub(super)`, or `pub(in path::to::somewhere)`
2090-
Restricted(DefId),
2091-
}
2092-
2093-
impl Visibility {
2094-
pub(crate) fn is_public(&self) -> bool {
2095-
matches!(self, Visibility::Public)
2096-
}
2097-
}
2098-
20992081
#[derive(Clone, Debug)]
21002082
pub(crate) struct Struct {
21012083
pub(crate) struct_type: CtorKind,

‎src/librustdoc/clean/utils.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ use crate::clean::render_macro_matchers::render_macro_matcher;
44
use crate::clean::{
55
clean_doc_module, clean_middle_const, clean_middle_region, clean_middle_ty, inline, Crate,
66
ExternalCrate, Generic, GenericArg, GenericArgs, ImportSource, Item, ItemKind, Lifetime, Path,
7-
PathSegment, Primitive, PrimitiveType, Type, TypeBinding, Visibility,
7+
PathSegment, Primitive, PrimitiveType, Type, TypeBinding,
88
};
99
use crate::core::DocContext;
10+
use crate::html::format::visibility_to_src_with_space;
1011

1112
use rustc_ast as ast;
1213
use rustc_ast::tokenstream::TokenTree;
@@ -583,7 +584,7 @@ pub(super) fn display_macro_source(
583584
name: Symbol,
584585
def: &ast::MacroDef,
585586
def_id: DefId,
586-
vis: Visibility,
587+
vis: ty::Visibility<DefId>,
587588
) -> String {
588589
let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
589590
// Extract the spans of all matchers. They represent the "interface" of the macro.
@@ -595,14 +596,14 @@ pub(super) fn display_macro_source(
595596
if matchers.len() <= 1 {
596597
format!(
597598
"{}macro {}{} {{\n ...\n}}",
598-
vis.to_src_with_space(cx.tcx, def_id),
599+
visibility_to_src_with_space(Some(vis), cx.tcx, def_id),
599600
name,
600601
matchers.map(|matcher| render_macro_matcher(cx.tcx, matcher)).collect::<String>(),
601602
)
602603
} else {
603604
format!(
604605
"{}macro {} {{\n{}}}",
605-
vis.to_src_with_space(cx.tcx, def_id),
606+
visibility_to_src_with_space(Some(vis), cx.tcx, def_id),
606607
name,
607608
render_macro_arms(cx.tcx, matchers, ","),
608609
)

‎src/librustdoc/html/format.rs

Lines changed: 74 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,87 +1420,84 @@ impl clean::FnDecl {
14201420
}
14211421
}
14221422

1423-
impl clean::Visibility {
1424-
pub(crate) fn print_with_space<'a, 'tcx: 'a>(
1425-
self,
1426-
item_did: ItemId,
1427-
cx: &'a Context<'tcx>,
1428-
) -> impl fmt::Display + 'a + Captures<'tcx> {
1429-
use std::fmt::Write as _;
1430-
1431-
let to_print: Cow<'static, str> = match self {
1432-
clean::Public => "pub ".into(),
1433-
clean::Inherited => "".into(),
1434-
clean::Visibility::Restricted(vis_did) => {
1435-
// FIXME(camelid): This may not work correctly if `item_did` is a module.
1436-
// However, rustdoc currently never displays a module's
1437-
// visibility, so it shouldn't matter.
1438-
let parent_module = find_nearest_parent_module(cx.tcx(), item_did.expect_def_id());
1439-
1440-
if vis_did.is_crate_root() {
1441-
"pub(crate) ".into()
1442-
} else if parent_module == Some(vis_did) {
1443-
// `pub(in foo)` where `foo` is the parent module
1444-
// is the same as no visibility modifier
1445-
"".into()
1446-
} else if parent_module
1447-
.and_then(|parent| find_nearest_parent_module(cx.tcx(), parent))
1448-
== Some(vis_did)
1449-
{
1450-
"pub(super) ".into()
1451-
} else {
1452-
let path = cx.tcx().def_path(vis_did);
1453-
debug!("path={:?}", path);
1454-
// modified from `resolved_path()` to work with `DefPathData`
1455-
let last_name = path.data.last().unwrap().data.get_opt_name().unwrap();
1456-
let anchor = anchor(vis_did, last_name, cx).to_string();
1457-
1458-
let mut s = "pub(in ".to_owned();
1459-
for seg in &path.data[..path.data.len() - 1] {
1460-
let _ = write!(s, "{}::", seg.data.get_opt_name().unwrap());
1461-
}
1462-
let _ = write!(s, "{}) ", anchor);
1463-
s.into()
1423+
pub(crate) fn visibility_print_with_space<'a, 'tcx: 'a>(
1424+
visibility: Option<ty::Visibility<DefId>>,
1425+
item_did: ItemId,
1426+
cx: &'a Context<'tcx>,
1427+
) -> impl fmt::Display + 'a + Captures<'tcx> {
1428+
use std::fmt::Write as _;
1429+
1430+
let to_print: Cow<'static, str> = match visibility {
1431+
None => "".into(),
1432+
Some(ty::Visibility::Public) => "pub ".into(),
1433+
Some(ty::Visibility::Restricted(vis_did)) => {
1434+
// FIXME(camelid): This may not work correctly if `item_did` is a module.
1435+
// However, rustdoc currently never displays a module's
1436+
// visibility, so it shouldn't matter.
1437+
let parent_module = find_nearest_parent_module(cx.tcx(), item_did.expect_def_id());
1438+
1439+
if vis_did.is_crate_root() {
1440+
"pub(crate) ".into()
1441+
} else if parent_module == Some(vis_did) {
1442+
// `pub(in foo)` where `foo` is the parent module
1443+
// is the same as no visibility modifier
1444+
"".into()
1445+
} else if parent_module.and_then(|parent| find_nearest_parent_module(cx.tcx(), parent))
1446+
== Some(vis_did)
1447+
{
1448+
"pub(super) ".into()
1449+
} else {
1450+
let path = cx.tcx().def_path(vis_did);
1451+
debug!("path={:?}", path);
1452+
// modified from `resolved_path()` to work with `DefPathData`
1453+
let last_name = path.data.last().unwrap().data.get_opt_name().unwrap();
1454+
let anchor = anchor(vis_did, last_name, cx).to_string();
1455+
1456+
let mut s = "pub(in ".to_owned();
1457+
for seg in &path.data[..path.data.len() - 1] {
1458+
let _ = write!(s, "{}::", seg.data.get_opt_name().unwrap());
14641459
}
1460+
let _ = write!(s, "{}) ", anchor);
1461+
s.into()
14651462
}
1466-
};
1467-
display_fn(move |f| write!(f, "{}", to_print))
1468-
}
1463+
}
1464+
};
1465+
display_fn(move |f| write!(f, "{}", to_print))
1466+
}
14691467

1470-
/// This function is the same as print_with_space, except that it renders no links.
1471-
/// It's used for macros' rendered source view, which is syntax highlighted and cannot have
1472-
/// any HTML in it.
1473-
pub(crate) fn to_src_with_space<'a, 'tcx: 'a>(
1474-
self,
1475-
tcx: TyCtxt<'tcx>,
1476-
item_did: DefId,
1477-
) -> impl fmt::Display + 'a + Captures<'tcx> {
1478-
let to_print = match self {
1479-
clean::Public => "pub ".to_owned(),
1480-
clean::Inherited => String::new(),
1481-
clean::Visibility::Restricted(vis_did) => {
1482-
// FIXME(camelid): This may not work correctly if `item_did` is a module.
1483-
// However, rustdoc currently never displays a module's
1484-
// visibility, so it shouldn't matter.
1485-
let parent_module = find_nearest_parent_module(tcx, item_did);
1486-
1487-
if vis_did.is_crate_root() {
1488-
"pub(crate) ".to_owned()
1489-
} else if parent_module == Some(vis_did) {
1490-
// `pub(in foo)` where `foo` is the parent module
1491-
// is the same as no visibility modifier
1492-
String::new()
1493-
} else if parent_module.and_then(|parent| find_nearest_parent_module(tcx, parent))
1494-
== Some(vis_did)
1495-
{
1496-
"pub(super) ".to_owned()
1497-
} else {
1498-
format!("pub(in {}) ", tcx.def_path_str(vis_did))
1499-
}
1468+
/// This function is the same as print_with_space, except that it renders no links.
1469+
/// It's used for macros' rendered source view, which is syntax highlighted and cannot have
1470+
/// any HTML in it.
1471+
pub(crate) fn visibility_to_src_with_space<'a, 'tcx: 'a>(
1472+
visibility: Option<ty::Visibility<DefId>>,
1473+
tcx: TyCtxt<'tcx>,
1474+
item_did: DefId,
1475+
) -> impl fmt::Display + 'a + Captures<'tcx> {
1476+
let to_print = match visibility {
1477+
None => String::new(),
1478+
Some(ty::Visibility::Public) => "pub ".to_owned(),
1479+
Some(ty::Visibility::Restricted(vis_did)) => {
1480+
// FIXME(camelid): This may not work correctly if `item_did` is a module.
1481+
// However, rustdoc currently never displays a module's
1482+
// visibility, so it shouldn't matter.
1483+
let parent_module = find_nearest_parent_module(tcx, item_did);
1484+
1485+
if vis_did.is_crate_root() {
1486+
"pub(crate) ".to_owned()
1487+
} else if parent_module == Some(vis_did) {
1488+
// `pub(in foo)` where `foo` is the parent module
1489+
// is the same as no visibility modifier
1490+
String::new()
1491+
} else if parent_module.and_then(|parent| find_nearest_parent_module(tcx, parent))
1492+
== Some(vis_did)
1493+
{
1494+
"pub(super) ".to_owned()
1495+
} else {
1496+
format!("pub(in {}) ", tcx.def_path_str(vis_did))
15001497
}
1501-
};
1502-
display_fn(move |f| f.write_str(&to_print))
1503-
}
1498+
}
1499+
};
1500+
display_fn(move |f| f.write_str(&to_print))
15041501
}
15051502

15061503
pub(crate) trait PrintWithSpace {

‎src/librustdoc/html/render/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ use crate::formats::{AssocItemRender, Impl, RenderMode};
7070
use crate::html::escape::Escape;
7171
use crate::html::format::{
7272
href, join_with_double_colon, print_abi_with_space, print_constness_with_space,
73-
print_default_space, print_generic_bounds, print_where_clause, Buffer, Ending, HrefError,
74-
PrintWithSpace,
73+
print_default_space, print_generic_bounds, print_where_clause, visibility_print_with_space,
74+
Buffer, Ending, HrefError, PrintWithSpace,
7575
};
7676
use crate::html::highlight;
7777
use crate::html::markdown::{
@@ -752,7 +752,7 @@ fn assoc_const(
752752
w,
753753
"{extra}{vis}const <a{href} class=\"constant\">{name}</a>: {ty}",
754754
extra = extra,
755-
vis = it.visibility(tcx).print_with_space(it.item_id, cx),
755+
vis = visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
756756
href = assoc_href_attr(it, link, cx),
757757
name = it.name.as_ref().unwrap(),
758758
ty = ty.print(cx),
@@ -809,7 +809,7 @@ fn assoc_method(
809809
let tcx = cx.tcx();
810810
let header = meth.fn_header(tcx).expect("Trying to get header from a non-function item");
811811
let name = meth.name.as_ref().unwrap();
812-
let vis = meth.visibility(tcx).print_with_space(meth.item_id, cx).to_string();
812+
let vis = visibility_print_with_space(meth.visibility(tcx), meth.item_id, cx).to_string();
813813
// FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
814814
// this condition.
815815
let constness = match render_mode {

‎src/librustdoc/html/render/print_item.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::def_id::DefId;
77
use rustc_middle::middle::stability;
88
use rustc_middle::span_bug;
99
use rustc_middle::ty::layout::LayoutError;
10-
use rustc_middle::ty::{Adt, TyCtxt};
10+
use rustc_middle::ty::{self, Adt, TyCtxt};
1111
use rustc_span::hygiene::MacroKind;
1212
use rustc_span::symbol::{kw, sym, Symbol};
1313
use rustc_target::abi::{Layout, Primitive, TagEncoding, Variants};
@@ -28,7 +28,7 @@ use crate::formats::{AssocItemRender, Impl, RenderMode};
2828
use crate::html::escape::Escape;
2929
use crate::html::format::{
3030
join_with_double_colon, print_abi_with_space, print_constness_with_space, print_where_clause,
31-
Buffer, Ending, PrintWithSpace,
31+
visibility_print_with_space, Buffer, Ending, PrintWithSpace,
3232
};
3333
use crate::html::highlight;
3434
use crate::html::layout::Page;
@@ -328,14 +328,14 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
328328
Some(src) => write!(
329329
w,
330330
"<div class=\"item-left\"><code>{}extern crate {} as {};",
331-
myitem.visibility(tcx).print_with_space(myitem.item_id, cx),
331+
visibility_print_with_space(myitem.visibility(tcx), myitem.item_id, cx),
332332
anchor(myitem.item_id.expect_def_id(), src, cx),
333333
myitem.name.unwrap(),
334334
),
335335
None => write!(
336336
w,
337337
"<div class=\"item-left\"><code>{}extern crate {};",
338-
myitem.visibility(tcx).print_with_space(myitem.item_id, cx),
338+
visibility_print_with_space(myitem.visibility(tcx), myitem.item_id, cx),
339339
anchor(myitem.item_id.expect_def_id(), myitem.name.unwrap(), cx),
340340
),
341341
}
@@ -385,7 +385,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
385385
</div>\
386386
{stab_tags_before}{stab_tags}{stab_tags_after}",
387387
stab = stab.unwrap_or_default(),
388-
vis = myitem.visibility(tcx).print_with_space(myitem.item_id, cx),
388+
vis = visibility_print_with_space(myitem.visibility(tcx), myitem.item_id, cx),
389389
imp = import.print(cx),
390390
);
391391
w.write_str(ITEM_TABLE_ROW_CLOSE);
@@ -410,7 +410,7 @@ fn item_module(w: &mut Buffer, cx: &mut Context<'_>, item: &clean::Item, items:
410410
let add = if stab.is_some() { " " } else { "" };
411411

412412
let visibility_emoji = match myitem.visibility(tcx) {
413-
clean::Visibility::Restricted(_) => {
413+
Some(ty::Visibility::Restricted(_)) => {
414414
"<span title=\"Restricted Visibility\">&nbsp;🔒</span> "
415415
}
416416
_ => "",
@@ -503,7 +503,7 @@ fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &cle
503503
let unsafety = header.unsafety.print_with_space();
504504
let abi = print_abi_with_space(header.abi).to_string();
505505
let asyncness = header.asyncness.print_with_space();
506-
let visibility = it.visibility(tcx).print_with_space(it.item_id, cx).to_string();
506+
let visibility = visibility_print_with_space(it.visibility(tcx), it.item_id, cx).to_string();
507507
let name = it.name.unwrap();
508508

509509
let generics_len = format!("{:#}", f.generics.print(cx)).len();
@@ -561,7 +561,7 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean:
561561
write!(
562562
w,
563563
"{}{}{}trait {}{}{}",
564-
it.visibility(tcx).print_with_space(it.item_id, cx),
564+
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
565565
t.unsafety(tcx).print_with_space(),
566566
if t.is_auto(tcx) { "auto " } else { "" },
567567
it.name.unwrap(),
@@ -1086,7 +1086,7 @@ fn item_typedef(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clea
10861086
fn write_content(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Typedef) {
10871087
wrap_item(w, "typedef", |w| {
10881088
render_attributes_in_pre(w, it, "");
1089-
write!(w, "{}", it.visibility(cx.tcx()).print_with_space(it.item_id, cx));
1089+
write!(w, "{}", visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx));
10901090
write!(
10911091
w,
10921092
"type {}{}{where_clause} = {type_};",
@@ -1183,7 +1183,7 @@ fn item_enum(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, e: &clean::
11831183
write!(
11841184
w,
11851185
"{}enum {}{}",
1186-
it.visibility(tcx).print_with_space(it.item_id, cx),
1186+
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
11871187
it.name.unwrap(),
11881188
e.generics.print(cx),
11891189
);
@@ -1398,7 +1398,7 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
13981398
write!(
13991399
w,
14001400
"{vis}const {name}: {typ}",
1401-
vis = it.visibility(tcx).print_with_space(it.item_id, cx),
1401+
vis = visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
14021402
name = it.name.unwrap(),
14031403
typ = c.type_.print(cx),
14041404
);
@@ -1499,7 +1499,7 @@ fn item_static(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, s: &clean
14991499
write!(
15001500
w,
15011501
"{vis}static {mutability}{name}: {typ}",
1502-
vis = it.visibility(cx.tcx()).print_with_space(it.item_id, cx),
1502+
vis = visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
15031503
mutability = s.mutability.print_with_space(),
15041504
name = it.name.unwrap(),
15051505
typ = s.type_.print(cx)
@@ -1517,7 +1517,7 @@ fn item_foreign_type(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item) {
15171517
write!(
15181518
w,
15191519
" {}type {};\n}}",
1520-
it.visibility(cx.tcx()).print_with_space(it.item_id, cx),
1520+
visibility_print_with_space(it.visibility(cx.tcx()), it.item_id, cx),
15211521
it.name.unwrap(),
15221522
);
15231523
});
@@ -1671,7 +1671,12 @@ fn render_union(
16711671
cx: &Context<'_>,
16721672
) {
16731673
let tcx = cx.tcx();
1674-
write!(w, "{}union {}", it.visibility(tcx).print_with_space(it.item_id, cx), it.name.unwrap(),);
1674+
write!(
1675+
w,
1676+
"{}union {}",
1677+
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
1678+
it.name.unwrap(),
1679+
);
16751680

16761681
let where_displayed = g
16771682
.map(|g| {
@@ -1698,7 +1703,7 @@ fn render_union(
16981703
write!(
16991704
w,
17001705
" {}{}: {},\n{}",
1701-
field.visibility(tcx).print_with_space(field.item_id, cx),
1706+
visibility_print_with_space(field.visibility(tcx), field.item_id, cx),
17021707
field.name.unwrap(),
17031708
ty.print(cx),
17041709
tab
@@ -1729,7 +1734,7 @@ fn render_struct(
17291734
write!(
17301735
w,
17311736
"{}{}{}",
1732-
it.visibility(tcx).print_with_space(it.item_id, cx),
1737+
visibility_print_with_space(it.visibility(tcx), it.item_id, cx),
17331738
if structhead { "struct " } else { "" },
17341739
it.name.unwrap()
17351740
);
@@ -1759,7 +1764,7 @@ fn render_struct(
17591764
w,
17601765
"\n{} {}{}: {},",
17611766
tab,
1762-
field.visibility(tcx).print_with_space(field.item_id, cx),
1767+
visibility_print_with_space(field.visibility(tcx), field.item_id, cx),
17631768
field.name.unwrap(),
17641769
ty.print(cx),
17651770
);
@@ -1791,7 +1796,7 @@ fn render_struct(
17911796
write!(
17921797
w,
17931798
"{}{}",
1794-
field.visibility(tcx).print_with_space(field.item_id, cx),
1799+
visibility_print_with_space(field.visibility(tcx), field.item_id, cx),
17951800
ty.print(cx),
17961801
)
17971802
}

‎src/librustdoc/json/conversions.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,12 @@ impl JsonRenderer<'_> {
100100
}
101101
}
102102

103-
fn convert_visibility(&self, v: clean::Visibility) -> Visibility {
104-
use clean::Visibility::*;
103+
fn convert_visibility(&self, v: Option<ty::Visibility<DefId>>) -> Visibility {
105104
match v {
106-
Public => Visibility::Public,
107-
Inherited => Visibility::Default,
108-
Restricted(did) if did.is_crate_root() => Visibility::Crate,
109-
Restricted(did) => Visibility::Restricted {
105+
None => Visibility::Default,
106+
Some(ty::Visibility::Public) => Visibility::Public,
107+
Some(ty::Visibility::Restricted(did)) if did.is_crate_root() => Visibility::Crate,
108+
Some(ty::Visibility::Restricted(did)) => Visibility::Restricted {
110109
parent: from_item_id(did.into(), self.tcx),
111110
path: self.tcx.def_path(did).to_string_no_crate_verbose(),
112111
},

‎src/librustdoc/passes/stripper.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! A collection of utility functions for the `strip_*` passes.
22
use rustc_hir::def_id::DefId;
3-
use rustc_middle::ty::TyCtxt;
3+
use rustc_middle::ty::{TyCtxt, Visibility};
44
use rustc_span::symbol::sym;
55
use std::mem;
66

@@ -81,13 +81,13 @@ impl<'a, 'tcx> DocFolder for Stripper<'a, 'tcx> {
8181
}
8282

8383
clean::StructFieldItem(..) => {
84-
if !i.visibility(self.tcx).is_public() {
84+
if i.visibility(self.tcx) != Some(Visibility::Public) {
8585
return Some(strip_item(i));
8686
}
8787
}
8888

8989
clean::ModuleItem(..) => {
90-
if i.item_id.is_local() && !i.visibility(self.tcx).is_public() {
90+
if i.item_id.is_local() && i.visibility(self.tcx) != Some(Visibility::Public) {
9191
debug!("Stripper: stripping module {:?}", i.name);
9292
let old = mem::replace(&mut self.update_retained, false);
9393
let ret = strip_item(self.fold_item_recur(i));
@@ -246,7 +246,7 @@ impl<'tcx> DocFolder for ImportStripper<'tcx> {
246246
fn fold_item(&mut self, i: Item) -> Option<Item> {
247247
match *i.kind {
248248
clean::ExternCrateItem { .. } | clean::ImportItem(..)
249-
if !i.visibility(self.tcx).is_public() =>
249+
if i.visibility(self.tcx) != Some(Visibility::Public) =>
250250
{
251251
None
252252
}

0 commit comments

Comments
 (0)
Please sign in to comment.