Skip to content

Commit 4d395b3

Browse files
committed
Remove Type from rustdoc Const
1 parent 43bd68e commit 4d395b3

File tree

13 files changed

+64
-62
lines changed

13 files changed

+64
-62
lines changed

src/librustdoc/clean/inline.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,10 @@ pub(crate) fn try_inline(
130130
}
131131
Res::Def(DefKind::Const, did) => {
132132
record_extern_fqn(cx, did, ItemType::Constant);
133-
cx.with_param_env(did, |cx| clean::ConstantItem(build_const(cx, did)))
133+
cx.with_param_env(did, |cx| {
134+
let (generics, ty, ct) = build_const_item(cx, did);
135+
clean::ConstantItem(generics, Box::new(ty), ct)
136+
})
134137
}
135138
Res::Def(DefKind::Macro(kind), did) => {
136139
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 {
717720
}
718721
}
719722

720-
fn build_const(cx: &mut DocContext<'_>, def_id: DefId) -> clean::Constant {
723+
fn build_const_item(
724+
cx: &mut DocContext<'_>,
725+
def_id: DefId,
726+
) -> (clean::Generics, clean::Type, clean::Constant) {
721727
let mut generics =
722728
clean_ty_generics(cx, cx.tcx.generics_of(def_id), cx.tcx.explicit_predicates_of(def_id));
723729
clean::simplify::move_bounds_to_generic_parameters(&mut generics);
724-
725-
clean::Constant {
726-
type_: Box::new(clean_middle_ty(
727-
ty::Binder::dummy(cx.tcx.type_of(def_id).instantiate_identity()),
728-
cx,
729-
Some(def_id),
730-
None,
731-
)),
732-
generics,
733-
kind: clean::ConstantKind::Extern { def_id },
734-
}
730+
let ty = clean_middle_ty(
731+
ty::Binder::dummy(cx.tcx.type_of(def_id).instantiate_identity()),
732+
cx,
733+
None,
734+
None,
735+
);
736+
(generics, ty, clean::Constant { kind: clean::ConstantKind::Extern { def_id } })
735737
}
736738

737739
fn build_static(cx: &mut DocContext<'_>, did: DefId, mutable: bool) -> clean::Static {

src/librustdoc/clean/mod.rs

+9-23
Original file line numberDiff line numberDiff line change
@@ -283,31 +283,17 @@ fn clean_lifetime<'tcx>(lifetime: &hir::Lifetime, cx: &mut DocContext<'tcx>) ->
283283

284284
pub(crate) fn clean_const<'tcx>(
285285
constant: &hir::ConstArg<'_>,
286-
cx: &mut DocContext<'tcx>,
286+
_cx: &mut DocContext<'tcx>,
287287
) -> Constant {
288-
let def_id = cx.tcx.hir().body_owner_def_id(constant.value.body).to_def_id();
289-
Constant {
290-
type_: Box::new(clean_middle_ty(
291-
ty::Binder::dummy(cx.tcx.type_of(def_id).instantiate_identity()),
292-
cx,
293-
Some(def_id),
294-
None,
295-
)),
296-
generics: Generics::default(),
297-
kind: ConstantKind::Anonymous { body: constant.value.body },
298-
}
288+
Constant { kind: ConstantKind::Anonymous { body: constant.value.body } }
299289
}
300290

301291
pub(crate) fn clean_middle_const<'tcx>(
302292
constant: ty::Binder<'tcx, ty::Const<'tcx>>,
303-
cx: &mut DocContext<'tcx>,
293+
_cx: &mut DocContext<'tcx>,
304294
) -> Constant {
305295
// FIXME: instead of storing the stringified expression, store `self` directly instead.
306-
Constant {
307-
type_: Box::new(clean_middle_ty(constant.map_bound(|c| c.ty()), cx, None, None)),
308-
generics: Generics::default(),
309-
kind: ConstantKind::TyConst { expr: constant.skip_binder().to_string().into() },
310-
}
296+
Constant { kind: ConstantKind::TyConst { expr: constant.skip_binder().to_string().into() } }
311297
}
312298

