From ec857e1e099c135f48c8706cdea65497309de2b3 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 29 Jan 2018 10:32:11 +0100 Subject: [PATCH] Deduplicate code in rustdoc --- src/librustdoc/clean/mod.rs | 62 ++++++++++++++----------------------- 1 file changed, 24 insertions(+), 38 deletions(-) diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index 2372153963a1..5d4addce2c43 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -2511,25 +2511,7 @@ impl Clean 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 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) }