Skip to content

Commit 699d28f

Browse files
committed
rustdoc: Show "const" for const-unstable if also overall unstable
If a const function is unstable overall (and thus, in all circumstances I know of, also const-unstable), we should show the option to use it as const. You need to enable a feature to use the function at all anyway. If the function is stabilized without also being const-stabilized, then we do not show the const keyword and instead show "const: unstable" in the version info.
1 parent fa7a3f9 commit 699d28f

File tree

5 files changed

+39
-20
lines changed

5 files changed

+39
-20
lines changed

src/librustdoc/clean/types.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ impl Item {
627627
) -> hir::FnHeader {
628628
let sig = tcx.fn_sig(def_id).skip_binder();
629629
let constness =
630-
if tcx.is_const_fn(def_id) && is_unstable_const_fn(tcx, def_id).is_none() {
630+
if tcx.is_const_fn(def_id) || is_unstable_const_fn(tcx, def_id).is_some() {
631631
hir::Constness::Const
632632
} else {
633633
hir::Constness::NotConst
@@ -649,9 +649,8 @@ impl Item {
649649
hir::Safety::Unsafe
650650
},
651651
abi,
652-
constness: if abi == Abi::RustIntrinsic
653-
&& tcx.is_const_fn(def_id)
654-
&& is_unstable_const_fn(tcx, def_id).is_none()
652+
constness: if tcx.is_const_fn(def_id)
653+
|| is_unstable_const_fn(tcx, def_id).is_some()
655654
{
656655
hir::Constness::Const
657656
} else {

src/librustdoc/html/format.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use std::fmt::{self, Display, Write};
1313
use std::iter::{self, once};
1414

1515
use rustc_ast as ast;
16-
use rustc_attr::{ConstStability, StabilityLevel};
16+
use rustc_attr::{ConstStability, StabilityLevel, StableSince};
1717
use rustc_data_structures::captures::Captures;
1818
use rustc_data_structures::fx::FxHashSet;
1919
use rustc_hir as hir;
@@ -1633,17 +1633,24 @@ impl PrintWithSpace for hir::Mutability {
16331633

16341634
pub(crate) fn print_constness_with_space(
16351635
c: &hir::Constness,
1636-
s: Option<ConstStability>,
1636+
overall_stab: Option<StableSince>,
1637+
const_stab: Option<ConstStability>,
16371638
) -> &'static str {
1638-
match (c, s) {
1639-
// const stable or when feature(staged_api) is not set
1640-
(
1641-
hir::Constness::Const,
1642-
Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }),
1643-
)
1644-
| (hir::Constness::Const, None) => "const ",
1645-
// const unstable or not const
1646-
_ => "",
1639+
match c {
1640+
hir::Constness::Const => match (overall_stab, const_stab) {
1641+
// const stable...
1642+
(_, Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }))
1643+
// ...or when feature(staged_api) is not set...
1644+
| (_, None)
1645+
// ...or when const unstable, but overall unstable too
1646+
| (None, Some(ConstStability { level: StabilityLevel::Unstable { .. }, .. })) => {
1647+
"const "
1648+
}
1649+
// const unstable (and overall stable)
1650+
(Some(_), Some(ConstStability { level: StabilityLevel::Unstable { .. }, .. })) => "",
1651+
},
1652+
// not const
1653+
hir::Constness::NotConst => "",
16471654
}
16481655
}
16491656

src/librustdoc/html/render/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -928,9 +928,11 @@ fn assoc_method(
928928
// FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
929929
// this condition.
930930
let constness = match render_mode {
931-
RenderMode::Normal => {
932-
print_constness_with_space(&header.constness, meth.const_stability(tcx))
933-
}
931+
RenderMode::Normal => print_constness_with_space(
932+
&header.constness,
933+
meth.stable_since(tcx),
934+
meth.const_stability(tcx),
935+
),
934936
RenderMode::ForDeref { .. } => "",
935937
};
936938
let asyncness = header.asyncness.print_with_space();

src/librustdoc/html/render/print_item.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,18 @@ fn extra_info_tags<'a, 'tcx: 'a>(
615615
fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &clean::Function) {
616616
let tcx = cx.tcx();
617617
let header = it.fn_header(tcx).expect("printing a function which isn't a function");
618-
let constness = print_constness_with_space(&header.constness, it.const_stability(tcx));
618+
debug!(
619+
"item_function/const: {:?} {:?} {:?} {:?}",
620+
it.name,
621+
&header.constness,
622+
it.stable_since(tcx),
623+
it.const_stability(tcx),
624+
);
625+
let constness = print_constness_with_space(
626+
&header.constness,
627+
it.stable_since(tcx),
628+
it.const_stability(tcx),
629+
);
619630
let safety = header.safety.print_with_space();
620631
let abi = print_abi_with_space(header.abi).to_string();
621632
let asyncness = header.asyncness.print_with_space();

tests/rustdoc/const-display.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub const unsafe fn foo_unsafe() -> u32 { 42 }
2424
#[unstable(feature = "humans", issue = "none")]
2525
pub const fn foo2() -> u32 { 42 }
2626

27-
// @has 'foo/fn.foo3.html' '//pre' 'pub fn foo3() -> u32'
27+
// @has 'foo/fn.foo3.html' '//pre' 'pub const fn foo3() -> u32'
2828
// @!hasraw - '//span[@class="since"]'
2929
#[unstable(feature = "humans", issue = "none")]
3030
#[rustc_const_unstable(feature = "humans", issue = "none")]

0 commit comments

Comments
 (0)