From b5b353bea74bb121d7270ac136939b5a77f03dec Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 21 Jan 2025 16:34:38 +0100 Subject: [PATCH 1/2] Improve display of const unstable feature --- src/librustdoc/html/render/mod.rs | 22 +++++++++++++++++-- .../html/templates/short_item_info.html | 12 ++++++++++ tests/rustdoc/const-display.rs | 2 +- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 9a9ce31caaa4c..885b3c10965b3 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -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, + }, } /// Render the stability, deprecation and portability information that is displayed at the top of @@ -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)) @@ -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 }); } diff --git a/src/librustdoc/html/templates/short_item_info.html b/src/librustdoc/html/templates/short_item_info.html index e76b98541cf61..b1a5ac1965922 100644 --- a/src/librustdoc/html/templates/short_item_info.html +++ b/src/librustdoc/html/templates/short_item_info.html @@ -18,6 +18,18 @@ ) {# #} {# #} + {% when Self::ConstUnstable with { feature, tracking } %} +
{# #} + 🔬 {# #} + {# #} + The const version is a nightly-only experimental API. ({# #} + {{feature}} + {% if let Some(num) = tracking %} +  #{{num}} + {% endif %} + ) {# #} + {# #} +
{% when Self::Portability with { message } %}
{{message|safe}}
{% endmatch %} diff --git a/tests/rustdoc/const-display.rs b/tests/rustdoc/const-display.rs index bc4270c421d5d..2e4cfd0bfb038 100644 --- a/tests/rustdoc/const-display.rs +++ b/tests/rustdoc/const-display.rs @@ -14,7 +14,7 @@ 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)' #[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' From a6bbf15b2ced56d10549edf5be327f104b83f702 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 21 Jan 2025 16:36:49 +0100 Subject: [PATCH 2/2] Add regression test for #131618 --- tests/rustdoc/const-display.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/rustdoc/const-display.rs b/tests/rustdoc/const-display.rs index 2e4cfd0bfb038..9877286ff59fd 100644 --- a/tests/rustdoc/const-display.rs +++ b/tests/rustdoc/const-display.rs @@ -7,12 +7,18 @@ //@ 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 = "111")] pub const unsafe fn foo_unsafe() -> u32 { 42 } @@ -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 }