Skip to content

Commit d38ae76

Browse files
committed
rustdoc: Pretty-print assoc const defaults on-demand
This should improve performance, clean up the code, and help pave the way for rust-lang#83035.
1 parent b8dc6aa commit d38ae76

File tree

4 files changed

+28
-9
lines changed

4 files changed

+28
-9
lines changed

src/librustdoc/clean/mod.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,9 @@ impl Clean<Item> for hir::TraitItem<'_> {
912912
cx.with_param_env(local_did, |cx| {
913913
let inner = match self.kind {
914914
hir::TraitItemKind::Const(ref ty, default) => {
915-
AssocConstItem(ty.clean(cx), default.map(|e| print_const_expr(cx.tcx, e)))
915+
let default =
916+
default.map(|e| ConstantKind::Local { def_id: local_did, body: e });
917+
AssocConstItem(ty.clean(cx), default)
916918
}
917919
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
918920
let mut m = clean_function(cx, sig, &self.generics, body);
@@ -959,7 +961,8 @@ impl Clean<Item> for hir::ImplItem<'_> {
959961
cx.with_param_env(local_did, |cx| {
960962
let inner = match self.kind {
961963
hir::ImplItemKind::Const(ref ty, expr) => {
962-
AssocConstItem(ty.clean(cx), Some(print_const_expr(cx.tcx, expr)))
964+
let default = Some(ConstantKind::Local { def_id: local_did, body: expr });
965+
AssocConstItem(ty.clean(cx), default)
963966
}
964967
hir::ImplItemKind::Fn(ref sig, body) => {
965968
let mut m = clean_function(cx, sig, &self.generics, body);
@@ -1009,7 +1012,7 @@ impl Clean<Item> for ty::AssocItem {
10091012
ty::AssocKind::Const => {
10101013
let ty = tcx.type_of(self.def_id);
10111014
let default = if self.defaultness.has_value() {
1012-
Some(inline::print_inlined_const(tcx, self.def_id))
1015+
Some(ConstantKind::Extern { def_id: self.def_id })
10131016
} else {
10141017
None
10151018
};

src/librustdoc/clean/types.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -670,7 +670,7 @@ crate enum ItemKind {
670670
MacroItem(Macro),
671671
ProcMacroItem(ProcMacro),
672672
PrimitiveItem(PrimitiveType),
673-
AssocConstItem(Type, Option<String>),
673+
AssocConstItem(Type, Option<ConstantKind>),
674674
/// An associated item in a trait or trait impl.
675675
///
676676
/// The bounds may be non-empty if there is a `where` clause.
@@ -2153,7 +2153,21 @@ crate enum ConstantKind {
21532153

21542154
impl Constant {
21552155
crate fn expr(&self, tcx: TyCtxt<'_>) -> String {
2156-
match self.kind {
2156+
self.kind.expr(tcx)
2157+
}
2158+
2159+
crate fn value(&self, tcx: TyCtxt<'_>) -> Option<String> {
2160+
self.kind.value(tcx)
2161+
}
2162+
2163+
crate fn is_literal(&self, tcx: TyCtxt<'_>) -> bool {
2164+
self.kind.is_literal(tcx)
2165+
}
2166+
}
2167+
2168+
impl ConstantKind {
2169+
crate fn expr(&self, tcx: TyCtxt<'_>) -> String {
2170+
match *self {
21572171
ConstantKind::TyConst { ref expr } => expr.clone(),
21582172
ConstantKind::Extern { def_id } => print_inlined_const(tcx, def_id),
21592173
ConstantKind::Local { body, .. } | ConstantKind::Anonymous { body } => {
@@ -2163,7 +2177,7 @@ impl Constant {
21632177
}
21642178

21652179
crate fn value(&self, tcx: TyCtxt<'_>) -> Option<String> {
2166-
match self.kind {
2180+
match *self {
21672181
ConstantKind::TyConst { .. } | ConstantKind::Anonymous { .. } => None,
21682182
ConstantKind::Extern { def_id } | ConstantKind::Local { def_id, .. } => {
21692183
print_evaluated_const(tcx, def_id)
@@ -2172,7 +2186,7 @@ impl Constant {
21722186
}
21732187

21742188
crate fn is_literal(&self, tcx: TyCtxt<'_>) -> bool {
2175-
match self.kind {
2189+
match *self {
21762190
ConstantKind::TyConst { .. } => false,
21772191
ConstantKind::Extern { def_id } => def_id.as_local().map_or(false, |def_id| {
21782192
is_literal_expr(tcx, tcx.hir().local_def_id_to_hir_id(def_id))

src/librustdoc/html/render/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ fn assoc_const(
762762
w: &mut Buffer,
763763
it: &clean::Item,
764764
ty: &clean::Type,
765-
_default: Option<&String>,
765+
_default: Option<&clean::ConstantKind>,
766766
link: AssocItemLink<'_>,
767767
extra: &str,
768768
cx: &Context<'_>,

src/librustdoc/json/conversions.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,9 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
219219
MacroItem(m) => ItemEnum::Macro(m.source),
220220
ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_tcx(tcx)),
221221
PrimitiveItem(p) => ItemEnum::PrimitiveType(p.as_sym().to_string()),
222-
AssocConstItem(t, s) => ItemEnum::AssocConst { type_: t.into_tcx(tcx), default: s },
222+
AssocConstItem(ty, default) => {
223+
ItemEnum::AssocConst { type_: ty.into_tcx(tcx), default: default.map(|c| c.expr(tcx)) }
224+
}
223225
AssocTypeItem(g, t) => ItemEnum::AssocType {
224226
bounds: g.into_iter().map(|x| x.into_tcx(tcx)).collect(),
225227
default: t.map(|x| x.into_tcx(tcx)),

0 commit comments

Comments
 (0)