Skip to content

Commit df32694

Browse files
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

+2-3
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

+1-8
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

+11-29
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

+5-4
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

+74-77
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

+4-4
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 {

0 commit comments

Comments
 (0)