Remove Type from rustdoc Const

This commit is contained in:
Boxy 2024-06-04 07:01:40 +01:00
parent 60a5bebbe5
commit 432c11feb6
13 changed files with 64 additions and 62 deletions

View File

@ -130,7 +130,10 @@ pub(crate) fn try_inline(
} }
Res::Def(DefKind::Const, did) => { Res::Def(DefKind::Const, did) => {
record_extern_fqn(cx, did, ItemType::Constant); 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) => { Res::Def(DefKind::Macro(kind), did) => {
let is_doc_hidden = cx.tcx.is_doc_hidden(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 = let mut generics =
clean_ty_generics(cx, cx.tcx.generics_of(def_id), cx.tcx.explicit_predicates_of(def_id)); 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::simplify::move_bounds_to_generic_parameters(&mut generics);
let ty = clean_middle_ty(
clean::Constant { ty::Binder::dummy(cx.tcx.type_of(def_id).instantiate_identity()),
type_: Box::new(clean_middle_ty( cx,
ty::Binder::dummy(cx.tcx.type_of(def_id).instantiate_identity()), None,
cx, None,
Some(def_id), );
None, (generics, ty, clean::Constant { kind: clean::ConstantKind::Extern { def_id } })
)),
generics,
kind: clean::ConstantKind::Extern { def_id },
}
} }
fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static { fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {

View File

@ -283,31 +283,17 @@ fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) ->
pub(crate) fn clean_const<'tcx>( pub(crate) fn clean_const<'tcx>(
constant: &hir::ConstArg<'_>, constant: &hir::ConstArg<'_>,
cx: &mut DocContext<'tcx>, _cx: &mut DocContext<'tcx>,
) -> Constant { ) -> Constant {
let def_id = cx.tcx.hir().body_owner_def_id(constant.value.body).to_def_id(); Constant { kind: ConstantKind::Anonymous { body: constant.value.body } }
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 },
}
} }
pub(crate) fn clean_middle_const<'tcx>( pub(crate) fn clean_middle_const<'tcx>(
constant: ty::Binder<'tcx, ty::Const<'tcx>>, constant: ty::Binder<'tcx, ty::Const<'tcx>>,
cx: &mut DocContext<'tcx>, _cx: &mut DocContext<'tcx>,
) -> Constant { ) -> Constant {
// FIXME: instead of storing the stringified expression, store `self` directly instead. // FIXME: instead of storing the stringified expression, store `self` directly instead.
Constant { Constant { kind: ConstantKind::TyConst { expr: constant.skip_binder().to_string().into() } }
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() },
}
} }
pub(crate) fn clean_middle_region<'tcx>(region: ty::Region<'tcx>) -> Option<Lifetime> { 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) => { ItemKind::Static(ty, mutability, body_id) => {
StaticItem(Static { type_: clean_ty(ty, cx), mutability, expr: Some(body_id) }) StaticItem(Static { type_: clean_ty(ty, cx), mutability, expr: Some(body_id) })
} }
ItemKind::Const(ty, generics, body_id) => ConstantItem(Constant { ItemKind::Const(ty, generics, body_id) => ConstantItem(
type_: Box::new(clean_ty(ty, cx)), clean_generics(generics, cx),
generics: clean_generics(generics, cx), Box::new(clean_ty(ty, cx)),
kind: ConstantKind::Local { body: body_id, def_id }, Constant { kind: ConstantKind::Local { body: body_id, def_id } },
}), ),
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy { ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
bounds: ty.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(), bounds: ty.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
generics: clean_generics(ty.generics, cx), generics: clean_generics(ty.generics, cx),

View File

@ -830,7 +830,6 @@ pub(crate) enum ItemKind {
TypeAliasItem(Box<TypeAlias>), TypeAliasItem(Box<TypeAlias>),
OpaqueTyItem(OpaqueTy), OpaqueTyItem(OpaqueTy),
StaticItem(Static), StaticItem(Static),
ConstantItem(Constant),
TraitItem(Box<Trait>), TraitItem(Box<Trait>),
TraitAliasItem(TraitAlias), TraitAliasItem(TraitAlias),
ImplItem(Box<Impl>), ImplItem(Box<Impl>),
@ -853,6 +852,7 @@ pub(crate) enum ItemKind {
PrimitiveItem(PrimitiveType), PrimitiveItem(PrimitiveType),
/// A required associated constant in a trait declaration. /// A required associated constant in a trait declaration.
TyAssocConstItem(Generics, Box<Type>), TyAssocConstItem(Generics, Box<Type>),
ConstantItem(Generics, Box<Type>, Constant),
/// An associated constant in a trait impl or a provided one in a trait declaration. /// An associated constant in a trait impl or a provided one in a trait declaration.
AssocConstItem(Generics, Box<Type>, ConstantKind), AssocConstItem(Generics, Box<Type>, ConstantKind),
/// A required associated type in a trait declaration. /// A required associated type in a trait declaration.
@ -888,7 +888,7 @@ impl ItemKind {
| TypeAliasItem(_) | TypeAliasItem(_)
| OpaqueTyItem(_) | OpaqueTyItem(_)
| StaticItem(_) | StaticItem(_)
| ConstantItem(_) | ConstantItem(_, _, _)
| TraitAliasItem(_) | TraitAliasItem(_)
| TyMethodItem(_) | TyMethodItem(_)
| MethodItem(_, _) | MethodItem(_, _)
@ -922,7 +922,7 @@ impl ItemKind {
| TypeAliasItem(_) | TypeAliasItem(_)
| OpaqueTyItem(_) | OpaqueTyItem(_)
| StaticItem(_) | StaticItem(_)
| ConstantItem(_) | ConstantItem(_, _, _)
| TraitAliasItem(_) | TraitAliasItem(_)
| ForeignFunctionItem(_) | ForeignFunctionItem(_)
| ForeignStaticItem(_) | ForeignStaticItem(_)
@ -2364,8 +2364,6 @@ pub(crate) struct Static {
#[derive(Clone, PartialEq, Eq, Hash, Debug)] #[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub(crate) struct Constant { pub(crate) struct Constant {
pub(crate) type_: Box<Type>,
pub(crate) generics: Generics,
pub(crate) kind: ConstantKind, pub(crate) kind: ConstantKind,
} }

View File

@ -353,8 +353,8 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
s s
} }
// array lengths are obviously usize // array lengths are obviously usize
ty::ConstKind::Value(ty::ValTree::Leaf(scalar)) ty::ConstKind::Value(ty, ty::ValTree::Leaf(scalar))
if *n.ty().kind() == ty::Uint(ty::UintTy::Usize) => if *ty.kind() == ty::Uint(ty::UintTy::Usize) =>
{ {
scalar.to_string() scalar.to_string()
} }

View File

@ -79,7 +79,7 @@ pub(crate) trait DocFolder: Sized {
| FunctionItem(_) | FunctionItem(_)
| OpaqueTyItem(_) | OpaqueTyItem(_)
| StaticItem(_) | StaticItem(_)
| ConstantItem(_) | ConstantItem(_, _, _)
| TraitAliasItem(_) | TraitAliasItem(_)
| TyMethodItem(_) | TyMethodItem(_)
| MethodItem(_, _) | MethodItem(_, _)

View File

@ -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::ProcMacroItem(ref m) => item_proc_macro(buf, cx, item, m),
clean::PrimitiveItem(_) => item_primitive(buf, cx, item), clean::PrimitiveItem(_) => item_primitive(buf, cx, item),
clean::StaticItem(ref i) | clean::ForeignStaticItem(ref i) => item_static(buf, cx, item, i), 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::ForeignTypeItem => item_foreign_type(buf, cx, item),
clean::KeywordItem => item_keyword(buf, cx, item), clean::KeywordItem => item_keyword(buf, cx, item),
clean::OpaqueTyItem(ref e) => item_opaque_ty(buf, cx, item, e), 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| { wrap_item(w, |w| {
let tcx = cx.tcx(); let tcx = cx.tcx();
render_attributes_in_code(w, it, cx); 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}const {name}{generics}: {typ}{where_clause}",
vis = visibility_print_with_space(it, cx), vis = visibility_print_with_space(it, cx),
name = it.name.unwrap(), name = it.name.unwrap(),
generics = c.generics.print(cx), generics = generics.print(cx),
typ = c.type_.print(cx), typ = ty.print(cx),
where_clause = print_where_clause(&c.generics, cx, 0, Ending::NoNewline), where_clause = print_where_clause(&generics, cx, 0, Ending::NoNewline),
); );
// FIXME: The code below now prints // FIXME: The code below now prints

View File

@ -183,7 +183,7 @@ impl FromWithTcx<clean::Constant> for Constant {
let expr = constant.expr(tcx); let expr = constant.expr(tcx);
let value = constant.value(tcx); let value = constant.value(tcx);
let is_literal = constant.is_literal(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, ForeignTypeItem => ItemEnum::ForeignType,
TypeAliasItem(t) => ItemEnum::TypeAlias(t.into_tcx(tcx)), TypeAliasItem(t) => ItemEnum::TypeAlias(t.into_tcx(tcx)),
OpaqueTyItem(t) => ItemEnum::OpaqueTy(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), MacroItem(m) => ItemEnum::Macro(m.source),
ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_tcx(tcx)), ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_tcx(tcx)),
PrimitiveItem(p) => { PrimitiveItem(p) => {

View File

@ -186,7 +186,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
| types::ItemEnum::Impl(_) | types::ItemEnum::Impl(_)
| types::ItemEnum::TypeAlias(_) | types::ItemEnum::TypeAlias(_)
| types::ItemEnum::OpaqueTy(_) | types::ItemEnum::OpaqueTy(_)
| types::ItemEnum::Constant(_) | types::ItemEnum::Constant { .. }
| types::ItemEnum::Static(_) | types::ItemEnum::Static(_)
| types::ItemEnum::ForeignType | types::ItemEnum::ForeignType
| types::ItemEnum::Macro(_) | types::ItemEnum::Macro(_)

View File

@ -62,7 +62,7 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
| clean::AssocTypeItem(..) | clean::AssocTypeItem(..)
| clean::TypeAliasItem(_) | clean::TypeAliasItem(_)
| clean::StaticItem(_) | clean::StaticItem(_)
| clean::ConstantItem(_) | clean::ConstantItem(_, _, _)
| clean::ExternCrateItem { .. } | clean::ExternCrateItem { .. }
| clean::ImportItem(_) | clean::ImportItem(_)
| clean::PrimitiveItem(_) | clean::PrimitiveItem(_)

View File

@ -28,7 +28,7 @@ pub(crate) trait DocVisitor: Sized {
| TypeAliasItem(_) | TypeAliasItem(_)
| OpaqueTyItem(_) | OpaqueTyItem(_)
| StaticItem(_) | StaticItem(_)
| ConstantItem(_) | ConstantItem(_, _, _)
| TraitAliasItem(_) | TraitAliasItem(_)
| TyMethodItem(_) | TyMethodItem(_)
| MethodItem(_, _) | MethodItem(_, _)

View File

@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
use std::path::PathBuf; use std::path::PathBuf;
/// rustdoc format-version. /// 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 /// 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 /// 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)] #[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Constant { pub struct Constant {
#[serde(rename = "type")]
pub type_: Type,
pub expr: String, pub expr: String,
pub value: Option<String>, pub value: Option<String>,
pub is_literal: bool, pub is_literal: bool,
@ -256,7 +254,12 @@ pub enum ItemEnum {
TypeAlias(TypeAlias), TypeAlias(TypeAlias),
OpaqueTy(OpaqueTy), OpaqueTy(OpaqueTy),
Constant(Constant), Constant {
#[serde(rename = "type")]
type_: Type,
#[serde(rename = "const")]
const_: Constant,
},
Static(Static), Static(Static),

View File

@ -150,7 +150,7 @@ impl Kind {
ItemEnum::Impl(_) => Impl, ItemEnum::Impl(_) => Impl,
ItemEnum::TypeAlias(_) => TypeAlias, ItemEnum::TypeAlias(_) => TypeAlias,
ItemEnum::OpaqueTy(_) => OpaqueTy, ItemEnum::OpaqueTy(_) => OpaqueTy,
ItemEnum::Constant(_) => Constant, ItemEnum::Constant { .. } => Constant,
ItemEnum::Static(_) => Static, ItemEnum::Static(_) => Static,
ItemEnum::Macro(_) => Macro, ItemEnum::Macro(_) => Macro,
ItemEnum::ProcMacro(_) => ProcMacro, ItemEnum::ProcMacro(_) => ProcMacro,

View File

@ -101,7 +101,10 @@ impl<'a> Validator<'a> {
ItemEnum::Impl(x) => self.check_impl(x, id), ItemEnum::Impl(x) => self.check_impl(x, id),
ItemEnum::TypeAlias(x) => self.check_type_alias(x), ItemEnum::TypeAlias(x) => self.check_type_alias(x),
ItemEnum::OpaqueTy(x) => self.check_opaque_ty(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::Static(x) => self.check_static(x),
ItemEnum::ForeignType => {} // nop ItemEnum::ForeignType => {} // nop
ItemEnum::Macro(x) => self.check_macro(x), ItemEnum::Macro(x) => self.check_macro(x),
@ -231,8 +234,8 @@ impl<'a> Validator<'a> {
self.check_generics(&x.generics); self.check_generics(&x.generics);
} }
fn check_constant(&mut self, x: &'a Constant) { fn check_constant(&mut self, _x: &'a Constant) {
self.check_type(&x.type_); // nop
} }
fn check_static(&mut self, x: &'a Static) { fn check_static(&mut self, x: &'a Static) {