mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-16 17:03:35 +00:00
Remove Type
from rustdoc Const
This commit is contained in:
parent
60a5bebbe5
commit
432c11feb6
@ -130,7 +130,10 @@ pub(crate) fn try_inline(
|
||||
}
|
||||
Res::Def(DefKind::Const, did) => {
|
||||
record_extern_fqn(cx, did, ItemType::Constant);
|
||||
cx.with_param_env(did, |cx| clean::ConstantItem(build_const(cx, did)))
|
||||
cx.with_param_env(did, |cx| {
|
||||
let (generics, ty, ct) = build_const_item(cx, did);
|
||||
clean::ConstantItem(generics, Box::new(ty), ct)
|
||||
})
|
||||
}
|
||||
Res::Def(DefKind::Macro(kind), did) => {
|
||||
let is_doc_hidden = cx.tcx.is_doc_hidden(did)
|
||||
@ -717,21 +720,20 @@ pub(crate) fn print_inlined_const(tcx: TyCtxt<'_>, did: DefId) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
fn build_const(cx: &mut DocContext<'_>, def_id: DefId) -> clean::Constant {
|
||||
fn build_const_item(
|
||||
cx: &mut DocContext<'_>,
|
||||
def_id: DefId,
|
||||
) -> (clean::Generics, clean::Type, clean::Constant) {
|
||||
let mut generics =
|
||||
clean_ty_generics(cx, cx.tcx.generics_of(def_id), cx.tcx.explicit_predicates_of(def_id));
|
||||
clean::simplify::move_bounds_to_generic_parameters(&mut generics);
|
||||
|
||||
clean::Constant {
|
||||
type_: Box::new(clean_middle_ty(
|
||||
ty::Binder::dummy(cx.tcx.type_of(def_id).instantiate_identity()),
|
||||
cx,
|
||||
Some(def_id),
|
||||
None,
|
||||
)),
|
||||
generics,
|
||||
kind: clean::ConstantKind::Extern { def_id },
|
||||
}
|
||||
let ty = clean_middle_ty(
|
||||
ty::Binder::dummy(cx.tcx.type_of(def_id).instantiate_identity()),
|
||||
cx,
|
||||
None,
|
||||
None,
|
||||
);
|
||||
(generics, ty, clean::Constant { kind: clean::ConstantKind::Extern { def_id } })
|
||||
}
|
||||
|
||||
fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {
|
||||
|
@ -283,31 +283,17 @@ fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) ->
|
||||
|
||||
pub(crate) fn clean_const<'tcx>(
|
||||
constant: &hir::ConstArg<'_>,
|
||||
cx: &mut DocContext<'tcx>,
|
||||
_cx: &mut DocContext<'tcx>,
|
||||
) -> Constant {
|
||||
let def_id = cx.tcx.hir().body_owner_def_id(constant.value.body).to_def_id();
|
||||
Constant {
|
||||
type_: Box::new(clean_middle_ty(
|
||||
ty::Binder::dummy(cx.tcx.type_of(def_id).instantiate_identity()),
|
||||
cx,
|
||||
Some(def_id),
|
||||
None,
|
||||
)),
|
||||
generics: Generics::default(),
|
||||
kind: ConstantKind::Anonymous { body: constant.value.body },
|
||||
}
|
||||
Constant { kind: ConstantKind::Anonymous { body: constant.value.body } }
|
||||
}
|
||||
|
||||
pub(crate) fn clean_middle_const<'tcx>(
|
||||
constant: ty::Binder<'tcx, ty::Const<'tcx>>,
|
||||
cx: &mut DocContext<'tcx>,
|
||||
_cx: &mut DocContext<'tcx>,
|
||||
) -> Constant {
|
||||
// FIXME: instead of storing the stringified expression, store `self` directly instead.
|
||||
Constant {
|
||||
type_: Box::new(clean_middle_ty(constant.map_bound(|c| c.ty()), cx, None, None)),
|
||||
generics: Generics::default(),
|
||||
kind: ConstantKind::TyConst { expr: constant.skip_binder().to_string().into() },
|
||||
}
|
||||
Constant { kind: ConstantKind::TyConst { expr: constant.skip_binder().to_string().into() } }
|
||||
}
|
||||
|
||||
pub(crate) fn clean_middle_region<'tcx>(region: ty::Region<'tcx>) -> Option<Lifetime> {
|
||||
@ -2738,11 +2724,11 @@ fn clean_maybe_renamed_item<'tcx>(
|
||||
ItemKind::Static(ty, mutability, body_id) => {
|
||||
StaticItem(Static { type_: clean_ty(ty, cx), mutability, expr: Some(body_id) })
|
||||
}
|
||||
ItemKind::Const(ty, generics, body_id) => ConstantItem(Constant {
|
||||
type_: Box::new(clean_ty(ty, cx)),
|
||||
generics: clean_generics(generics, cx),
|
||||
kind: ConstantKind::Local { body: body_id, def_id },
|
||||
}),
|
||||
ItemKind::Const(ty, generics, body_id) => ConstantItem(
|
||||
clean_generics(generics, cx),
|
||||
Box::new(clean_ty(ty, cx)),
|
||||
Constant { kind: ConstantKind::Local { body: body_id, def_id } },
|
||||
),
|
||||
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
|
||||
bounds: ty.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
|
||||
generics: clean_generics(ty.generics, cx),
|
||||
|
@ -830,7 +830,6 @@ pub(crate) enum ItemKind {
|
||||
TypeAliasItem(Box<TypeAlias>),
|
||||
OpaqueTyItem(OpaqueTy),
|
||||
StaticItem(Static),
|
||||
ConstantItem(Constant),
|
||||
TraitItem(Box<Trait>),
|
||||
TraitAliasItem(TraitAlias),
|
||||
ImplItem(Box<Impl>),
|
||||
@ -853,6 +852,7 @@ pub(crate) enum ItemKind {
|
||||
PrimitiveItem(PrimitiveType),
|
||||
/// A required associated constant in a trait declaration.
|
||||
TyAssocConstItem(Generics, Box<Type>),
|
||||
ConstantItem(Generics, Box<Type>, Constant),
|
||||
/// An associated constant in a trait impl or a provided one in a trait declaration.
|
||||
AssocConstItem(Generics, Box<Type>, ConstantKind),
|
||||
/// A required associated type in a trait declaration.
|
||||
@ -888,7 +888,7 @@ impl ItemKind {
|
||||
| TypeAliasItem(_)
|
||||
| OpaqueTyItem(_)
|
||||
| StaticItem(_)
|
||||
| ConstantItem(_)
|
||||
| ConstantItem(_, _, _)
|
||||
| TraitAliasItem(_)
|
||||
| TyMethodItem(_)
|
||||
| MethodItem(_, _)
|
||||
@ -922,7 +922,7 @@ impl ItemKind {
|
||||
| TypeAliasItem(_)
|
||||
| OpaqueTyItem(_)
|
||||
| StaticItem(_)
|
||||
| ConstantItem(_)
|
||||
| ConstantItem(_, _, _)
|
||||
| TraitAliasItem(_)
|
||||
| ForeignFunctionItem(_)
|
||||
| ForeignStaticItem(_)
|
||||
@ -2364,8 +2364,6 @@ pub(crate) struct Static {
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
|
||||
pub(crate) struct Constant {
|
||||
pub(crate) type_: Box<Type>,
|
||||
pub(crate) generics: Generics,
|
||||
pub(crate) kind: ConstantKind,
|
||||
}
|
||||
|
||||
|
@ -353,8 +353,8 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
|
||||
s
|
||||
}
|
||||
// array lengths are obviously usize
|
||||
ty::ConstKind::Value(ty::ValTree::Leaf(scalar))
|
||||
if *n.ty().kind() == ty::Uint(ty::UintTy::Usize) =>
|
||||
ty::ConstKind::Value(ty, ty::ValTree::Leaf(scalar))
|
||||
if *ty.kind() == ty::Uint(ty::UintTy::Usize) =>
|
||||
{
|
||||
scalar.to_string()
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ pub(crate) trait DocFolder: Sized {
|
||||
| FunctionItem(_)
|
||||
| OpaqueTyItem(_)
|
||||
| StaticItem(_)
|
||||
| ConstantItem(_)
|
||||
| ConstantItem(_, _, _)
|
||||
| TraitAliasItem(_)
|
||||
| TyMethodItem(_)
|
||||
| MethodItem(_, _)
|
||||
|
@ -266,7 +266,7 @@ pub(super) fn print_item(cx: &mut Context<'_>, item: &clean::Item, buf: &mut Buf
|
||||
clean::ProcMacroItem(ref m) => item_proc_macro(buf, cx, item, m),
|
||||
clean::PrimitiveItem(_) => item_primitive(buf, cx, item),
|
||||
clean::StaticItem(ref i) | clean::ForeignStaticItem(ref i) => item_static(buf, cx, item, i),
|
||||
clean::ConstantItem(ref c) => item_constant(buf, cx, item, c),
|
||||
clean::ConstantItem(generics, ty, c) => item_constant(buf, cx, item, generics, ty, c),
|
||||
clean::ForeignTypeItem => item_foreign_type(buf, cx, item),
|
||||
clean::KeywordItem => item_keyword(buf, cx, item),
|
||||
clean::OpaqueTyItem(ref e) => item_opaque_ty(buf, cx, item, e),
|
||||
@ -1844,7 +1844,14 @@ fn item_primitive(w: &mut impl fmt::Write, cx: &mut Context<'_>, it: &clean::Ite
|
||||
}
|
||||
}
|
||||
|
||||
fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &clean::Constant) {
|
||||
fn item_constant(
|
||||
w: &mut Buffer,
|
||||
cx: &mut Context<'_>,
|
||||
it: &clean::Item,
|
||||
generics: &clean::Generics,
|
||||
ty: &clean::Type,
|
||||
c: &clean::Constant,
|
||||
) {
|
||||
wrap_item(w, |w| {
|
||||
let tcx = cx.tcx();
|
||||
render_attributes_in_code(w, it, cx);
|
||||
@ -1854,9 +1861,9 @@ fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &cle
|
||||
"{vis}const {name}{generics}: {typ}{where_clause}",
|
||||
vis = visibility_print_with_space(it, cx),
|
||||
name = it.name.unwrap(),
|
||||
generics = c.generics.print(cx),
|
||||
typ = c.type_.print(cx),
|
||||
where_clause = print_where_clause(&c.generics, cx, 0, Ending::NoNewline),
|
||||
generics = generics.print(cx),
|
||||
typ = ty.print(cx),
|
||||
where_clause = print_where_clause(&generics, cx, 0, Ending::NoNewline),
|
||||
);
|
||||
|
||||
// FIXME: The code below now prints
|
||||
|
@ -183,7 +183,7 @@ impl FromWithTcx<clean::Constant> for Constant {
|
||||
let expr = constant.expr(tcx);
|
||||
let value = constant.value(tcx);
|
||||
let is_literal = constant.is_literal(tcx);
|
||||
Constant { type_: (*constant.type_).into_tcx(tcx), expr, value, is_literal }
|
||||
Constant { expr, value, is_literal }
|
||||
}
|
||||
}
|
||||
|
||||
@ -321,7 +321,10 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
|
||||
ForeignTypeItem => ItemEnum::ForeignType,
|
||||
TypeAliasItem(t) => ItemEnum::TypeAlias(t.into_tcx(tcx)),
|
||||
OpaqueTyItem(t) => ItemEnum::OpaqueTy(t.into_tcx(tcx)),
|
||||
ConstantItem(c) => ItemEnum::Constant(c.into_tcx(tcx)),
|
||||
// FIXME(generic_const_items): Add support for generic free consts
|
||||
ConstantItem(_generics, t, c) => {
|
||||
ItemEnum::Constant { type_: (*t).into_tcx(tcx), const_: c.into_tcx(tcx) }
|
||||
}
|
||||
MacroItem(m) => ItemEnum::Macro(m.source),
|
||||
ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_tcx(tcx)),
|
||||
PrimitiveItem(p) => {
|
||||
|
@ -186,7 +186,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
|
||||
| types::ItemEnum::Impl(_)
|
||||
| types::ItemEnum::TypeAlias(_)
|
||||
| types::ItemEnum::OpaqueTy(_)
|
||||
| types::ItemEnum::Constant(_)
|
||||
| types::ItemEnum::Constant { .. }
|
||||
| types::ItemEnum::Static(_)
|
||||
| types::ItemEnum::ForeignType
|
||||
| types::ItemEnum::Macro(_)
|
||||
|
@ -62,7 +62,7 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
|
||||
| clean::AssocTypeItem(..)
|
||||
| clean::TypeAliasItem(_)
|
||||
| clean::StaticItem(_)
|
||||
| clean::ConstantItem(_)
|
||||
| clean::ConstantItem(_, _, _)
|
||||
| clean::ExternCrateItem { .. }
|
||||
| clean::ImportItem(_)
|
||||
| clean::PrimitiveItem(_)
|
||||
|
@ -28,7 +28,7 @@ pub(crate) trait DocVisitor: Sized {
|
||||
| TypeAliasItem(_)
|
||||
| OpaqueTyItem(_)
|
||||
| StaticItem(_)
|
||||
| ConstantItem(_)
|
||||
| ConstantItem(_, _, _)
|
||||
| TraitAliasItem(_)
|
||||
| TyMethodItem(_)
|
||||
| MethodItem(_, _)
|
||||
|
@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// rustdoc format-version.
|
||||
pub const FORMAT_VERSION: u32 = 29;
|
||||
pub const FORMAT_VERSION: u32 = 30;
|
||||
|
||||
/// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information
|
||||
/// about the language items in the local crate, as well as info about external items to allow
|
||||
@ -167,8 +167,6 @@ pub enum GenericArg {
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
|
||||
pub struct Constant {
|
||||
#[serde(rename = "type")]
|
||||
pub type_: Type,
|
||||
pub expr: String,
|
||||
pub value: Option<String>,
|
||||
pub is_literal: bool,
|
||||
@ -256,7 +254,12 @@ pub enum ItemEnum {
|
||||
|
||||
TypeAlias(TypeAlias),
|
||||
OpaqueTy(OpaqueTy),
|
||||
Constant(Constant),
|
||||
Constant {
|
||||
#[serde(rename = "type")]
|
||||
type_: Type,
|
||||
#[serde(rename = "const")]
|
||||
const_: Constant,
|
||||
},
|
||||
|
||||
Static(Static),
|
||||
|
||||
|
@ -150,7 +150,7 @@ impl Kind {
|
||||
ItemEnum::Impl(_) => Impl,
|
||||
ItemEnum::TypeAlias(_) => TypeAlias,
|
||||
ItemEnum::OpaqueTy(_) => OpaqueTy,
|
||||
ItemEnum::Constant(_) => Constant,
|
||||
ItemEnum::Constant { .. } => Constant,
|
||||
ItemEnum::Static(_) => Static,
|
||||
ItemEnum::Macro(_) => Macro,
|
||||
ItemEnum::ProcMacro(_) => ProcMacro,
|
||||
|
@ -101,7 +101,10 @@ impl<'a> Validator<'a> {
|
||||
ItemEnum::Impl(x) => self.check_impl(x, id),
|
||||
ItemEnum::TypeAlias(x) => self.check_type_alias(x),
|
||||
ItemEnum::OpaqueTy(x) => self.check_opaque_ty(x),
|
||||
ItemEnum::Constant(x) => self.check_constant(x),
|
||||
ItemEnum::Constant { type_, const_ } => {
|
||||
self.check_type(type_);
|
||||
self.check_constant(const_);
|
||||
}
|
||||
ItemEnum::Static(x) => self.check_static(x),
|
||||
ItemEnum::ForeignType => {} // nop
|
||||
ItemEnum::Macro(x) => self.check_macro(x),
|
||||
@ -231,8 +234,8 @@ impl<'a> Validator<'a> {
|
||||
self.check_generics(&x.generics);
|
||||
}
|
||||
|
||||
fn check_constant(&mut self, x: &'a Constant) {
|
||||
self.check_type(&x.type_);
|
||||
fn check_constant(&mut self, _x: &'a Constant) {
|
||||
// nop
|
||||
}
|
||||
|
||||
fn check_static(&mut self, x: &'a Static) {
|
||||
|
Loading…
Reference in New Issue
Block a user