Skip to content

Commit 606feba

Browse files
authored
Rollup merge of #85957 - BoxyUwU:rustdoc-const-generic-defaults, r=oli-obk
Display defaults on const params- rustdoc previously rustdoc would render this struct declaration: `pub struct Foo<const N: usize = 10>;` as: `pub struct Foo<const N: usize>;` this PR changes it to render correctly
2 parents 3bc8221 + f7117f8 commit 606feba

File tree

8 files changed

+39
-10
lines changed

8 files changed

+39
-10
lines changed

src/librustdoc/clean/auto_trait.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,10 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
664664
}
665665
}
666666
GenericParamDefKind::Lifetime => {}
667-
GenericParamDefKind::Const { .. } => {}
667+
GenericParamDefKind::Const { ref mut default, .. } => {
668+
// We never want something like `impl<const N: usize = 10>`
669+
default.take();
670+
}
668671
}
669672
}
670673

src/librustdoc/clean/mod.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -445,11 +445,15 @@ impl Clean<GenericParamDef> for ty::GenericParamDef {
445445
},
446446
)
447447
}
448-
ty::GenericParamDefKind::Const { .. } => (
448+
ty::GenericParamDefKind::Const { has_default, .. } => (
449449
self.name,
450450
GenericParamDefKind::Const {
451451
did: self.def_id,
452452
ty: cx.tcx.type_of(self.def_id).clean(cx),
453+
default: match has_default {
454+
true => Some(cx.tcx.const_param_default(self.def_id).to_string()),
455+
false => None,
456+
},
453457
},
454458
),
455459
};
@@ -487,12 +491,15 @@ impl Clean<GenericParamDef> for hir::GenericParam<'_> {
487491
synthetic,
488492
},
489493
),
490-
hir::GenericParamKind::Const { ref ty, default: _ } => (
494+
hir::GenericParamKind::Const { ref ty, default } => (
491495
self.name.ident().name,
492496
GenericParamDefKind::Const {
493497
did: cx.tcx.hir().local_def_id(self.hir_id).to_def_id(),
494498
ty: ty.clean(cx),
495-
// FIXME(const_generics_defaults): add `default` field here for docs
499+
default: default.map(|ct| {
500+
let def_id = cx.tcx.hir().local_def_id(ct.hir_id);
501+
ty::Const::from_anon_const(cx.tcx, def_id).to_string()
502+
}),
496503
},
497504
),
498505
};

src/librustdoc/clean/types.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1220,6 +1220,7 @@ crate enum GenericParamDefKind {
12201220
Const {
12211221
did: DefId,
12221222
ty: Type,
1223+
default: Option<String>,
12231224
},
12241225
}
12251226

src/librustdoc/html/format.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,22 @@ impl clean::GenericParamDef {
177177

178178
Ok(())
179179
}
180-
clean::GenericParamDefKind::Const { ref ty, .. } => {
180+
clean::GenericParamDefKind::Const { ref ty, ref default, .. } => {
181181
if f.alternate() {
182-
write!(f, "const {}: {:#}", self.name, ty.print(cx))
182+
write!(f, "const {}: {:#}", self.name, ty.print(cx))?;
183183
} else {
184-
write!(f, "const {}:&nbsp;{}", self.name, ty.print(cx))
184+
write!(f, "const {}:&nbsp;{}", self.name, ty.print(cx))?;
185185
}
186+
187+
if let Some(default) = default {
188+
if f.alternate() {
189+
write!(f, " = {:#}", default)?;
190+
} else {
191+
write!(f, "&nbsp;=&nbsp;{}", default)?;
192+
}
193+
}
194+
195+
Ok(())
186196
}
187197
})
188198
}

src/librustdoc/json/conversions.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,9 @@ impl FromWithTcx<clean::GenericParamDefKind> for GenericParamDefKind {
317317
bounds: bounds.into_iter().map(|x| x.into_tcx(tcx)).collect(),
318318
default: default.map(|x| x.into_tcx(tcx)),
319319
},
320-
Const { did: _, ty } => GenericParamDefKind::Const(ty.into_tcx(tcx)),
320+
Const { did: _, ty, default } => {
321+
GenericParamDefKind::Const { ty: ty.into_tcx(tcx), default }
322+
}
321323
}
322324
}
323325
}

src/librustdoc/json/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
234234
)
235235
})
236236
.collect(),
237-
format_version: 5,
237+
format_version: 6,
238238
};
239239
let mut p = self.out_path.clone();
240240
p.push(output.index.get(&output.root).unwrap().name.clone().unwrap());

src/rustdoc-json-types/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ pub struct GenericParamDef {
324324
pub enum GenericParamDefKind {
325325
Lifetime,
326326
Type { bounds: Vec<GenericBound>, default: Option<Type> },
327-
Const(Type),
327+
Const { ty: Type, default: Option<String> },
328328
}
329329

330330
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![crate_name = "foo"]
2+
#![feature(const_generics_defaults)]
3+
4+
// @has foo/struct.Foo.html '//pre[@class="rust struct"]' \
5+
// 'pub struct Foo<const M: usize = 10_usize, const N: usize = M, T = i32>(_);'
6+
pub struct Foo<const M: usize = 10, const N: usize = M, T = i32>(T);

0 commit comments

Comments
 (0)