313299
pub(crate) fn clean_middle_region<'tcx>(region: ty::Region<'tcx>) -> Option<Lifetime> {
@@ -2738,11 +2724,11 @@ fn clean_maybe_renamed_item<'tcx>(
27382724
ItemKind::Static(ty, mutability, body_id) => {
27392725
StaticItem(Static { type_: clean_ty(ty, cx), mutability, expr: Some(body_id) })
27402726
}
2741-
ItemKind::Const(ty, generics, body_id) => ConstantItem(Constant {
2742-
type_: Box::new(clean_ty(ty, cx)),
2743-
generics: clean_generics(generics, cx),
2744-
kind: ConstantKind::Local { body: body_id, def_id },
2745-
}),
2727+
ItemKind::Const(ty, generics, body_id) => ConstantItem(
2728+
clean_generics(generics, cx),
2729+
Box::new(clean_ty(ty, cx)),
2730+
Constant { kind: ConstantKind::Local { body: body_id, def_id } },
2731+
),
27462732
ItemKind::OpaqueTy(ref ty) => OpaqueTyItem(OpaqueTy {
27472733
bounds: ty.bounds.iter().filter_map(|x| clean_generic_bound(x, cx)).collect(),
27482734
generics: clean_generics(ty.generics, cx),

src/librustdoc/clean/types.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -830,7 +830,6 @@ pub(crate) enum ItemKind {
830830
TypeAliasItem(Box<TypeAlias>),
831831
OpaqueTyItem(OpaqueTy),
832832
StaticItem(Static),
833-
ConstantItem(Constant),
834833
TraitItem(Box<Trait>),
835834
TraitAliasItem(TraitAlias),
836835
ImplItem(Box<Impl>),
@@ -853,6 +852,7 @@ pub(crate) enum ItemKind {
853852
PrimitiveItem(PrimitiveType),
854853
/// A required associated constant in a trait declaration.
855854
TyAssocConstItem(Generics, Box<Type>),
855+
ConstantItem(Generics, Box<Type>, Constant),
856856
/// An associated constant in a trait impl or a provided one in a trait declaration.
857857
AssocConstItem(Generics, Box<Type>, ConstantKind),
858858
/// A required associated type in a trait declaration.
@@ -888,7 +888,7 @@ impl ItemKind {
888888
| TypeAliasItem(_)
889889
| OpaqueTyItem(_)
890890
| StaticItem(_)
891-
| ConstantItem(_)
891+
| ConstantItem(_, _, _)
892892
| TraitAliasItem(_)
893893
| TyMethodItem(_)
894894
| MethodItem(_, _)
@@ -922,7 +922,7 @@ impl ItemKind {
922922
| TypeAliasItem(_)
923923
| OpaqueTyItem(_)
924924
| StaticItem(_)
925-
| ConstantItem(_)
925+
| ConstantItem(_, _, _)
926926
| TraitAliasItem(_)
927927
| ForeignFunctionItem(_)
928928
| ForeignStaticItem(_)
@@ -2364,8 +2364,6 @@ pub(crate) struct Static {
23642364

23652365
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
23662366
pub(crate) struct Constant {
2367-
pub(crate) type_: Box<Type>,
2368-
pub(crate) generics: Generics,
23692367
pub(crate) kind: ConstantKind,
23702368
}
23712369

src/librustdoc/clean/utils.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,8 @@ pub(crate) fn print_const(cx: &DocContext<'_>, n: ty::Const<'_>) -> String {
353353
s
354354
}
355355
// array lengths are obviously usize
356-
ty::ConstKind::Value(ty::ValTree::Leaf(scalar))
357-
if *n.ty().kind() == ty::Uint(ty::UintTy::Usize) =>
356+
ty::ConstKind::Value(ty, ty::ValTree::Leaf(scalar))
357+
if *ty.kind() == ty::Uint(ty::UintTy::Usize) =>
358358
{
359359
scalar.to_string()
360360
}

src/librustdoc/fold.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub(crate) trait DocFolder: Sized {
7979
| FunctionItem(_)
8080
| OpaqueTyItem(_)
8181
| StaticItem(_)
82-
| ConstantItem(_)
82+
| ConstantItem(_, _, _)
8383
| TraitAliasItem(_)
8484
| TyMethodItem(_)
8585
| MethodItem(_, _)

src/librustdoc/html/render/print_item.rs

+12-5
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ pub(super) fn print_item(cx: &mut Context<'_>, item: &clean::Item, buf: &mut Buf
266266
clean::ProcMacroItem(ref m) => item_proc_macro(buf, cx, item, m),
267267
clean::PrimitiveItem(_) => item_primitive(buf, cx, item),
268268
clean::StaticItem(ref i) | clean::ForeignStaticItem(ref i) => item_static(buf, cx, item, i),
269-
clean::ConstantItem(ref c) => item_constant(buf, cx, item, c),
269+
clean::ConstantItem(generics, ty, c) => item_constant(buf, cx, item, generics, ty, c),
270270
clean::ForeignTypeItem => item_foreign_type(buf, cx, item),
271271
clean::KeywordItem => item_keyword(buf, cx, item),
272272
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
18441844
}
18451845
}
18461846

1847-
fn item_constant(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, c: &clean::Constant) {
1847+
fn item_constant(
1848+
w: &mut Buffer,
1849+
cx: &mut Context<'_>,
1850+
it: &clean::Item,
1851+
generics: &clean::Generics,
1852+
ty: &clean::Type,
1853+
c: &clean::Constant,
1854+
) {
18481855
wrap_item(w, |w| {
18491856
let tcx = cx.tcx();
18501857
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
18541861
"{vis}const {name}{generics}: {typ}{where_clause}",
18551862
vis = visibility_print_with_space(it, cx),
18561863
name = it.name.unwrap(),
1857-
generics = c.generics.print(cx),
1858-
typ = c.type_.print(cx),
1859-
where_clause = print_where_clause(&c.generics, cx, 0, Ending::NoNewline),
1864+
generics = generics.print(cx),
1865+
typ = ty.print(cx),
1866+
where_clause = print_where_clause(&generics, cx, 0, Ending::NoNewline),
18601867
);
18611868

18621869
// FIXME: The code below now prints

src/librustdoc/json/conversions.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl FromWithTcx<clean::Constant> for Constant {
183183
let expr = constant.expr(tcx);
184184
let value = constant.value(tcx);
185185
let is_literal = constant.is_literal(tcx);
186-
Constant { type_: (*constant.type_).into_tcx(tcx), expr, value, is_literal }
186+
Constant { expr, value, is_literal }
187187
}
188188
}
189189

@@ -321,7 +321,10 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
321321
ForeignTypeItem => ItemEnum::ForeignType,
322322
TypeAliasItem(t) => ItemEnum::TypeAlias(t.into_tcx(tcx)),
323323
OpaqueTyItem(t) => ItemEnum::OpaqueTy(t.into_tcx(tcx)),
324-
ConstantItem(c) => ItemEnum::Constant(c.into_tcx(tcx)),
324+
// FIXME(generic_const_items): Add support for generic free consts
325+
ConstantItem(_generics, t, c) => {
326+
ItemEnum::Constant { type_: (*t).into_tcx(tcx), const_: c.into_tcx(tcx) }
327+
}
325328
MacroItem(m) => ItemEnum::Macro(m.source),
326329
ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_tcx(tcx)),
327330
PrimitiveItem(p) => {

src/librustdoc/json/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
186186
| types::ItemEnum::Impl(_)
187187
| types::ItemEnum::TypeAlias(_)
188188
| types::ItemEnum::OpaqueTy(_)
189-
| types::ItemEnum::Constant(_)
189+
| types::ItemEnum::Constant { .. }
190190
| types::ItemEnum::Static(_)
191191
| types::ItemEnum::ForeignType
192192
| types::ItemEnum::Macro(_)

src/librustdoc/passes/check_doc_test_visibility.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ pub(crate) fn should_have_doc_example(cx: &DocContext<'_>, item: &clean::Item) -
6262
| clean::AssocTypeItem(..)
6363
| clean::TypeAliasItem(_)
6464
| clean::StaticItem(_)
65-
| clean::ConstantItem(_)
65+
| clean::ConstantItem(_, _, _)
6666
| clean::ExternCrateItem { .. }
6767
| clean::ImportItem(_)
6868
| clean::PrimitiveItem(_)

src/librustdoc/visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(crate) trait DocVisitor: Sized {
2828
| TypeAliasItem(_)
2929
| OpaqueTyItem(_)
3030
| StaticItem(_)
31-
| ConstantItem(_)
31+
| ConstantItem(_, _, _)
3232
| TraitAliasItem(_)
3333
| TyMethodItem(_)
3434
| MethodItem(_, _)

src/rustdoc-json-types/lib.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
88
use std::path::PathBuf;
99

1010
/// rustdoc format-version.
11-
pub const FORMAT_VERSION: u32 = 29;
11+
pub const FORMAT_VERSION: u32 = 30;
1212

1313
/// A `Crate` is the root of the emitted JSON blob. It contains all type/documentation information
1414
/// about the language items in the local crate, as well as info about external items to allow
@@ -167,8 +167,6 @@ pub enum GenericArg {
167167

168168
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
169169
pub struct Constant {
170-
#[serde(rename = "type")]
171-
pub type_: Type,
172170
pub expr: String,
173171
pub value: Option<String>,
174172
pub is_literal: bool,
@@ -256,7 +254,12 @@ pub enum ItemEnum {
256254

257255
TypeAlias(TypeAlias),
258256
OpaqueTy(OpaqueTy),
259-
Constant(Constant),
257+
Constant {
258+
#[serde(rename = "type")]
259+
type_: Type,
260+
#[serde(rename = "const")]
261+
const_: Constant,
262+
},
260263

261264
Static(Static),
262265

src/tools/jsondoclint/src/item_kind.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl Kind {
150150
ItemEnum::Impl(_) => Impl,
151151
ItemEnum::TypeAlias(_) => TypeAlias,
152152
ItemEnum::OpaqueTy(_) => OpaqueTy,
153-
ItemEnum::Constant(_) => Constant,
153+
ItemEnum::Constant { .. } => Constant,
154154
ItemEnum::Static(_) => Static,
155155
ItemEnum::Macro(_) => Macro,
156156
ItemEnum::ProcMacro(_) => ProcMacro,

src/tools/jsondoclint/src/validator.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ impl<'a> Validator<'a> {
101101
ItemEnum::Impl(x) => self.check_impl(x, id),
102102
ItemEnum::TypeAlias(x) => self.check_type_alias(x),
103103
ItemEnum::OpaqueTy(x) => self.check_opaque_ty(x),
104-
ItemEnum::Constant(x) => self.check_constant(x),
104+
ItemEnum::Constant { type_, const_ } => {
105+
self.check_type(type_);
106+
self.check_constant(const_);
107+
}
105108
ItemEnum::Static(x) => self.check_static(x),
106109
ItemEnum::ForeignType => {} // nop
107110
ItemEnum::Macro(x) => self.check_macro(x),
@@ -231,8 +234,8 @@ impl<'a> Validator<'a> {
231234
self.check_generics(&x.generics);
232235
}
233236

234-
fn check_constant(&mut self, x: &'a Constant) {
235-
self.check_type(&x.type_);
237+
fn check_constant(&mut self, _x: &'a Constant) {
238+
// nop
236239
}
237240

238241
fn check_static(&mut self, x: &'a Static) {

0 commit comments

Comments
 (0)