Skip to content

Commit 01fe1c0

Browse files
authored
Rollup merge of #143381 - fee1-dead-contrib:push-pzxuvlnymxpu, r=GuillaumeGomez
rustdoc: don't treat methods under const impls or traits as const Fixes #143071
2 parents 05f5690 + 510e5d7 commit 01fe1c0

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/librustdoc/clean/types.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,20 @@ impl Item {
647647
) -> hir::FnHeader {
648648
let sig = tcx.fn_sig(def_id).skip_binder();
649649
let constness = if tcx.is_const_fn(def_id) {
650-
hir::Constness::Const
650+
// rustc's `is_const_fn` returns `true` for associated functions that have an `impl const` parent
651+
// or that have a `#[const_trait]` parent. Do not display those as `const` in rustdoc because we
652+
// won't be printing correct syntax plus the syntax is unstable.
653+
match tcx.opt_associated_item(def_id) {
654+
Some(ty::AssocItem {
655+
container: ty::AssocItemContainer::Impl,
656+
trait_item_def_id: Some(_),
657+
..
658+
})
659+
| Some(ty::AssocItem { container: ty::AssocItemContainer::Trait, .. }) => {
660+
hir::Constness::NotConst
661+
}
662+
None | Some(_) => hir::Constness::Const,
663+
}
651664
} else {
652665
hir::Constness::NotConst
653666
};
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// check that we don't render `#[const_trait]` methods as `const` - even for
2+
// const `trait`s and `impl`s.
3+
#![crate_name = "foo"]
4+
#![feature(const_trait_impl)]
5+
6+
//@ has foo/trait.Tr.html
7+
//@ has - '//*[@id="tymethod.required"]' 'fn required()'
8+
//@ !has - '//*[@id="tymethod.required"]' 'const'
9+
//@ has - '//*[@id="method.defaulted"]' 'fn defaulted()'
10+
//@ !has - '//*[@id="method.defaulted"]' 'const'
11+
#[const_trait]
12+
pub trait Tr {
13+
fn required();
14+
fn defaulted() {}
15+
}
16+
17+
pub struct ConstImpl {}
18+
pub struct NonConstImpl {}
19+
20+
//@ has foo/struct.ConstImpl.html
21+
//@ has - '//*[@id="method.required"]' 'fn required()'
22+
//@ !has - '//*[@id="method.required"]' 'const'
23+
//@ has - '//*[@id="method.defaulted"]' 'fn defaulted()'
24+
//@ !has - '//*[@id="method.defaulted"]' 'const'
25+
impl const Tr for ConstImpl {
26+
fn required() {}
27+
}
28+
29+
//@ has foo/struct.NonConstImpl.html
30+
//@ has - '//*[@id="method.required"]' 'fn required()'
31+
//@ !has - '//*[@id="method.required"]' 'const'
32+
//@ has - '//*[@id="method.defaulted"]' 'fn defaulted()'
33+
//@ !has - '//*[@id="method.defaulted"]' 'const'
34+
impl Tr for NonConstImpl {
35+
fn required() {}
36+
}

0 commit comments

Comments
 (0)