Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve display of const unstable display feature info #135830

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,12 @@ enum ShortItemInfo {
Portability {
message: String,
},
/// The feature corresponding to a const unstable item, and optionally
/// a tracking issue URL and number.
ConstUnstable {
feature: String,
tracking: Option<u32>,
},
}

/// Render the stability, deprecation and portability information that is displayed at the top of
Expand Down Expand Up @@ -723,10 +729,10 @@ fn short_item_info(
extra_info.push(ShortItemInfo::Deprecation { message });
}

let stability = item.stability(cx.tcx());
// Render unstable items. But don't render "rustc_private" crates (internal compiler crates).
// Those crates are permanently unstable so it makes no sense to render "unstable" everywhere.
if let Some((StabilityLevel::Unstable { reason: _, issue, .. }, feature)) = item
.stability(cx.tcx())
if let Some((StabilityLevel::Unstable { reason: _, issue, .. }, feature)) = stability
.as_ref()
.filter(|stab| stab.feature != sym::rustc_private)
.map(|stab| (stab.level, stab.feature))
Expand All @@ -740,6 +746,18 @@ fn short_item_info(
extra_info.push(ShortItemInfo::Unstable { feature: feature.to_string(), tracking });
}

// Only display const unstable if NOT entirely unstable.
if stability.and_then(|stability| stability.stable_since()).is_some()
&& let Some(ConstStability {
level: StabilityLevel::Unstable { issue, .. }, feature, ..
}) = item.const_stability(cx.tcx())
{
extra_info.push(ShortItemInfo::ConstUnstable {
feature: feature.to_string(),
tracking: issue.map(|issue| issue.get()),
});
}

if let Some(message) = portability(item, parent) {
extra_info.push(ShortItemInfo::Portability { message });
}
Expand Down
12 changes: 12 additions & 0 deletions src/librustdoc/html/templates/short_item_info.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@
) {# #}
</span> {# #}
</div>
{% when Self::ConstUnstable with { feature, tracking } %}
<div class="stab unstable"> {# #}
<span class="emoji">🔬</span> {# #}
<span> {# #}
The const version is a nightly-only experimental API. ({# #}
<code>{{feature}}</code>
{% if let Some(num) = tracking %}
&nbsp;<a href="https://github.com/rust-lang/rust/issues/{{num}}">#{{num}}</a>
{% endif %}
) {# #}
</span> {# #}
</div>
{% when Self::Portability with { message } %}
<div class="stab portability">{{message|safe}}</div>
{% endmatch %}
16 changes: 14 additions & 2 deletions tests/rustdoc/const-display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@

//@ has 'foo/fn.foo.html' '//pre' 'pub fn foo() -> u32'
//@ has - '//span[@class="since"]' '1.0.0 (const: unstable)'
//@ has - '//span[@class="item-info"]//span' 'The const version is a \
// nightly-only experimental API. (foo)'
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature="foo", issue = "none")]
pub const fn foo() -> u32 { 42 }

//@ has 'foo/fn.foo_unsafe.html' '//pre' 'pub unsafe fn foo_unsafe() -> u32'
//@ has - '//span[@class="since"]' '1.0.0 (const: unstable)'
//@ has - '//span[@class="item-info"]//span' 'The const version is a \
// nightly-only experimental API. (foo #111)'
//@ has - '//span[@class="item-info"]//a[@href="https://github.com/rust-lang/rust/issues/111"]' \
// '#111'
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature="foo", issue = "none")]
#[rustc_const_unstable(feature="foo", issue = "111")]
pub const unsafe fn foo_unsafe() -> u32 { 42 }

//@ has 'foo/fn.foo2.html' '//pre' 'pub const fn foo2() -> u32'
Expand Down Expand Up @@ -63,12 +69,18 @@ pub struct Foo;
impl Foo {
//@ has 'foo/struct.Foo.html' '//*[@id="method.gated"]/h4[@class="code-header"]' 'pub fn gated() -> u32'
//@ has - '//span[@class="since"]' '1.0.0 (const: unstable)'
//@ has - '//span[@class="item-info"]//span' 'The const version is a \
// nightly-only experimental API. (foo #112)'
//@ has - '//span[@class="item-info"]//a[@href="https://github.com/rust-lang/rust/issues/112"]' \
// '#112'
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature="foo", issue = "none")]
#[rustc_const_unstable(feature="foo", issue = "112")]
pub const fn gated() -> u32 { 42 }

//@ has 'foo/struct.Foo.html' '//*[@id="method.gated_unsafe"]/h4[@class="code-header"]' 'pub unsafe fn gated_unsafe() -> u32'
//@ has - '//span[@class="since"]' '1.0.0 (const: unstable)'
//@ has - '//span[@class="item-info"]//span' 'The const version is a \
// nightly-only experimental API. (foo)'
#[stable(feature = "rust1", since = "1.0.0")]
#[rustc_const_unstable(feature="foo", issue = "none")]
pub const unsafe fn gated_unsafe() -> u32 { 42 }
Expand Down
Loading