]> git.lizzy.rs Git - rust.git/commitdiff
rustdoc: Pretty-print assoc const defaults on-demand
authorNoah Lev <camelidcamel@gmail.com>
Sat, 11 Dec 2021 22:15:52 +0000 (14:15 -0800)
committerNoah Lev <camelidcamel@gmail.com>
Sat, 11 Dec 2021 22:22:34 +0000 (14:22 -0800)
This should improve performance, clean up the code, and help pave the
way for #83035.

src/librustdoc/clean/mod.rs
src/librustdoc/clean/types.rs
src/librustdoc/html/render/mod.rs
src/librustdoc/json/conversions.rs

index f11fa0295c52220c9f7d9546973245dfff3d6d12..2b4466d4041705d98cb70d5f586466d9b9f11c2b 100644 (file)
@@ -912,7 +912,9 @@ fn clean(&self, cx: &mut DocContext<'_>) -> Item {
         cx.with_param_env(local_did, |cx| {
             let inner = match self.kind {
                 hir::TraitItemKind::Const(ref ty, default) => {
-                    AssocConstItem(ty.clean(cx), default.map(|e| print_const_expr(cx.tcx, e)))
+                    let default =
+                        default.map(|e| ConstantKind::Local { def_id: local_did, body: e });
+                    AssocConstItem(ty.clean(cx), default)
                 }
                 hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
                     let mut m = clean_function(cx, sig, &self.generics, body);
@@ -959,7 +961,8 @@ fn clean(&self, cx: &mut DocContext<'_>) -> Item {
         cx.with_param_env(local_did, |cx| {
             let inner = match self.kind {
                 hir::ImplItemKind::Const(ref ty, expr) => {
-                    AssocConstItem(ty.clean(cx), Some(print_const_expr(cx.tcx, expr)))
+                    let default = Some(ConstantKind::Local { def_id: local_did, body: expr });
+                    AssocConstItem(ty.clean(cx), default)
                 }
                 hir::ImplItemKind::Fn(ref sig, body) => {
                     let mut m = clean_function(cx, sig, &self.generics, body);
@@ -1009,7 +1012,7 @@ fn clean(&self, cx: &mut DocContext<'_>) -> Item {
             ty::AssocKind::Const => {
                 let ty = tcx.type_of(self.def_id);
                 let default = if self.defaultness.has_value() {
-                    Some(inline::print_inlined_const(tcx, self.def_id))
+                    Some(ConstantKind::Extern { def_id: self.def_id })
                 } else {
                     None
                 };
index 1fe4aa9023ea310193eb6f2494b2d8f47b64122e..6ce6d889150108e5b543ff222238f9b855f2f4cd 100644 (file)
@@ -670,7 +670,7 @@ pub fn from_def_id_and_attrs_and_parts(
     MacroItem(Macro),
     ProcMacroItem(ProcMacro),
     PrimitiveItem(PrimitiveType),
-    AssocConstItem(Type, Option<String>),
+    AssocConstItem(Type, Option<ConstantKind>),
     /// An associated item in a trait or trait impl.
     ///
     /// The bounds may be non-empty if there is a `where` clause.
@@ -2153,7 +2153,21 @@ impl Path {
 
 impl Constant {
     crate fn expr(&self, tcx: TyCtxt<'_>) -> String {
-        match self.kind {
+        self.kind.expr(tcx)
+    }
+
+    crate fn value(&self, tcx: TyCtxt<'_>) -> Option<String> {
+        self.kind.value(tcx)
+    }
+
+    crate fn is_literal(&self, tcx: TyCtxt<'_>) -> bool {
+        self.kind.is_literal(tcx)
+    }
+}
+
+impl ConstantKind {
+    crate fn expr(&self, tcx: TyCtxt<'_>) -> String {
+        match *self {
             ConstantKind::TyConst { ref expr } => expr.clone(),
             ConstantKind::Extern { def_id } => print_inlined_const(tcx, def_id),
             ConstantKind::Local { body, .. } | ConstantKind::Anonymous { body } => {
@@ -2163,7 +2177,7 @@ impl Constant {
     }
 
     crate fn value(&self, tcx: TyCtxt<'_>) -> Option<String> {
-        match self.kind {
+        match *self {
             ConstantKind::TyConst { .. } | ConstantKind::Anonymous { .. } => None,
             ConstantKind::Extern { def_id } | ConstantKind::Local { def_id, .. } => {
                 print_evaluated_const(tcx, def_id)
@@ -2172,7 +2186,7 @@ impl Constant {
     }
 
     crate fn is_literal(&self, tcx: TyCtxt<'_>) -> bool {
-        match self.kind {
+        match *self {
             ConstantKind::TyConst { .. } => false,
             ConstantKind::Extern { def_id } => def_id.as_local().map_or(false, |def_id| {
                 is_literal_expr(tcx, tcx.hir().local_def_id_to_hir_id(def_id))
index 166e084012724e18999eb920770880617556c5ad..db31751176bdeb0e8f4a2166c570fe970f7a8dd1 100644 (file)
@@ -762,7 +762,7 @@ fn assoc_const(
     w: &mut Buffer,
     it: &clean::Item,
     ty: &clean::Type,
-    _default: Option<&String>,
+    _default: Option<&clean::ConstantKind>,
     link: AssocItemLink<'_>,
     extra: &str,
     cx: &Context<'_>,
index 7fc295747f41a5657419d11707034609dee05f1f..ee29bfcc7a48d62080d3e54de4278f61286c402a 100644 (file)
@@ -219,7 +219,9 @@ fn from_clean_item(item: clean::Item, tcx: TyCtxt<'_>) -> ItemEnum {
         MacroItem(m) => ItemEnum::Macro(m.source),
         ProcMacroItem(m) => ItemEnum::ProcMacro(m.into_tcx(tcx)),
         PrimitiveItem(p) => ItemEnum::PrimitiveType(p.as_sym().to_string()),
-        AssocConstItem(t, s) => ItemEnum::AssocConst { type_: t.into_tcx(tcx), default: s },
+        AssocConstItem(ty, default) => {
+            ItemEnum::AssocConst { type_: ty.into_tcx(tcx), default: default.map(|c| c.expr(tcx)) }
+        }
         AssocTypeItem(g, t) => ItemEnum::AssocType {
             bounds: g.into_iter().map(|x| x.into_tcx(tcx)).collect(),
             default: t.map(|x| x.into_tcx(tcx)),