Skip to content

Commit

Permalink
Improve display of const unstable feature
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jan 21, 2025
1 parent 9a1d156 commit b5b353b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
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 %}
2 changes: 1 addition & 1 deletion tests/rustdoc/const-display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit b5b353b

Please sign in to comment.