Skip to content

Commit 1275cc1

Browse files
committed
Auto merge of #74936 - GuillaumeGomez:const-rustc_const_unstable, r=jyn514
Don't print "const" keyword on non-nightly build if rustc_const_unstable is used on the item Fixes #74579.
2 parents 08324fe + 2a281e0 commit 1275cc1

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/librustdoc/clean/mod.rs

+23-5
Original file line numberDiff line numberDiff line change
@@ -1083,25 +1083,37 @@ impl Clean<TypeKind> for hir::def::DefKind {
10831083

10841084
impl Clean<Item> for hir::TraitItem<'_> {
10851085
fn clean(&self, cx: &DocContext<'_>) -> Item {
1086+
let local_did = cx.tcx.hir().local_def_id(self.hir_id);
10861087
let inner = match self.kind {
10871088
hir::TraitItemKind::Const(ref ty, default) => {
10881089
AssocConstItem(ty.clean(cx), default.map(|e| print_const_expr(cx, e)))
10891090
}
10901091
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
1091-
MethodItem((sig, &self.generics, body, None).clean(cx))
1092+
let mut m = (sig, &self.generics, body, None).clean(cx);
1093+
if m.header.constness == hir::Constness::Const
1094+
&& !is_min_const_fn(cx.tcx, local_did.to_def_id())
1095+
{
1096+
m.header.constness = hir::Constness::NotConst;
1097+
}
1098+
MethodItem(m)
10921099
}
10931100
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(ref names)) => {
10941101
let (generics, decl) = enter_impl_trait(cx, || {
10951102
(self.generics.clean(cx), (&*sig.decl, &names[..]).clean(cx))
10961103
});
10971104
let (all_types, ret_types) = get_all_types(&generics, &decl, cx);
1098-
TyMethodItem(TyMethod { header: sig.header, decl, generics, all_types, ret_types })
1105+
let mut t = TyMethod { header: sig.header, decl, generics, all_types, ret_types };
1106+
if t.header.constness == hir::Constness::Const
1107+
&& !is_min_const_fn(cx.tcx, local_did.to_def_id())
1108+
{
1109+
t.header.constness = hir::Constness::NotConst;
1110+
}
1111+
TyMethodItem(t)
10991112
}
11001113
hir::TraitItemKind::Type(ref bounds, ref default) => {
11011114
AssocTypeItem(bounds.clean(cx), default.clean(cx))
11021115
}
11031116
};
1104-
let local_did = cx.tcx.hir().local_def_id(self.hir_id);
11051117
Item {
11061118
name: Some(self.ident.name.clean(cx)),
11071119
attrs: self.attrs.clean(cx),
@@ -1117,20 +1129,26 @@ impl Clean<Item> for hir::TraitItem<'_> {
11171129

11181130
impl Clean<Item> for hir::ImplItem<'_> {
11191131
fn clean(&self, cx: &DocContext<'_>) -> Item {
1132+
let local_did = cx.tcx.hir().local_def_id(self.hir_id);
11201133
let inner = match self.kind {
11211134
hir::ImplItemKind::Const(ref ty, expr) => {
11221135
AssocConstItem(ty.clean(cx), Some(print_const_expr(cx, expr)))
11231136
}
11241137
hir::ImplItemKind::Fn(ref sig, body) => {
1125-
MethodItem((sig, &self.generics, body, Some(self.defaultness)).clean(cx))
1138+
let mut m = (sig, &self.generics, body, Some(self.defaultness)).clean(cx);
1139+
if m.header.constness == hir::Constness::Const
1140+
&& !is_min_const_fn(cx.tcx, local_did.to_def_id())
1141+
{
1142+
m.header.constness = hir::Constness::NotConst;
1143+
}
1144+
MethodItem(m)
11261145
}
11271146
hir::ImplItemKind::TyAlias(ref ty) => {
11281147
let type_ = ty.clean(cx);
11291148
let item_type = type_.def_id().and_then(|did| inline::build_ty(cx, did));
11301149
TypedefItem(Typedef { type_, generics: Generics::default(), item_type }, true)
11311150
}
11321151
};
1133-
let local_did = cx.tcx.hir().local_def_id(self.hir_id);
11341152
Item {
11351153
name: Some(self.ident.name.clean(cx)),
11361154
source: self.span.clean(cx),

src/test/rustdoc/const-display.rs

+9
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,12 @@ pub const unsafe fn bar2_gated() -> u32 { 42 }
3232

3333
// @has 'foo/fn.bar_not_gated.html' '//pre' 'pub unsafe fn bar_not_gated() -> u32'
3434
pub const unsafe fn bar_not_gated() -> u32 { 42 }
35+
36+
pub struct Foo;
37+
38+
impl Foo {
39+
// @has 'foo/struct.Foo.html' '//h4[@id="method.gated"]/code' 'pub unsafe fn gated() -> u32'
40+
#[stable(feature = "rust1", since = "1.0.0")]
41+
#[rustc_const_unstable(feature="foo", issue = "none")]
42+
pub const unsafe fn gated() -> u32 { 42 }
43+
}

0 commit comments

Comments
 (0)