Skip to content

Commit f3fe5c3

Browse files
authored
Rollup merge of #86473 - fee1-dead:rustdoc-const-unstable, r=jyn514
Rustdoc: Account for const-unstable functions Fixes #86464
2 parents 9c664b2 + b57077b commit f3fe5c3

File tree

4 files changed

+112
-51
lines changed

4 files changed

+112
-51
lines changed

src/librustdoc/html/format.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::cell::Cell;
99
use std::fmt;
1010
use std::iter;
1111

12+
use rustc_attr::{ConstStability, StabilityLevel};
1213
use rustc_data_structures::captures::Captures;
1314
use rustc_data_structures::fx::FxHashSet;
1415
use rustc_hir as hir;
@@ -1253,15 +1254,6 @@ impl PrintWithSpace for hir::Unsafety {
12531254
}
12541255
}
12551256

1256-
impl PrintWithSpace for hir::Constness {
1257-
fn print_with_space(&self) -> &str {
1258-
match self {
1259-
hir::Constness::Const => "const ",
1260-
hir::Constness::NotConst => "",
1261-
}
1262-
}
1263-
}
1264-
12651257
impl PrintWithSpace for hir::IsAsync {
12661258
fn print_with_space(&self) -> &str {
12671259
match self {
@@ -1280,6 +1272,22 @@ impl PrintWithSpace for hir::Mutability {
12801272
}
12811273
}
12821274

1275+
crate fn print_constness_with_space(
1276+
c: &hir::Constness,
1277+
s: Option<&ConstStability>,
1278+
) -> &'static str {
1279+
match (c, s) {
1280+
// const stable or when feature(staged_api) is not set
1281+
(
1282+
hir::Constness::Const,
1283+
Some(ConstStability { level: StabilityLevel::Stable { .. }, .. }),
1284+
)
1285+
| (hir::Constness::Const, None) => "const ",
1286+
// const unstable or not const
1287+
_ => "",
1288+
}
1289+
}
1290+
12831291
impl clean::Import {
12841292
crate fn print<'a, 'tcx: 'a>(
12851293
&'a self,

src/librustdoc/html/render/mod.rs

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use std::str;
4242
use std::string::ToString;
4343

4444
use rustc_ast_pretty::pprust;
45-
use rustc_attr::{Deprecation, StabilityLevel};
45+
use rustc_attr::{ConstStability, Deprecation, StabilityLevel};
4646
use rustc_data_structures::fx::FxHashSet;
4747
use rustc_hir as hir;
4848
use rustc_hir::def::CtorKind;
@@ -61,8 +61,8 @@ use crate::formats::item_type::ItemType;
6161
use crate::formats::{AssocItemRender, Impl, RenderMode};
6262
use crate::html::escape::Escape;
6363
use crate::html::format::{
64-
href, print_abi_with_space, print_default_space, print_generic_bounds, print_where_clause,
65-
Buffer, PrintWithSpace,
64+
href, print_abi_with_space, print_constness_with_space, print_default_space,
65+
print_generic_bounds, print_where_clause, Buffer, PrintWithSpace,
6666
};
6767
use crate::html::markdown::{Markdown, MarkdownHtml, MarkdownSummaryLine};
6868

@@ -826,21 +826,45 @@ fn assoc_type(
826826
fn render_stability_since_raw(
827827
w: &mut Buffer,
828828
ver: Option<&str>,
829-
const_ver: Option<&str>,
829+
const_stability: Option<&ConstStability>,
830830
containing_ver: Option<&str>,
831831
containing_const_ver: Option<&str>,
832832
) {
833833
let ver = ver.filter(|inner| !inner.is_empty());
834-
let const_ver = const_ver.filter(|inner| !inner.is_empty());
835834

836-
match (ver, const_ver) {
837-
(Some(v), Some(cv)) if const_ver != containing_const_ver => {
835+
match (ver, const_stability) {
836+
// stable and const stable
837+
(Some(v), Some(ConstStability { level: StabilityLevel::Stable { since }, .. }))
838+
if Some(since.as_str()).as_deref() != containing_const_ver =>
839+
{
838840
write!(
839841
w,
840842
"<span class=\"since\" title=\"Stable since Rust version {0}, const since {1}\">{0} (const: {1})</span>",
841-
v, cv
843+
v, since
842844
);
843845
}
846+
// stable and const unstable
847+
(
848+
Some(v),
849+
Some(ConstStability { level: StabilityLevel::Unstable { issue, .. }, feature, .. }),
850+
) => {
851+
write!(
852+
w,
853+
"<span class=\"since\" title=\"Stable since Rust version {0}, const unstable\">{0} (const: ",
854+
v
855+
);
856+
if let Some(n) = issue {
857+
write!(
858+
w,
859+
"<a href=\"https://github.com/rust-lang/rust/issues/{}\" title=\"Tracking issue for {}\">unstable</a>",
860+
n, feature
861+
);
862+
} else {
863+
write!(w, "unstable");
864+
}
865+
write!(w, ")</span>");
866+
}
867+
// stable
844868
(Some(v), _) if ver != containing_ver => {
845869
write!(
846870
w,
@@ -888,11 +912,13 @@ fn render_assoc_item(
888912
}
889913
};
890914
let vis = meth.visibility.print_with_space(meth.def_id, cx).to_string();
891-
let constness = header.constness.print_with_space();
915+
let constness =
916+
print_constness_with_space(&header.constness, meth.const_stability(cx.tcx()));
892917
let asyncness = header.asyncness.print_with_space();
893918
let unsafety = header.unsafety.print_with_space();
894919
let defaultness = print_default_space(meth.is_default());
895920
let abi = print_abi_with_space(header.abi).to_string();
921+
896922
// NOTE: `{:#}` does not print HTML formatting, `{}` does. So `g.print` can't be reused between the length calculation and `write!`.
897923
let generics_len = format!("{:#}", g.print(cx)).len();
898924
let mut header_len = "fn ".len()
@@ -917,15 +943,15 @@ fn render_assoc_item(
917943
w.reserve(header_len + "<a href=\"\" class=\"fnname\">{".len() + "</a>".len());
918944
write!(
919945
w,
920-
"{}{}{}{}{}{}{}fn <a href=\"{href}\" class=\"fnname\">{name}</a>\
946+
"{indent}{vis}{constness}{asyncness}{unsafety}{defaultness}{abi}fn <a href=\"{href}\" class=\"fnname\">{name}</a>\
921947
{generics}{decl}{notable_traits}{where_clause}",
922-
indent_str,
923-
vis,
924-
constness,
925-
asyncness,
926-
unsafety,
927-
defaultness,
928-
abi,
948+
indent = indent_str,
949+
vis = vis,
950+
constness = constness,
951+
asyncness = asyncness,
952+
unsafety = unsafety,
953+
defaultness = defaultness,
954+
abi = abi,
929955
href = href,
930956
name = name,
931957
generics = g.print(cx),
@@ -1583,7 +1609,7 @@ fn render_rightside(
15831609
render_stability_since_raw(
15841610
w,
15851611
item.stable_since(tcx).as_deref(),
1586-
item.const_stable_since(tcx).as_deref(),
1612+
item.const_stability(tcx),
15871613
containing_item.stable_since(tcx).as_deref(),
15881614
containing_item.const_stable_since(tcx).as_deref(),
15891615
);

src/librustdoc/html/render/print_item.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use crate::clean::{self, GetDefId};
2222
use crate::formats::item_type::ItemType;
2323
use crate::formats::{AssocItemRender, Impl, RenderMode};
2424
use crate::html::escape::Escape;
25-
use crate::html::format::{print_abi_with_space, print_where_clause, Buffer, PrintWithSpace};
25+
use crate::html::format::{
26+
print_abi_with_space, print_constness_with_space, print_where_clause, Buffer, PrintWithSpace,
27+
};
2628
use crate::html::highlight;
2729
use crate::html::layout::Page;
2830
use crate::html::markdown::MarkdownSummaryLine;
@@ -94,7 +96,7 @@ pub(super) fn print_item(cx: &Context<'_>, item: &clean::Item, buf: &mut Buffer,
9496
render_stability_since_raw(
9597
buf,
9698
item.stable_since(cx.tcx()).as_deref(),
97-
item.const_stable_since(cx.tcx()).as_deref(),
99+
item.const_stability(cx.tcx()),
98100
None,
99101
None,
100102
);
@@ -430,29 +432,36 @@ fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) ->
430432
}
431433

432434
fn item_function(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, f: &clean::Function) {
433-
let header_len = format!(
434-
"{}{}{}{}{:#}fn {}{:#}",
435-
it.visibility.print_with_space(it.def_id, cx),
436-
f.header.constness.print_with_space(),
437-
f.header.asyncness.print_with_space(),
438-
f.header.unsafety.print_with_space(),
439-
print_abi_with_space(f.header.abi),
440-
it.name.as_ref().unwrap(),
441-
f.generics.print(cx),
442-
)
443-
.len();
435+
let vis = it.visibility.print_with_space(it.def_id, cx).to_string();
436+
let constness = print_constness_with_space(&f.header.constness, it.const_stability(cx.tcx()));
437+
let asyncness = f.header.asyncness.print_with_space();
438+
let unsafety = f.header.unsafety.print_with_space();
439+
let abi = print_abi_with_space(f.header.abi).to_string();
440+
let name = it.name.as_ref().unwrap();
441+
442+
let generics_len = format!("{:#}", f.generics.print(cx)).len();
443+
let header_len = "fn ".len()
444+
+ vis.len()
445+
+ constness.len()
446+
+ asyncness.len()
447+
+ unsafety.len()
448+
+ abi.len()
449+
+ name.as_str().len()
450+
+ generics_len;
451+
444452
w.write_str("<pre class=\"rust fn\">");
445453
render_attributes_in_pre(w, it, "");
454+
w.reserve(header_len);
446455
write!(
447456
w,
448457
"{vis}{constness}{asyncness}{unsafety}{abi}fn \
449458
{name}{generics}{decl}{notable_traits}{where_clause}</pre>",
450-
vis = it.visibility.print_with_space(it.def_id, cx),
451-
constness = f.header.constness.print_with_space(),
452-
asyncness = f.header.asyncness.print_with_space(),
453-
unsafety = f.header.unsafety.print_with_space(),
454-
abi = print_abi_with_space(f.header.abi),
455-
name = it.name.as_ref().unwrap(),
459+
vis = vis,
460+
constness = constness,
461+
asyncness = asyncness,
462+
unsafety = unsafety,
463+
abi = abi,
464+
name = name,
456465
generics = f.generics.print(cx),
457466
where_clause = print_where_clause(&f.generics, cx, 0, true),
458467
decl = f.decl.full_print(header_len, 0, f.header.asyncness, cx),
@@ -1291,7 +1300,7 @@ fn render_stability_since(
12911300
render_stability_since_raw(
12921301
w,
12931302
item.stable_since(tcx).as_deref(),
1294-
item.const_stable_since(tcx).as_deref(),
1303+
item.const_stability(tcx),
12951304
containing_item.stable_since(tcx).as_deref(),
12961305
containing_item.const_stable_since(tcx).as_deref(),
12971306
)

src/test/rustdoc/const-display.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,20 @@
77
#![feature(foo, foo2)]
88
#![feature(staged_api)]
99

10-
// @has 'foo/fn.foo.html' '//pre' 'pub unsafe fn foo() -> u32'
10+
// @has 'foo/fn.foo.html' '//pre' 'pub fn foo() -> u32'
11+
// @has - '//span[@class="since"]' '1.0.0 (const: unstable)'
1112
#[stable(feature = "rust1", since = "1.0.0")]
1213
#[rustc_const_unstable(feature="foo", issue = "none")]
13-
pub const unsafe fn foo() -> u32 { 42 }
14+
pub const fn foo() -> u32 { 42 }
15+
16+
// @has 'foo/fn.foo_unsafe.html' '//pre' 'pub unsafe fn foo_unsafe() -> u32'
17+
// @has - '//span[@class="since"]' '1.0.0 (const: unstable)'
18+
#[stable(feature = "rust1", since = "1.0.0")]
19+
#[rustc_const_unstable(feature="foo", issue = "none")]
20+
pub const unsafe fn foo_unsafe() -> u32 { 42 }
1421

1522
// @has 'foo/fn.foo2.html' '//pre' 'pub const fn foo2() -> u32'
23+
// @!has - '//span[@class="since"]'
1624
#[unstable(feature = "humans", issue = "none")]
1725
pub const fn foo2() -> u32 { 42 }
1826

@@ -22,7 +30,9 @@ pub const fn foo2() -> u32 { 42 }
2230
#[rustc_const_stable(feature = "rust1", since = "1.0.0")]
2331
pub const fn bar2() -> u32 { 42 }
2432

33+
2534
// @has 'foo/fn.foo2_gated.html' '//pre' 'pub const unsafe fn foo2_gated() -> u32'
35+
// @!has - '//span[@class="since"]'
2636
#[unstable(feature = "foo2", issue = "none")]
2737
pub const unsafe fn foo2_gated() -> u32 { 42 }
2838

@@ -33,15 +43,23 @@ pub const unsafe fn foo2_gated() -> u32 { 42 }
3343
pub const unsafe fn bar2_gated() -> u32 { 42 }
3444

3545
// @has 'foo/fn.bar_not_gated.html' '//pre' 'pub const unsafe fn bar_not_gated() -> u32'
46+
// @!has - '//span[@class="since"]'
3647
pub const unsafe fn bar_not_gated() -> u32 { 42 }
3748

3849
pub struct Foo;
3950

4051
impl Foo {
41-
// @has 'foo/struct.Foo.html' '//div[@id="method.gated"]/code' 'pub unsafe fn gated() -> u32'
52+
// @has 'foo/struct.Foo.html' '//div[@id="method.gated"]/code' 'pub fn gated() -> u32'
53+
// @has - '//span[@class="since"]' '1.0.0 (const: unstable)'
54+
#[stable(feature = "rust1", since = "1.0.0")]
55+
#[rustc_const_unstable(feature="foo", issue = "none")]
56+
pub const fn gated() -> u32 { 42 }
57+
58+
// @has 'foo/struct.Foo.html' '//div[@id="method.gated_unsafe"]/code' 'pub unsafe fn gated_unsafe() -> u32'
59+
// @has - '//span[@class="since"]' '1.0.0 (const: unstable)'
4260
#[stable(feature = "rust1", since = "1.0.0")]
4361
#[rustc_const_unstable(feature="foo", issue = "none")]
44-
pub const unsafe fn gated() -> u32 { 42 }
62+
pub const unsafe fn gated_unsafe() -> u32 { 42 }
4563

4664
// @has 'foo/struct.Foo.html' '//div[@id="method.stable_impl"]/code' 'pub const fn stable_impl() -> u32'
4765
// @has - '//span[@class="since"]' '1.0.0 (const: 1.2.0)'

0 commit comments

Comments
 (0)