Skip to content

Commit 2ecc657

Browse files
authored
Rollup merge of rust-lang#89954 - GuillaumeGomez:legacy-const-generic-doc, r=Amanieu
Fix legacy_const_generic doc arguments display Fixes rust-lang#83167. cc `@Amanieu`
2 parents 18bb8c6 + 5c75a48 commit 2ecc657

File tree

4 files changed

+57
-1
lines changed

4 files changed

+57
-1
lines changed

src/librustdoc/clean/mod.rs

+34-1
Original file line numberDiff line numberDiff line change
@@ -749,11 +749,42 @@ fn clean_fn_or_proc_macro(
749749
} else {
750750
hir::Constness::NotConst
751751
};
752+
clean_fn_decl_legacy_const_generics(&mut func, attrs);
752753
FunctionItem(func)
753754
}
754755
}
755756
}
756757

758+
/// This is needed to make it more "readable" when documenting functions using
759+
/// `rustc_legacy_const_generics`. More information in
760+
/// <https://github.com/rust-lang/rust/issues/83167>.
761+
fn clean_fn_decl_legacy_const_generics(func: &mut Function, attrs: &[ast::Attribute]) {
762+
for meta_item_list in attrs
763+
.iter()
764+
.filter(|a| a.has_name(sym::rustc_legacy_const_generics))
765+
.filter_map(|a| a.meta_item_list())
766+
{
767+
for (pos, literal) in meta_item_list.iter().filter_map(|meta| meta.literal()).enumerate() {
768+
match literal.kind {
769+
ast::LitKind::Int(a, _) => {
770+
let gen = func.generics.params.remove(0);
771+
if let GenericParamDef { name, kind: GenericParamDefKind::Const { ty, .. } } =
772+
gen
773+
{
774+
func.decl
775+
.inputs
776+
.values
777+
.insert(a as _, Argument { name, type_: *ty, is_const: true });
778+
} else {
779+
panic!("unexpected non const in position {}", pos);
780+
}
781+
}
782+
_ => panic!("invalid arg index"),
783+
}
784+
}
785+
}
786+
}
787+
757788
impl<'a> Clean<Function> for (&'a hir::FnSig<'a>, &'a hir::Generics<'a>, hir::BodyId) {
758789
fn clean(&self, cx: &mut DocContext<'_>) -> Function {
759790
let (generics, decl) = enter_impl_trait(cx, |cx| {
@@ -779,7 +810,7 @@ impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], &'a [Ident]) {
779810
if name.is_empty() {
780811
name = kw::Underscore;
781812
}
782-
Argument { name, type_: ty.clean(cx) }
813+
Argument { name, type_: ty.clean(cx), is_const: false }
783814
})
784815
.collect(),
785816
}
@@ -798,6 +829,7 @@ impl<'a> Clean<Arguments> for (&'a [hir::Ty<'a>], hir::BodyId) {
798829
.map(|(i, ty)| Argument {
799830
name: name_from_pat(body.params[i].pat),
800831
type_: ty.clean(cx),
832+
is_const: false,
801833
})
802834
.collect(),
803835
}
@@ -828,6 +860,7 @@ impl<'tcx> Clean<FnDecl> for (DefId, ty::PolyFnSig<'tcx>) {
828860
.map(|t| Argument {
829861
type_: t.clean(cx),
830862
name: names.next().map_or(kw::Empty, |i| i.name),
863+
is_const: false,
831864
})
832865
.collect(),
833866
},

src/librustdoc/clean/types.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,9 @@ crate struct Arguments {
13531353
crate struct Argument {
13541354
crate type_: Type,
13551355
crate name: Symbol,
1356+
/// This field is used to represent "const" arguments from the `rustc_legacy_const_generics`
1357+
/// feature. More information in <https://github.com/rust-lang/rust/issues/83167>.
1358+
crate is_const: bool,
13561359
}
13571360

13581361
#[derive(Clone, PartialEq, Debug)]

src/librustdoc/html/format.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1177,6 +1177,10 @@ impl clean::FnDecl {
11771177
args.push_str(" <br>");
11781178
args_plain.push(' ');
11791179
}
1180+
if input.is_const {
1181+
args.push_str("const ");
1182+
args_plain.push_str("const ");
1183+
}
11801184
if !input.name.is_empty() {
11811185
args.push_str(&format!("{}: ", input.name));
11821186
args_plain.push_str(&format!("{}: ", input.name));
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![crate_name = "foo"]
2+
#![feature(rustc_attrs)]
3+
4+
// @has 'foo/fn.foo.html'
5+
// @has - '//*[@class="rust fn"]' 'fn foo(x: usize, const Y: usize, z: usize) -> [usize; 3]'
6+
#[rustc_legacy_const_generics(1)]
7+
pub fn foo<const Y: usize>(x: usize, z: usize) -> [usize; 3] {
8+
[x, Y, z]
9+
}
10+
11+
// @has 'foo/fn.bar.html'
12+
// @has - '//*[@class="rust fn"]' 'fn bar(x: usize, const Y: usize, const Z: usize) -> [usize; 3]'
13+
#[rustc_legacy_const_generics(1, 2)]
14+
pub fn bar<const Y: usize, const Z: usize>(x: usize) -> [usize; 3] {
15+
[x, Y, z]
16+
}

0 commit comments

Comments
 (0)