Skip to content

Commit 475f20c

Browse files
varkoryodaldevoid
andcommitted
Add Const kind to rustdoc
Co-Authored-By: Gabriel Smith <[email protected]>
1 parent 9a5f7b1 commit 475f20c

File tree

5 files changed

+75
-6
lines changed

5 files changed

+75
-6
lines changed

src/librustdoc/clean/auto_trait.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
773773
}
774774
}
775775
GenericParamDefKind::Lifetime => {}
776+
GenericParamDefKind::Const { .. } => {}
776777
}
777778
}
778779

src/librustdoc/clean/mod.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,15 @@ impl Clean<Lifetime> for hir::GenericParam {
12531253
}
12541254
}
12551255

1256+
impl Clean<Constant> for hir::ConstArg {
1257+
fn clean(&self, cx: &DocContext) -> Constant {
1258+
Constant {
1259+
type_: cx.tcx.type_of(cx.tcx.hir().body_owner_def_id(self.value.body)).clean(cx),
1260+
expr: print_const_expr(cx, self.value.body),
1261+
}
1262+
}
1263+
}
1264+
12561265
impl<'tcx> Clean<Lifetime> for ty::GenericParamDef {
12571266
fn clean(&self, _cx: &DocContext) -> Lifetime {
12581267
Lifetime(self.name.to_string())
@@ -1418,6 +1427,10 @@ pub enum GenericParamDefKind {
14181427
default: Option<Type>,
14191428
synthetic: Option<hir::SyntheticTyParamKind>,
14201429
},
1430+
Const {
1431+
did: DefId,
1432+
ty: Type,
1433+
},
14211434
}
14221435

14231436
#[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)]
@@ -1430,7 +1443,10 @@ pub struct GenericParamDef {
14301443
impl GenericParamDef {
14311444
pub fn is_synthetic_type_param(&self) -> bool {
14321445
match self.kind {
1433-
GenericParamDefKind::Lifetime => false,
1446+
GenericParamDefKind::Lifetime |
1447+
GenericParamDefKind::Const { .. } => {
1448+
false
1449+
}
14341450
GenericParamDefKind::Type { ref synthetic, .. } => synthetic.is_some(),
14351451
}
14361452
}
@@ -1494,6 +1510,12 @@ impl Clean<GenericParamDef> for hir::GenericParam {
14941510
synthetic: synthetic,
14951511
})
14961512
}
1513+
hir::GenericParamKind::Const { ref ty } => {
1514+
(self.name.ident().name.clean(cx), GenericParamDefKind::Const {
1515+
did: cx.tcx.hir().local_def_id(self.id),
1516+
ty: ty.clean(cx),
1517+
})
1518+
}
14971519
};
14981520

