about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>2018-01-29 10:32:11 +0100
committerOliver Schneider <git-spam-no-reply9815368754983@oli-obk.de>2018-03-08 08:34:13 +0100
commitec857e1e099c135f48c8706cdea65497309de2b3 (patch)
tree7396cc88eca58636e93429a25d423d3c1f3140bf
parentd57a109203526c2aa1c4bf88984726a82dd4bec8 (diff)
downloadrust-ec857e1e099c135f48c8706cdea65497309de2b3.tar.gz
rust-ec857e1e099c135f48c8706cdea65497309de2b3.zip
Deduplicate code in rustdoc
-rw-r--r--src/librustdoc/clean/mod.rs62
1 files changed, 24 insertions, 38 deletions
diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs
index 2372153963a..5d4addce2c4 100644
--- a/src/librustdoc/clean/mod.rs
+++ b/src/librustdoc/clean/mod.rs
@@ -2511,25 +2511,7 @@ impl Clean<Type> for hir::Ty {
                         ty: cx.tcx.types.usize
                     })
                 });
-                let n = match n.val {
-                    ConstVal::Unevaluated(def_id, _) => {
-                        if let Some(node_id) = cx.tcx.hir.as_local_node_id(def_id) {
-                            print_const_expr(cx, cx.tcx.hir.body_owned_by(node_id))
-                        } else {
-                            inline::print_inlined_const(cx, def_id)
-                        }
-                    },
-                    ConstVal::Value(val) => {
-                        let mut s = String::new();
-                        ::rustc::mir::print_miri_value(val, n.ty, &mut s).unwrap();
-                        // array lengths are obviously usize
-                        if s.ends_with("usize") {
-                            let n = s.len() - "usize".len();
-                            s.truncate(n);
-                        }
-                        s
-                    },
-                };
+                let n = print_const(cx, n);
                 Array(box ty.clean(cx), n)
             },
             TyTup(ref tys) => Tuple(tys.clean(cx)),
@@ -2656,25 +2638,7 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
                         n = new_n;
                     }
                 };
-                let n = match n.val {
-                    ConstVal::Unevaluated(def_id, _) => {
-                        if let Some(node_id) = cx.tcx.hir.as_local_node_id(def_id) {
-                            print_const_expr(cx, cx.tcx.hir.body_owned_by(node_id))
-                        } else {
-                            inline::print_inlined_const(cx, def_id)
-                        }
-                    },
-                    ConstVal::Value(val) => {
-                        let mut s = String::new();
-                        ::rustc::mir::print_miri_value(val, n.ty, &mut s).unwrap();
-                        // array lengths are obviously usize
-                        if s.ends_with("usize") {
-                            let n = s.len() - "usize".len();
-                            s.truncate(n);
-                        }
-                        s
-                    },
-                };
+                let n = print_const(cx, n);
                 Array(box ty.clean(cx), n)
             }
             ty::TyRawPtr(mt) => RawPointer(mt.mutbl.clean(cx), box mt.ty.clean(cx)),
@@ -3658,6 +3622,28 @@ fn name_from_pat(p: &hir::Pat) -> String {
     }
 }
 
+fn print_const(cx: &DocContext, n: &ty::Const) -> String {
+    match n.val {
+        ConstVal::Unevaluated(def_id, _) => {
+            if let Some(node_id) = cx.tcx.hir.as_local_node_id(def_id) {
+                print_const_expr(cx, cx.tcx.hir.body_owned_by(node_id))
+            } else {
+                inline::print_inlined_const(cx, def_id)
+            }
+        },
+        ConstVal::Value(val) => {
+            let mut s = String::new();
+            ::rustc::mir::print_miri_value(val, n.ty, &mut s).unwrap();
+            // array lengths are obviously usize
+            if s.ends_with("usize") {
+                let n = s.len() - "usize".len();
+                s.truncate(n);
+            }
+            s
+        },
+    }
+}
+
 fn print_const_expr(cx: &DocContext, body: hir::BodyId) -> String {
     cx.tcx.hir.node_to_pretty_string(body.node_id)
 }