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

rustdoc: Clarify const-stability with regard to normal stability #125599

Merged
merged 2 commits into from
May 27, 2024
Merged
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
7 changes: 3 additions & 4 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ impl Item {
) -> hir::FnHeader {
let sig = tcx.fn_sig(def_id).skip_binder();
let constness =
if tcx.is_const_fn(def_id) && is_unstable_const_fn(tcx, def_id).is_none() {
if tcx.is_const_fn(def_id) || is_unstable_const_fn(tcx, def_id).is_some() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that not only is this necessary for this PR, but also is more accurate since it matches how the function is actually declared.

hir::Constness::Const
} else {
hir::Constness::NotConst
Expand All @@ -649,9 +649,8 @@ impl Item {
hir::Safety::Unsafe
},
abi,
constness: if abi == Abi::RustIntrinsic
&& tcx.is_const_fn(def_id)
&& is_unstable_const_fn(tcx, def_id).is_none()
Comment on lines -652 to -654
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what this intrinsic special-casing was about, so I should probably look into if we still need it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it seems like it was just to be conservative, but I can't think of any reason why tcx.is_const_fn would "lie" to us here. Is there any reason this needs to be kept? cc @clubby789 @GuillaumeGomez

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove it and see what's breaking? 😆

constness: if tcx.is_const_fn(def_id)
|| is_unstable_const_fn(tcx, def_id).is_some()
{
hir::Constness::Const
} else {
Expand Down
29 changes: 18 additions & 11 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use std::fmt::{self, Display, Write};
use std::iter::{self, once};

use rustc_ast as ast;
use rustc_attr::{ConstStability, StabilityLevel};
use rustc_attr::{ConstStability, StabilityLevel, StableSince};
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxHashSet;
use rustc_hir as hir;
Expand Down Expand Up @@ -1633,17 +1633,24 @@ impl PrintWithSpace for hir::Mutability {

pub(crate) fn print_constness_with_space(
c: &hir::Constness,
s: Option<ConstStability>,
overall_stab: Option<StableSince>,
const_stab: Option<ConstStability>,
) -> &'static str {
match (c, s) {
// const stable or when feature(staged_api) is not set
(
hir::Constness::Const,
Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }),
)
| (hir::Constness::Const, None) => "const ",
// const unstable or not const
_ => "",
match c {
hir::Constness::Const => match (overall_stab, const_stab) {
// const stable...
(_, Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }))
// ...or when feature(staged_api) is not set...
| (_, None)
// ...or when const unstable, but overall unstable too
| (None, Some(ConstStability { level: StabilityLevel::Unstable { .. }, .. })) => {
"const "
}
// const unstable (and overall stable)
(Some(_), Some(ConstStability { level: StabilityLevel::Unstable { .. }, .. })) => "",
},
// not const
hir::Constness::NotConst => "",
}
}

Expand Down
29 changes: 18 additions & 11 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -928,9 +928,11 @@ fn assoc_method(
// FIXME: Once https://github.com/rust-lang/rust/issues/67792 is implemented, we can remove
// this condition.
let constness = match render_mode {
RenderMode::Normal => {
print_constness_with_space(&header.constness, meth.const_stability(tcx))
}
RenderMode::Normal => print_constness_with_space(
&header.constness,
meth.stable_since(tcx),
meth.const_stability(tcx),
),
RenderMode::ForDeref { .. } => "",
};
let asyncness = header.asyncness.print_with_space();
Expand Down Expand Up @@ -1016,18 +1018,23 @@ fn render_stability_since_raw_with_extra(
.map(|since| (format!("const since {since}"), format!("const: {since}")))
}
Some(ConstStability { level: StabilityLevel::Unstable { issue, .. }, feature, .. }) => {
let unstable = if let Some(n) = issue {
format!(
"<a \
if stable_version.is_none() {
// don't display const unstable if entirely unstable
None
} else {
let unstable = if let Some(n) = issue {
format!(
"<a \
href=\"https://github.com/rust-lang/rust/issues/{n}\" \
title=\"Tracking issue for {feature}\"\
>unstable</a>"
)
} else {
String::from("unstable")
};
)
} else {
String::from("unstable")
};

Some((String::from("const unstable"), format!("const: {unstable}")))
Some((String::from("const unstable"), format!("const: {unstable}")))
}
}
_ => None,
};
Expand Down
13 changes: 12 additions & 1 deletion src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,18 @@ fn extra_info_tags<'a, 'tcx: 'a>(
fn item_function(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, f: &clean::Function) {
let tcx = cx.tcx();
let header = it.fn_header(tcx).expect("printing a function which isn't a function");
let constness = print_constness_with_space(&header.constness, it.const_stability(tcx));
debug!(
"item_function/const: {:?} {:?} {:?} {:?}",
it.name,
&header.constness,
it.stable_since(tcx),
it.const_stability(tcx),
);
let constness = print_constness_with_space(
&header.constness,
it.stable_since(tcx),
it.const_stability(tcx),
);
let safety = header.safety.print_with_space();
let abi = print_abi_with_space(header.abi).to_string();
let asyncness = header.asyncness.print_with_space();
Expand Down
6 changes: 6 additions & 0 deletions tests/rustdoc/const-display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ pub const unsafe fn foo_unsafe() -> u32 { 42 }
#[unstable(feature = "humans", issue = "none")]
pub const fn foo2() -> u32 { 42 }

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

// @has 'foo/fn.bar2.html' '//pre' 'pub const fn bar2() -> u32'
// @has - //span '1.0.0 (const: 1.0.0)'
#[stable(feature = "rust1", since = "1.0.0")]
Expand Down
Loading