Skip to content

Commit 2d37f38

Browse files
committed
Auto merge of rust-lang#95024 - koehlma:rustdoc-private-items, r=GuillaumeGomez,camelid,jsha
rustdoc: add 🔒 to items with restricted visibility This change marks items with restricted visibility with 🔒 when building with `--document-private-items`: <img width="278" alt="Screen Shot 2022-03-20 at 23 50 24" src="https://user-images.githubusercontent.com/509209/159189513-9e4b09bb-6785-41a5-bfe2-df02f83f8641.png"> There also appears a “Restricted Visibility” tooltip when hovering over the emoji. --- The original PR for reference: This change makes private items slightly transparent (similar to `unstable` items in rustc): <img width="272" alt="Screen Shot 2022-03-16 at 22 17 43" src="https://user-images.githubusercontent.com/509209/158692627-a1f6f5ec-e043-4aa2-9352-8d2b15c31c08.png"> I found myself using `--document-private-items` a lot recently because I find the documentation of private internals quite helpful when working on a larger project. However, not being able to distinguish private from public items (see rust-lang#87785) when looking at the documentation makes this somewhat cumbersome. This PR addresses the third suggestion of issue rust-lang#87785 by marking private items typographically. It seems to me that the other suggestions are more involved but this is at least a first step. A private item is also made slightly transparent in the path displayed in the header of a page: <img width="467" alt="Screen Shot 2022-03-16 at 22 19 51" src="https://user-images.githubusercontent.com/509209/158692885-0bbd3417-3c0b-486f-b8ab-99c05c6fa7ca.png"> I am looking forward to feedback and suggestions.
2 parents 0e4524e + 1c523ba commit 2d37f38

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

Diff for: src/librustdoc/html/render/print_item.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -376,17 +376,26 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
376376
let stab = myitem.stability_class(cx.tcx());
377377
let add = if stab.is_some() { " " } else { "" };
378378

379+
let visibility_emoji = match myitem.visibility {
380+
clean::Visibility::Restricted(_) => {
381+
"<span title=\"Restricted Visibility\">&nbsp;🔒</span> "
382+
}
383+
_ => "",
384+
};
385+
379386
let doc_value = myitem.doc_value().unwrap_or_default();
380387
w.write_str(ITEM_TABLE_ROW_OPEN);
381388
write!(
382389
w,
383390
"<div class=\"item-left {stab}{add}module-item\">\
384-
<a class=\"{class}\" href=\"{href}\" title=\"{title}\">{name}</a>\
385-
{unsafety_flag}\
386-
{stab_tags}\
391+
<a class=\"{class}\" href=\"{href}\" title=\"{title}\">{name}</a>\
392+
{visibility_emoji}\
393+
{unsafety_flag}\
394+
{stab_tags}\
387395
</div>\
388396
<div class=\"item-right docblock-short\">{docs}</div>",
389397
name = myitem.name.unwrap(),
398+
visibility_emoji = visibility_emoji,
390399
stab_tags = extra_info_tags(myitem, item, cx.tcx()),
391400
docs = MarkdownSummaryLine(&doc_value, &myitem.links(cx)).into_string(),
392401
class = myitem.type_(),

Diff for: src/test/rustdoc/visibility.rs

+31
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,68 @@
44

55
#![crate_name = "foo"]
66

7+
// @!has 'foo/index.html' '//a[@href="struct.FooPublic.html"]/..' 'FooPublic 🔒'
78
// @has 'foo/struct.FooPublic.html' '//pre' 'pub struct FooPublic'
89
pub struct FooPublic;
10+
// @has 'foo/index.html' '//a[@href="struct.FooJustCrate.html"]/..' 'FooJustCrate 🔒'
911
// @has 'foo/struct.FooJustCrate.html' '//pre' 'pub(crate) struct FooJustCrate'
1012
crate struct FooJustCrate;
13+
// @has 'foo/index.html' '//a[@href="struct.FooPubCrate.html"]/..' 'FooPubCrate 🔒'
1114
// @has 'foo/struct.FooPubCrate.html' '//pre' 'pub(crate) struct FooPubCrate'
1215
pub(crate) struct FooPubCrate;
16+
// @has 'foo/index.html' '//a[@href="struct.FooSelf.html"]/..' 'FooSelf 🔒'
1317
// @has 'foo/struct.FooSelf.html' '//pre' 'pub(crate) struct FooSelf'
1418
pub(self) struct FooSelf;
19+
// @has 'foo/index.html' '//a[@href="struct.FooInSelf.html"]/..' 'FooInSelf 🔒'
1520
// @has 'foo/struct.FooInSelf.html' '//pre' 'pub(crate) struct FooInSelf'
1621
pub(in self) struct FooInSelf;
22+
// @has 'foo/index.html' '//a[@href="struct.FooPriv.html"]/..' 'FooPriv 🔒'
1723
// @has 'foo/struct.FooPriv.html' '//pre' 'pub(crate) struct FooPriv'
1824
struct FooPriv;
1925

26+
// @!has 'foo/index.html' '//a[@href="pub_mod/index.html"]/..' 'pub_mod 🔒'
27+
pub mod pub_mod {}
28+
29+
// @has 'foo/index.html' '//a[@href="pub_crate_mod/index.html"]/..' 'pub_crate_mod 🔒'
30+
pub(crate) mod pub_crate_mod {}
31+
32+
// @has 'foo/index.html' '//a[@href="a/index.html"]/..' 'a 🔒'
2033
mod a {
34+
// @has 'foo/a/index.html' '//a[@href="struct.FooASuper.html"]/..' 'FooASuper 🔒'
2135
// @has 'foo/a/struct.FooASuper.html' '//pre' 'pub(crate) struct FooASuper'
2236
pub(super) struct FooASuper;
37+
// @has 'foo/a/index.html' '//a[@href="struct.FooAInSuper.html"]/..' 'FooAInSuper 🔒'
2338
// @has 'foo/a/struct.FooAInSuper.html' '//pre' 'pub(crate) struct FooAInSuper'
2439
pub(in super) struct FooAInSuper;
40+
// @has 'foo/a/index.html' '//a[@href="struct.FooAInA.html"]/..' 'FooAInA 🔒'
2541
// @has 'foo/a/struct.FooAInA.html' '//pre' 'struct FooAInA'
2642
// @!has 'foo/a/struct.FooAInA.html' '//pre' 'pub'
2743
pub(in a) struct FooAInA;
44+
// @has 'foo/a/index.html' '//a[@href="struct.FooAPriv.html"]/..' 'FooAPriv 🔒'
2845
// @has 'foo/a/struct.FooAPriv.html' '//pre' 'struct FooAPriv'
2946
// @!has 'foo/a/struct.FooAPriv.html' '//pre' 'pub'
3047
struct FooAPriv;
3148

49+
// @has 'foo/a/index.html' '//a[@href="b/index.html"]/..' 'b 🔒'
3250
mod b {
51+
// @has 'foo/a/b/index.html' '//a[@href="struct.FooBSuper.html"]/..' 'FooBSuper 🔒'
3352
// @has 'foo/a/b/struct.FooBSuper.html' '//pre' 'pub(super) struct FooBSuper'
3453
pub(super) struct FooBSuper;
54+
// @has 'foo/a/b/index.html' '//a[@href="struct.FooBInSuperSuper.html"]/..' 'FooBInSuperSuper 🔒'
3555
// @has 'foo/a/b/struct.FooBInSuperSuper.html' '//pre' 'pub(crate) struct FooBInSuperSuper'
3656
pub(in super::super) struct FooBInSuperSuper;
57+
// @has 'foo/a/b/index.html' '//a[@href="struct.FooBInAB.html"]/..' 'FooBInAB 🔒'
3758
// @has 'foo/a/b/struct.FooBInAB.html' '//pre' 'struct FooBInAB'
3859
// @!has 'foo/a/b/struct.FooBInAB.html' '//pre' 'pub'
3960
pub(in a::b) struct FooBInAB;
61+
// @has 'foo/a/b/index.html' '//a[@href="struct.FooBPriv.html"]/..' 'FooBPriv 🔒'
4062
// @has 'foo/a/b/struct.FooBPriv.html' '//pre' 'struct FooBPriv'
4163
// @!has 'foo/a/b/struct.FooBPriv.html' '//pre' 'pub'
4264
struct FooBPriv;
65+
66+
// @!has 'foo/a/b/index.html' '//a[@href="struct.FooBPub.html"]/..' 'FooBPub 🔒'
67+
// @has 'foo/a/b/struct.FooBPub.html' '//pre' 'pub struct FooBPub'
68+
pub struct FooBPub;
4369
}
4470
}
4571

@@ -53,13 +79,18 @@ mod a {
5379
//
5480
// @has 'foo/trait.PubTrait.html' '//pre' 'fn function();'
5581
// @!has 'foo/trait.PubTrait.html' '//pre' 'pub fn function();'
82+
//
83+
// @!has 'foo/index.html' '//a[@href="trait.PubTrait.html"]/..' 'PubTrait 🔒'
5684

5785
pub trait PubTrait {
5886
type Type;
5987
const CONST: usize;
6088
fn function();
6189
}
6290

91+
// @has 'foo/index.html' '//a[@href="trait.PrivTrait.html"]/..' 'PrivTrait 🔒'
92+
trait PrivTrait {}
93+
6394
// @has 'foo/struct.FooPublic.html' '//h4[@class="code-header"]' 'type Type'
6495
// @!has 'foo/struct.FooPublic.html' '//h4[@class="code-header"]' 'pub type Type'
6596
//

0 commit comments

Comments
 (0)