From 475f20c73db54517255416b66db2edb304e85729 Mon Sep 17 00:00:00 2001 From: varkor Date: Fri, 15 Feb 2019 22:24:00 +0000 Subject: [PATCH] Add Const kind to rustdoc Co-Authored-By: Gabriel Smith --- src/librustdoc/clean/auto_trait.rs | 1 + src/librustdoc/clean/mod.rs | 54 ++++++++++++++++++++++++++++-- src/librustdoc/core.rs | 13 +++++-- src/librustdoc/html/format.rs | 10 ++++++ src/librustdoc/html/render.rs | 3 +- 5 files changed, 75 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/clean/auto_trait.rs b/src/librustdoc/clean/auto_trait.rs index b99181c0d4f..8796cfa01e0 100644 --- a/src/librustdoc/clean/auto_trait.rs +++ b/src/librustdoc/clean/auto_trait.rs @@ -773,6 +773,7 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> { } } GenericParamDefKind::Lifetime => {} + GenericParamDefKind::Const { .. } => {} } } diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index d12b5021ca9..b08646ec004 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1253,6 +1253,15 @@ impl Clean for hir::GenericParam { } } +impl Clean for hir::ConstArg { + fn clean(&self, cx: &DocContext) -> Constant { + Constant { + type_: cx.tcx.type_of(cx.tcx.hir().body_owner_def_id(self.value.body)).clean(cx), + expr: print_const_expr(cx, self.value.body), + } + } +} + impl<'tcx> Clean for ty::GenericParamDef { fn clean(&self, _cx: &DocContext) -> Lifetime { Lifetime(self.name.to_string()) @@ -1418,6 +1427,10 @@ pub enum GenericParamDefKind { default: Option, synthetic: Option, }, + Const { + did: DefId, + ty: Type, + }, } #[derive(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Debug, Hash)] @@ -1430,7 +1443,10 @@ pub struct GenericParamDef { impl GenericParamDef { pub fn is_synthetic_type_param(&self) -> bool { match self.kind { - GenericParamDefKind::Lifetime => false, + GenericParamDefKind::Lifetime | + GenericParamDefKind::Const { .. } => { + false + } GenericParamDefKind::Type { ref synthetic, .. } => synthetic.is_some(), } } @@ -1494,6 +1510,12 @@ impl Clean for hir::GenericParam { synthetic: synthetic, }) } + hir::GenericParamKind::Const { ref ty } => { + (self.name.ident().name.clean(cx), GenericParamDefKind::Const { + did: cx.tcx.hir().local_def_id(self.id), + ty: ty.clean(cx), + }) + } }; GenericParamDef { @@ -1533,6 +1555,7 @@ impl Clean for hir::Generics { GenericParamDefKind::Type { did, ref bounds, .. } => { cx.impl_trait_bounds.borrow_mut().insert(did, bounds.clone()); } + GenericParamDefKind::Const { .. } => unreachable!(), } param }) @@ -1566,6 +1589,7 @@ impl Clean for hir::Generics { break } } + GenericParamDefKind::Const { .. } => {} } } } @@ -2544,6 +2568,7 @@ impl Clean for hir::Ty { let provided_params = &path.segments.last().expect("segments were empty"); let mut ty_substs = FxHashMap::default(); let mut lt_substs = FxHashMap::default(); + let mut const_substs = FxHashMap::default(); provided_params.with_generic_args(|generic_args| { let mut indices: GenericParamCount = Default::default(); for param in generics.params.iter() { @@ -2595,10 +2620,32 @@ impl Clean for hir::Ty { } indices.types += 1; } + hir::GenericParamKind::Const { .. } => { + let const_param_def = + Def::ConstParam(cx.tcx.hir().local_def_id(param.id)); + let mut j = 0; + let const_ = generic_args.args.iter().find_map(|arg| { + match arg { + hir::GenericArg::Const(ct) => { + if indices.consts == j { + return Some(ct); + } + j += 1; + None + } + _ => None, + } + }); + if let Some(ct) = const_.cloned() { + const_substs.insert(const_param_def, ct.clean(cx)); + } + // FIXME(const_generics:defaults) + indices.consts += 1; + } } } }); - return cx.enter_alias(ty_substs, lt_substs, || ty.clean(cx)); + return cx.enter_alias(ty_substs, lt_substs, const_substs, || ty.clean(cx)); } resolve_type(cx, path.clean(cx), self.id) } @@ -3190,6 +3237,9 @@ impl Clean for hir::GenericArgs { GenericArg::Type(ty) => { types.push(ty.clean(cx)); } + GenericArg::Const(..) => { + unimplemented!() // FIXME(const_generics) + } } } GenericArgs::AngleBracketed { diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index e90127ca162..5df82c7cc9e 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -65,6 +65,8 @@ pub struct DocContext<'a, 'tcx: 'a, 'rcx: 'a> { pub ty_substs: RefCell>, /// Table `NodeId` of lifetime parameter definition -> substituted lifetime pub lt_substs: RefCell>, + /// Table node id of const parameter definition -> substituted const + pub ct_substs: RefCell>, /// Table DefId of `impl Trait` in argument position -> bounds pub impl_trait_bounds: RefCell>>, pub send_trait: Option, @@ -85,14 +87,18 @@ impl<'a, 'tcx, 'rcx> DocContext<'a, 'tcx, 'rcx> { pub fn enter_alias(&self, ty_substs: FxHashMap, lt_substs: FxHashMap, + ct_substs: FxHashMap, f: F) -> R where F: FnOnce() -> R { - let (old_tys, old_lts) = - (mem::replace(&mut *self.ty_substs.borrow_mut(), ty_substs), - mem::replace(&mut *self.lt_substs.borrow_mut(), lt_substs)); + let (old_tys, old_lts, old_cts) = ( + mem::replace(&mut *self.ty_substs.borrow_mut(), ty_substs), + mem::replace(&mut *self.lt_substs.borrow_mut(), lt_substs), + mem::replace(&mut *self.ct_substs.borrow_mut(), ct_substs), + ); let r = f(); *self.ty_substs.borrow_mut() = old_tys; *self.lt_substs.borrow_mut() = old_lts; + *self.ct_substs.borrow_mut() = old_cts; r } @@ -527,6 +533,7 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt renderinfo: RefCell::new(renderinfo), ty_substs: Default::default(), lt_substs: Default::default(), + ct_substs: Default::default(), impl_trait_bounds: Default::default(), send_trait: send_trait, fake_def_ids: Default::default(), diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 35555f61bd1..4fbbaf0f2e1 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -138,6 +138,16 @@ impl fmt::Display for clean::GenericParamDef { Ok(()) } + clean::GenericParamDefKind::Const { ref ty, .. } => { + f.write_str("const ")?; + f.write_str(&self.name)?; + + if f.alternate() { + write!(f, ": {:#}", ty) + } else { + write!(f, ": {}", ty) + } + } } } } diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index e660d27d74b..bf0757902fe 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -1746,7 +1746,8 @@ impl<'a> Cache { for param in &generics.params { match param.kind { clean::GenericParamDefKind::Lifetime => {} - clean::GenericParamDefKind::Type { did, .. } => { + clean::GenericParamDefKind::Type { did, .. } | + clean::GenericParamDefKind::Const { did, .. } => { self.typarams.insert(did, param.name.clone()); } }