Skip to content

Commit 812576f

Browse files
authored
Rollup merge of rust-lang#56824 - euclio:internal-apis, r=QuietMisdreavus
rustdoc: display rustc_private APIs as "Internal" This PR updates the display of `rustc_private` APIs to be "Internal" instead of "Experimental", and changes the colors appropriately. It also updates the copy of the `rustc_private` feature to sound more informative and less like a compiler suggestion. The PR additionally contains a significant refactor of the `short_stability` function to remove duplication and fix a few rendering bugs due to extra or missing spaces. Before: ![screen shot 2018-12-14 at 11 45 28 am](https://user-images.githubusercontent.com/1372438/50015926-c9768d80-ff95-11e8-9649-5df29df6909b.png) After: ![screen shot 2018-12-14 at 11 45 11 am](https://user-images.githubusercontent.com/1372438/50015934-cf6c6e80-ff95-11e8-912b-74b893f55425.png)
2 parents 80c398b + 8d393bf commit 812576f

File tree

9 files changed

+183
-127
lines changed

9 files changed

+183
-127
lines changed

src/librustdoc/clean/mod.rs

+27-21
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ impl Item {
478478
classes.push("unstable");
479479
}
480480

481-
if !s.deprecated_since.is_empty() {
481+
if s.deprecation.is_some() {
482482
classes.push("deprecated");
483483
}
484484

@@ -503,6 +503,15 @@ impl Item {
503503
pub fn type_(&self) -> ItemType {
504504
ItemType::from(self)
505505
}
506+
507+
/// Returns the info in the item's `#[deprecated]` or `#[rustc_deprecated]` attributes.
508+
///
509+
/// If the item is not deprecated, returns `None`.
510+
pub fn deprecation(&self) -> Option<&Deprecation> {
511+
self.deprecation
512+
.as_ref()
513+
.or_else(|| self.stability.as_ref().and_then(|s| s.deprecation.as_ref()))
514+
}
506515
}
507516

508517
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
@@ -3844,40 +3853,37 @@ impl Clean<Item> for doctree::ProcMacro {
38443853
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
38453854
pub struct Stability {
38463855
pub level: stability::StabilityLevel,
3847-
pub feature: String,
3856+
pub feature: Option<String>,
38483857
pub since: String,
3849-
pub deprecated_since: String,
3850-
pub deprecated_reason: String,
3851-
pub unstable_reason: String,
3852-
pub issue: Option<u32>
3858+
pub deprecation: Option<Deprecation>,
3859+
pub unstable_reason: Option<String>,
3860+
pub issue: Option<u32>,
38533861
}
38543862

38553863
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
38563864
pub struct Deprecation {
3857-
pub since: String,
3858-
pub note: String,
3865+
pub since: Option<String>,
3866+
pub note: Option<String>,
38593867
}
38603868

38613869
impl Clean<Stability> for attr::Stability {
38623870
fn clean(&self, _: &DocContext) -> Stability {
38633871
Stability {
38643872
level: stability::StabilityLevel::from_attr_level(&self.level),
3865-
feature: self.feature.to_string(),
3873+
feature: Some(self.feature.to_string()).filter(|f| !f.is_empty()),
38663874
since: match self.level {
38673875
attr::Stable {ref since} => since.to_string(),
38683876
_ => String::new(),
38693877
},
3870-
deprecated_since: match self.rustc_depr {
3871-
Some(attr::RustcDeprecation {ref since, ..}) => since.to_string(),
3872-
_=> String::new(),
3873-
},
3874-
deprecated_reason: match self.rustc_depr {
3875-
Some(ref depr) => depr.reason.to_string(),
3876-
_ => String::new(),
3877-
},
3878+
deprecation: self.rustc_depr.as_ref().map(|d| {
3879+
Deprecation {
3880+
note: Some(d.reason.to_string()).filter(|r| !r.is_empty()),
3881+
since: Some(d.since.to_string()).filter(|d| !d.is_empty()),
3882+
}
3883+
}),
38783884
unstable_reason: match self.level {
3879-
attr::Unstable { reason: Some(ref reason), .. } => reason.to_string(),
3880-
_ => String::new(),
3885+
attr::Unstable { reason: Some(ref reason), .. } => Some(reason.to_string()),
3886+
_ => None,
38813887
},
38823888
issue: match self.level {
38833889
attr::Unstable {issue, ..} => Some(issue),
@@ -3896,8 +3902,8 @@ impl<'a> Clean<Stability> for &'a attr::Stability {
38963902
impl Clean<Deprecation> for attr::Deprecation {
38973903
fn clean(&self, _: &DocContext) -> Deprecation {
38983904
Deprecation {
3899-
since: self.since.as_ref().map_or(String::new(), |s| s.to_string()),
3900-
note: self.note.as_ref().map_or(String::new(), |s| s.to_string()),
3905+
since: self.since.map(|s| s.to_string()).filter(|s| !s.is_empty()),
3906+
note: self.note.map(|n| n.to_string()).filter(|n| !n.is_empty()),
39013907
}
39023908
}
39033909
}

src/librustdoc/html/render.rs

+105-95
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use rustc::hir;
6565
use rustc::util::nodemap::{FxHashMap, FxHashSet};
6666
use rustc_data_structures::flock;
6767

68-
use clean::{self, AttributesExt, GetDefId, SelfTy, Mutability};
68+
use clean::{self, AttributesExt, Deprecation, GetDefId, SelfTy, Mutability};
6969
use config::RenderOptions;
7070
use doctree;
7171
use fold::DocFolder;
@@ -2458,7 +2458,7 @@ fn document_full(w: &mut fmt::Formatter, item: &clean::Item,
24582458

24592459
fn document_stability(w: &mut fmt::Formatter, cx: &Context, item: &clean::Item,
24602460
is_hidden: bool) -> fmt::Result {
2461-
let stabilities = short_stability(item, cx, true);
2461+
let stabilities = short_stability(item, cx);
24622462
if !stabilities.is_empty() {
24632463
write!(w, "<div class='stability{}'>", if is_hidden { " hidden" } else { "" })?;
24642464
for stability in stabilities {
@@ -2651,18 +2651,6 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
26512651
_ => {
26522652
if myitem.name.is_none() { continue }
26532653

2654-
let stabilities = short_stability(myitem, cx, false);
2655-
2656-
let stab_docs = if !stabilities.is_empty() {
2657-
stabilities.iter()
2658-
.map(|s| format!("[{}]", s))
2659-
.collect::<Vec<_>>()
2660-
.as_slice()
2661-
.join(" ")
2662-
} else {
2663-
String::new()
2664-
};
2665-
26662654
let unsafety_flag = match myitem.inner {
26672655
clean::FunctionItem(ref func) | clean::ForeignFunctionItem(ref func)
26682656
if func.header.unsafety == hir::Unsafety::Unsafe => {
@@ -2683,11 +2671,11 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
26832671
<tr class='{stab}{add}module-item'>\
26842672
<td><a class=\"{class}\" href=\"{href}\" \
26852673
title='{title}'>{name}</a>{unsafety_flag}</td>\
2686-
<td class='docblock-short'>{stab_docs}{docs}\
2674+
<td class='docblock-short'>{stab_tags}{docs}\
26872675
</td>\
26882676
</tr>",
26892677
name = *myitem.name.as_ref().unwrap(),
2690-
stab_docs = stab_docs,
2678+
stab_tags = stability_tags(myitem),
26912679
docs = MarkdownSummaryLine(doc_value, &myitem.links()),
26922680
class = myitem.type_(),
26932681
add = add,
@@ -2714,101 +2702,123 @@ fn item_module(w: &mut fmt::Formatter, cx: &Context,
27142702
Ok(())
27152703
}
27162704

2717-
fn short_stability(item: &clean::Item, cx: &Context, show_reason: bool) -> Vec<String> {
2705+
/// Render the stability and deprecation tags that are displayed in the item's summary at the
2706+
/// module level.
2707+
fn stability_tags(item: &clean::Item) -> String {
2708+
let mut tags = String::new();
2709+
2710+
// The trailing space after each tag is to space it properly against the rest of the docs.
2711+
if item.deprecation().is_some() {
2712+
tags.push_str("[<div class='stab deprecated'>Deprecated</div>] ");
2713+
}
2714+
2715+
if let Some(stab) = item
2716+
.stability
2717+
.as_ref()
2718+
.filter(|s| s.level == stability::Unstable)
2719+
{
2720+
if stab.feature.as_ref().map(|s| &**s) == Some("rustc_private") {
2721+
tags.push_str("[<div class='stab internal'>Internal</div>] ");
2722+
} else {
2723+
tags.push_str("[<div class='stab unstable'>Experimental</div>] ");
2724+
}
2725+
}
2726+
2727+
if let Some(ref cfg) = item.attrs.cfg {
2728+
tags.push_str(&format!(
2729+
"[<div class='stab portability'>{}</div>] ",
2730+
cfg.render_short_html()
2731+
));
2732+
}
2733+
2734+
tags
2735+
}
2736+
2737+
/// Render the stability and/or deprecation warning that is displayed at the top of the item's
2738+
/// documentation.
2739+
fn short_stability(item: &clean::Item, cx: &Context) -> Vec<String> {
27182740
let mut stability = vec![];
27192741
let error_codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
27202742

2721-
if let Some(stab) = item.stability.as_ref() {
2722-
let deprecated_reason = if show_reason && !stab.deprecated_reason.is_empty() {
2723-
format!(": {}", stab.deprecated_reason)
2743+
if let Some(Deprecation { since, note }) = &item.deprecation() {
2744+
let mut message = if let Some(since) = since {
2745+
if stability::deprecation_in_effect(since) {
2746+
format!("Deprecated since {}", Escape(since))
2747+
} else {
2748+
format!("Deprecating in {}", Escape(since))
2749+
}
27242750
} else {
2725-
String::new()
2751+
String::from("Deprecated")
27262752
};
2727-
if !stab.deprecated_since.is_empty() {
2728-
let since = if show_reason {
2729-
format!(" since {}", Escape(&stab.deprecated_since))
2730-
} else {
2731-
String::new()
2732-
};
2753+
2754+
if let Some(note) = note {
27332755
let mut ids = cx.id_map.borrow_mut();
2734-
let html = MarkdownHtml(&deprecated_reason, RefCell::new(&mut ids), error_codes);
2735-
let text = if stability::deprecation_in_effect(&stab.deprecated_since) {
2736-
format!("Deprecated{}{}", since, html)
2756+
let html = MarkdownHtml(&note, RefCell::new(&mut ids), error_codes);
2757+
message.push_str(&format!(": {}", html));
2758+
}
2759+
stability.push(format!("<div class='stab deprecated'>{}</div>", message));
2760+
}
2761+
2762+
if let Some(stab) = item
2763+
.stability
2764+
.as_ref()
2765+
.filter(|stab| stab.level == stability::Unstable)
2766+
{
2767+
let is_rustc_private = stab.feature.as_ref().map(|s| &**s) == Some("rustc_private");
2768+
2769+
let mut message = if is_rustc_private {
2770+
"<span class='emoji'>⚙️</span> This is an internal compiler API."
2771+
} else {
2772+
"<span class='emoji'>🔬</span> This is a nightly-only experimental API."
2773+
}
2774+
.to_owned();
2775+
2776+
if let Some(feature) = stab.feature.as_ref() {
2777+
let mut feature = format!("<code>{}</code>", Escape(&feature));
2778+
if let (Some(url), Some(issue)) = (&cx.shared.issue_tracker_base_url, stab.issue) {
2779+
feature.push_str(&format!(
2780+
"&nbsp;<a href=\"{url}{issue}\">#{issue}</a>",
2781+
url = url,
2782+
issue = issue
2783+
));
2784+
}
2785+
2786+
message.push_str(&format!(" ({})", feature));
2787+
}
2788+
2789+
if let Some(unstable_reason) = &stab.unstable_reason {
2790+
// Provide a more informative message than the compiler help.
2791+
let unstable_reason = if is_rustc_private {
2792+
"This crate is being loaded from the sysroot, a permanently unstable location \
2793+
for private compiler dependencies. It is not intended for general use. Prefer \
2794+
using a public version of this crate from \
2795+
[crates.io](https://crates.io) via [`Cargo.toml`]\
2796+
(https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html)."
27372797
} else {
2738-
format!("Deprecating in {}{}", Escape(&stab.deprecated_since), html)
2798+
unstable_reason
27392799
};
2740-
stability.push(format!("<div class='stab deprecated'>{}</div>", text))
2741-
};
27422800

2743-
if stab.level == stability::Unstable {
2744-
if show_reason {
2745-
let unstable_extra = match (!stab.feature.is_empty(),
2746-
&cx.shared.issue_tracker_base_url,
2747-
stab.issue) {
2748-
(true, &Some(ref tracker_url), Some(issue_no)) if issue_no > 0 =>
2749-
format!(" (<code>{} </code><a href=\"{}{}\">#{}</a>)",
2750-
Escape(&stab.feature), tracker_url, issue_no, issue_no),
2751-
(false, &Some(ref tracker_url), Some(issue_no)) if issue_no > 0 =>
2752-
format!(" (<a href=\"{}{}\">#{}</a>)", Escape(&tracker_url), issue_no,
2753-
issue_no),
2754-
(true, ..) =>
2755-
format!(" (<code>{}</code>)", Escape(&stab.feature)),
2756-
_ => String::new(),
2757-
};
2758-
if stab.unstable_reason.is_empty() {
2759-
stability.push(format!("<div class='stab unstable'>\
2760-
<span class=microscope>🔬</span> \
2761-
This is a nightly-only experimental API. {}\
2762-
</div>",
2763-
unstable_extra));
2764-
} else {
2765-
let mut ids = cx.id_map.borrow_mut();
2766-
let text = format!("<summary><span class=microscope>🔬</span> \
2767-
This is a nightly-only experimental API. {}\
2768-
</summary>{}",
2769-
unstable_extra,
2770-
MarkdownHtml(
2771-
&stab.unstable_reason,
2772-
RefCell::new(&mut ids),
2773-
error_codes));
2774-
stability.push(format!("<div class='stab unstable'><details>{}</details></div>",
2775-
text));
2776-
}
2777-
} else {
2778-
stability.push("<div class='stab unstable'>Experimental</div>".to_string())
2779-
}
2780-
};
2781-
} else if let Some(depr) = item.deprecation.as_ref() {
2782-
let note = if show_reason && !depr.note.is_empty() {
2783-
format!(": {}", depr.note)
2784-
} else {
2785-
String::new()
2786-
};
2787-
let since = if show_reason && !depr.since.is_empty() {
2788-
format!(" since {}", Escape(&depr.since))
2789-
} else {
2790-
String::new()
2791-
};
2801+
let mut ids = cx.id_map.borrow_mut();
2802+
message = format!(
2803+
"<details><summary>{}</summary>{}</details>",
2804+
message,
2805+
MarkdownHtml(&unstable_reason, RefCell::new(&mut ids), error_codes)
2806+
);
2807+
}
27922808

2793-
let mut ids = cx.id_map.borrow_mut();
2794-
let text = if stability::deprecation_in_effect(&depr.since) {
2795-
format!("Deprecated{}{}",
2796-
since,
2797-
MarkdownHtml(&note, RefCell::new(&mut ids), error_codes))
2809+
let class = if is_rustc_private {
2810+
"internal"
27982811
} else {
2799-
format!("Deprecating in {}{}",
2800-
Escape(&depr.since),
2801-
MarkdownHtml(&note, RefCell::new(&mut ids), error_codes))
2812+
"unstable"
28022813
};
2803-
stability.push(format!("<div class='stab deprecated'>{}</div>", text))
2814+
stability.push(format!("<div class='stab {}'>{}</div>", class, message));
28042815
}
28052816

28062817
if let Some(ref cfg) = item.attrs.cfg {
2807-
stability.push(format!("<div class='stab portability'>{}</div>", if show_reason {
2818+
stability.push(format!(
2819+
"<div class='stab portability'>{}</div>",
28082820
cfg.render_long_html()
2809-
} else {
2810-
cfg.render_short_html()
2811-
}));
2821+
));
28122822
}
28132823

28142824
stability

src/librustdoc/html/static/rustdoc.css

+1-1
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,7 @@ body.blur > :not(#help) {
769769
display: list-item;
770770
}
771771

772-
.stab .microscope {
772+
.stab .emoji {
773773
font-size: 1.5em;
774774
}
775775

src/librustdoc/html/static/themes/dark.css

+5
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ a {
174174
color: #D2991D;
175175
}
176176

177+
.stab.internal a {
178+
color: #304FFE;
179+
}
180+
177181
a.test-arrow {
178182
color: #dedede;
179183
}
@@ -199,6 +203,7 @@ a.test-arrow {
199203
}
200204

201205
.stab.unstable { background: #FFF5D6; border-color: #FFC600; color: #404040; }
206+
.stab.internal { background: #FFB9B3; border-color: #B71C1C; color: #404040; }
202207
.stab.deprecated { background: #F3DFFF; border-color: #7F0087; color: #404040; }
203208
.stab.portability { background: #C4ECFF; border-color: #7BA5DB; color: #404040; }
204209

src/librustdoc/html/static/themes/light.css

+5
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ a {
174174
color: #3873AD;
175175
}
176176

177+
.stab.internal a {
178+
color: #304FFE;
179+
}
180+
177181
a.test-arrow {
178182
color: #f5f5f5;
179183
}
@@ -200,6 +204,7 @@ a.test-arrow {
200204
}
201205

202206
.stab.unstable { background: #FFF5D6; border-color: #FFC600; }
207+
.stab.internal { background: #FFB9B3; border-color: #B71C1C; }
203208
.stab.deprecated { background: #F3DFFF; border-color: #7F0087; }
204209
.stab.portability { background: #C4ECFF; border-color: #7BA5DB; }
205210

0 commit comments

Comments
 (0)