14991521
GenericParamDef {
@@ -1533,6 +1555,7 @@ impl Clean<Generics> for hir::Generics {
15331555
GenericParamDefKind::Type { did, ref bounds, .. } => {
15341556
cx.impl_trait_bounds.borrow_mut().insert(did, bounds.clone());
15351557
}
1558+
GenericParamDefKind::Const { .. } => unreachable!(),
15361559
}
15371560
param
15381561
})
@@ -1566,6 +1589,7 @@ impl Clean<Generics> for hir::Generics {
15661589
break
15671590
}
15681591
}
1592+
GenericParamDefKind::Const { .. } => {}
15691593
}
15701594
}
15711595
}
@@ -2544,6 +2568,7 @@ impl Clean<Type> for hir::Ty {
25442568
let provided_params = &path.segments.last().expect("segments were empty");
25452569
let mut ty_substs = FxHashMap::default();
25462570
let mut lt_substs = FxHashMap::default();
2571+
let mut const_substs = FxHashMap::default();
25472572
provided_params.with_generic_args(|generic_args| {
25482573
let mut indices: GenericParamCount = Default::default();
25492574
for param in generics.params.iter() {
@@ -2595,10 +2620,32 @@ impl Clean<Type> for hir::Ty {
25952620
}
25962621
indices.types += 1;
25972622
}
2623+
hir::GenericParamKind::Const { .. } => {
2624+
let const_param_def =
2625+
Def::ConstParam(cx.tcx.hir().local_def_id(param.id));
2626+
let mut j = 0;
2627+
let const_ = generic_args.args.iter().find_map(|arg| {
2628+
match arg {
2629+
hir::GenericArg::Const(ct) => {
2630+
if indices.consts == j {
2631+
return Some(ct);
2632+
}
2633+
j += 1;
2634+
None
2635+
}
2636+
_ => None,
2637+
}
2638+
});
2639+
if let Some(ct) = const_.cloned() {
2640+
const_substs.insert(const_param_def, ct.clean(cx));
2641+
}
2642+
// FIXME(const_generics:defaults)
2643+
indices.consts += 1;
2644+
}
25982645
}
25992646
}
26002647
});
2601-
return cx.enter_alias(ty_substs, lt_substs, || ty.clean(cx));
2648+
return cx.enter_alias(ty_substs, lt_substs, const_substs, || ty.clean(cx));
26022649
}
26032650
resolve_type(cx, path.clean(cx), self.id)
26042651
}
@@ -3190,6 +3237,9 @@ impl Clean<GenericArgs> for hir::GenericArgs {
31903237
GenericArg::Type(ty) => {
31913238
types.push(ty.clean(cx));
31923239
}
3240+
GenericArg::Const(..) => {
3241+
unimplemented!() // FIXME(const_generics)
3242+
}
31933243
}
31943244
}
31953245
GenericArgs::AngleBracketed {

src/librustdoc/core.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ pub struct DocContext<'a, 'tcx: 'a, 'rcx: 'a> {
6565
pub ty_substs: RefCell<FxHashMap<Def, clean::Type>>,
6666
/// Table `NodeId` of lifetime parameter definition -> substituted lifetime
6767
pub lt_substs: RefCell<FxHashMap<DefId, clean::Lifetime>>,
68+
/// Table node id of const parameter definition -> substituted const
69+
pub ct_substs: RefCell<FxHashMap<Def, clean::Constant>>,
6870
/// Table DefId of `impl Trait` in argument position -> bounds
6971
pub impl_trait_bounds: RefCell<FxHashMap<DefId, Vec<clean::GenericBound>>>,
7072
pub send_trait: Option<DefId>,
@@ -85,14 +87,18 @@ impl<'a, 'tcx, 'rcx> DocContext<'a, 'tcx, 'rcx> {
8587
pub fn enter_alias<F, R>(&self,
8688
ty_substs: FxHashMap<Def, clean::Type>,
8789
lt_substs: FxHashMap<DefId, clean::Lifetime>,
90+
ct_substs: FxHashMap<Def, clean::Constant>,
8891
f: F) -> R
8992
where F: FnOnce() -> R {
90-
let (old_tys, old_lts) =
91-
(mem::replace(&mut *self.ty_substs.borrow_mut(), ty_substs),
92-
mem::replace(&mut *self.lt_substs.borrow_mut(), lt_substs));
93+
let (old_tys, old_lts, old_cts) = (
94+
mem::replace(&mut *self.ty_substs.borrow_mut(), ty_substs),
95+
mem::replace(&mut *self.lt_substs.borrow_mut(), lt_substs),
96+
mem::replace(&mut *self.ct_substs.borrow_mut(), ct_substs),
97+
);
9398
let r = f();
9499
*self.ty_substs.borrow_mut() = old_tys;
95100
*self.lt_substs.borrow_mut() = old_lts;
101+
*self.ct_substs.borrow_mut() = old_cts;
96102
r
97103
}
98104

@@ -527,6 +533,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
527533
renderinfo: RefCell::new(renderinfo),
528534
ty_substs: Default::default(),
529535
lt_substs: Default::default(),
536+
ct_substs: Default::default(),
530537
impl_trait_bounds: Default::default(),
531538
send_trait: send_trait,
532539
fake_def_ids: Default::default(),

src/librustdoc/html/format.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,16 @@ impl fmt::Display for clean::GenericParamDef {
138138

139139
Ok(())
140140
}
141+
clean::GenericParamDefKind::Const { ref ty, .. } => {
142+
f.write_str("const ")?;
143+
f.write_str(&self.name)?;
144+
145+
if f.alternate() {
146+
write!(f, ": {:#}", ty)
147+
} else {
148+
write!(f, ":&nbsp;{}", ty)
149+
}
150+
}
141151
}
142152
}
143153
}

src/librustdoc/html/render.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,8 @@ impl<'a> Cache {
17461746
for param in &generics.params {
17471747
match param.kind {
17481748
clean::GenericParamDefKind::Lifetime => {}
1749-
clean::GenericParamDefKind::Type { did, .. } => {
1749+
clean::GenericParamDefKind::Type { did, .. } |
1750+
clean::GenericParamDefKind::Const { did, .. } => {
17501751
self.typarams.insert(did, param.name.clone());
17511752
}
17521753
}

0 commit comments

Comments
 (0)