Deduplicate code in rustdoc

This commit is contained in:
Oliver Schneider 2018-01-29 10:32:11 +01:00
parent d57a109203
commit ec857e1e09
No known key found for this signature in database
GPG key ID: A69F8D225B3AD7D9

View file

@ -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)
}