Skip to content

Commit 5fb27bc

Browse files
committed
Check for const_unstable before printing const
1 parent 9c495b3 commit 5fb27bc

File tree

4 files changed

+65
-44
lines changed

4 files changed

+65
-44
lines changed

src/librustdoc/html/format.rs

+17-9
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 no stability attribute
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+
(hir::Constness::Const, _) | (hir::Constness::NotConst, _) => "",
1288+
}
1289+
}
1290+
12831291
impl clean::Import {
12841292
crate fn print<'a, 'tcx: 'a>(
12851293
&'a self,

src/librustdoc/html/render/mod.rs

+17-13
Original file line numberDiff line numberDiff line change
@@ -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

@@ -833,16 +833,17 @@ fn render_stability_since_raw(
833833
let ver = ver.filter(|inner| !inner.is_empty());
834834

835835
match (ver, const_stability) {
836+
// stable and const stable
836837
(Some(v), Some(ConstStability { level: StabilityLevel::Stable { since }, .. }))
837838
if Some(since.as_str()).as_deref() != containing_const_ver =>
838839
{
839840
write!(
840841
w,
841842
"<span class=\"since\" title=\"Stable since Rust version {0}, const since {1}\">{0} (const: {1})</span>",
842-
v,
843-
since.as_str()
843+
v, since
844844
);
845845
}
846+
// stable and const unstable
846847
(
847848
Some(v),
848849
Some(ConstStability { level: StabilityLevel::Unstable { issue, .. }, feature, .. }),
@@ -863,6 +864,7 @@ fn render_stability_since_raw(
863864
}
864865
write!(w, ")</span>");
865866
}
867+
// stable
866868
(Some(v), _) if ver != containing_ver => {
867869
write!(
868870
w,
@@ -910,11 +912,13 @@ fn render_assoc_item(
910912
}
911913
};
912914
let vis = meth.visibility.print_with_space(meth.def_id, cx).to_string();
913-
let constness = header.constness.print_with_space();
915+
let constness =
916+
print_constness_with_space(&header.constness, meth.const_stability(cx.tcx()));
914917
let asyncness = header.asyncness.print_with_space();
915918
let unsafety = header.unsafety.print_with_space();
916919
let defaultness = print_default_space(meth.is_default());
917920
let abi = print_abi_with_space(header.abi).to_string();
921+
918922
// NOTE: `{:#}` does not print HTML formatting, `{}` does. So `g.print` can't be reused between the length calculation and `write!`.
919923
let generics_len = format!("{:#}", g.print(cx)).len();
920924
let mut header_len = "fn ".len()
@@ -939,15 +943,15 @@ fn render_assoc_item(
939943
w.reserve(header_len + "<a href=\"\" class=\"fnname\">{".len() + "</a>".len());
940944
write!(
941945
w,
942-
"{}{}{}{}{}{}{}fn <a href=\"{href}\" class=\"fnname\">{name}</a>\
946+
"{indent}{vis}{constness}{asyncness}{unsafety}{defaultness}{abi}fn <a href=\"{href}\" class=\"fnname\">{name}</a>\
943947
{generics}{decl}{notable_traits}{where_clause}",
944-
indent_str,
945-
vis,
946-
constness,
947-
asyncness,
948-
unsafety,
949-
defaultness,
950-
abi,
948+
indent = indent_str,
949+
vis = vis,
950+
constness = constness,
951+
asyncness = asyncness,
952+
unsafety = unsafety,
953+
defaultness = defaultness,
954+
abi = abi,
951955
href = href,
952956
name = name,
953957
generics = g.print(cx),

src/librustdoc/html/render/print_item.rs

+27-18
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;
@@ -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),

src/test/rustdoc/const-display.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
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'
1111
// @has - '//span[@class="since"]' '1.0.0 (const: unstable)'
1212
#[stable(feature = "rust1", since = "1.0.0")]
1313
#[rustc_const_unstable(feature="foo", issue = "none")]
14-
pub const unsafe fn foo() -> u32 { 42 }
14+
pub const fn foo() -> u32 { 42 }
1515

1616
// @has 'foo/fn.foo2.html' '//pre' 'pub const fn foo2() -> u32'
1717
#[unstable(feature = "humans", issue = "none")]
@@ -39,11 +39,11 @@ pub const unsafe fn bar_not_gated() -> u32 { 42 }
3939
pub struct Foo;
4040

4141
impl Foo {
42-
// @has 'foo/struct.Foo.html' '//div[@id="method.gated"]/code' 'pub unsafe fn gated() -> u32'
42+
// @has 'foo/struct.Foo.html' '//div[@id="method.gated"]/code' 'pub fn gated() -> u32'
4343
// @has - '//span[@class="since"]' '1.0.0 (const: unstable)'
4444
#[stable(feature = "rust1", since = "1.0.0")]
4545
#[rustc_const_unstable(feature="foo", issue = "none")]
46-
pub const unsafe fn gated() -> u32 { 42 }
46+
pub const fn gated() -> u32 { 42 }
4747

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

0 commit comments

Comments
 (0)