Skip to content

Commit 16d8a91

Browse files
committed
Auto merge of rust-lang#91812 - camelid:assoc-const-lazy, r=GuillaumeGomez
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.
2 parents 9b45f04 + 719d7a5 commit 16d8a91

File tree

4 files changed

+31
-20
lines changed

4 files changed

+31
-20
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

+4-12
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,6 @@ fn assoc_const(
762762
w: &mut Buffer,
763763
it: &clean::Item,
764764
ty: &clean::Type,
765-
_default: Option<&String>,
766765
link: AssocItemLink<'_>,
767766
extra: &str,
768767
cx: &Context<'_>,
@@ -958,15 +957,9 @@ fn render_assoc_item(
958957
clean::MethodItem(ref m, _) => {
959958
method(w, item, m.header, &m.generics, &m.decl, link, parent, cx, render_mode)
960959
}
961-
clean::AssocConstItem(ref ty, ref default) => assoc_const(
962-
w,
963-
item,
964-
ty,
965-
default.as_ref(),
966-
link,
967-
if parent == ItemType::Trait { " " } else { "" },
968-
cx,
969-
),
960+
clean::AssocConstItem(ref ty, _) => {
961+
assoc_const(w, item, ty, link, if parent == ItemType::Trait { " " } else { "" }, cx)
962+
}
970963
clean::AssocTypeItem(ref bounds, ref default) => assoc_type(
971964
w,
972965
item,
@@ -1467,7 +1460,7 @@ fn render_impl(
14671460
w.write_str("</h4>");
14681461
w.write_str("</div>");
14691462
}
1470-
clean::AssocConstItem(ref ty, ref default) => {
1463+
clean::AssocConstItem(ref ty, _) => {
14711464
let source_id = format!("{}.{}", item_type, name);
14721465
let id = cx.derive_id(source_id.clone());
14731466
write!(
@@ -1482,7 +1475,6 @@ fn render_impl(
14821475
w,
14831476
item,
14841477
ty,
1485-
default.as_ref(),
14861478
link.anchor(if trait_.is_some() { &source_id } else { &id }),
14871479
"",
14881480
cx,

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)