Add Const kind to rustdoc

Co-Authored-By: Gabriel Smith <yodaldevoid@users.noreply.github.com>
This commit is contained in:
varkor 2019-02-15 22:24:00 +00:00
parent 9a5f7b1eae
commit 475f20c73d
5 changed files with 75 additions and 6 deletions

View File

@ -773,6 +773,7 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
}
}
GenericParamDefKind::Lifetime => {}
GenericParamDefKind::Const { .. } => {}
}
}

View File

@ -1253,6 +1253,15 @@ impl Clean<Lifetime> for hir::GenericParam {
}
}
impl Clean<Constant> 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<Lifetime> for ty::GenericParamDef {
fn clean(&self, _cx: &DocContext) -> Lifetime {
Lifetime(self.name.to_string())
@ -1418,6 +1427,10 @@ pub enum GenericParamDefKind {
default: Option<Type>,
synthetic: Option<hir::SyntheticTyParamKind>,
},
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<GenericParamDef> 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<Generics> 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<Generics> for hir::Generics {
break
}
}
GenericParamDefKind::Const { .. } => {}
}
}
}
@ -2544,6 +2568,7 @@ impl Clean<Type> 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<Type> 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<GenericArgs> for hir::GenericArgs {
GenericArg::Type(ty) => {
types.push(ty.clean(cx));
}
GenericArg::Const(..) => {
unimplemented!() // FIXME(const_generics)
}
}
}
GenericArgs::AngleBracketed {

View File

@ -65,6 +65,8 @@ pub struct DocContext<'a, 'tcx: 'a, 'rcx: 'a> {
pub ty_substs: RefCell<FxHashMap<Def, clean::Type>>,
/// Table `NodeId` of lifetime parameter definition -> substituted lifetime
pub lt_substs: RefCell<FxHashMap<DefId, clean::Lifetime>>,
/// Table node id of const parameter definition -> substituted const
pub ct_substs: RefCell<FxHashMap<Def, clean::Constant>>,
/// Table DefId of `impl Trait` in argument position -> bounds
pub impl_trait_bounds: RefCell<FxHashMap<DefId, Vec<clean::GenericBound>>>,
pub send_trait: Option<DefId>,
@ -85,14 +87,18 @@ impl<'a, 'tcx, 'rcx> DocContext<'a, 'tcx, 'rcx> {
pub fn enter_alias<F, R>(&self,
ty_substs: FxHashMap<Def, clean::Type>,
lt_substs: FxHashMap<DefId, clean::Lifetime>,
ct_substs: FxHashMap<Def, clean::Constant>,
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(),

View File

@ -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, ":&nbsp;{}", ty)
}
}
}
}
}

View File

@ -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());
}
}