Skip to content

Commit 0901052

Browse files
committed
Print rendered miri values in rustdoc
1 parent 1836349 commit 0901052

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

src/librustc/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1778,7 +1778,7 @@ fn fmt_const_val<W: Write>(fmt: &mut W, const_val: &ty::Const) -> fmt::Result {
17781778
}
17791779
}
17801780

1781-
fn print_miri_value<W: Write>(value: Value, ty: Ty, f: &mut W) -> fmt::Result {
1781+
pub fn print_miri_value<W: Write>(value: Value, ty: Ty, f: &mut W) -> fmt::Result {
17821782
use ty::TypeVariants::*;
17831783
use rustc_const_math::ConstFloat;
17841784
match (value, &ty.sty) {

src/librustdoc/clean/mod.rs

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,14 +2066,24 @@ impl Clean<Type> for hir::Ty {
20662066
promoted: None
20672067
};
20682068
let n = cx.tcx.const_eval(param_env.and(cid)).unwrap();
2069-
let n = if let ConstVal::Unevaluated(def_id, _) = n.val {
2070-
if let Some(node_id) = cx.tcx.hir.as_local_node_id(def_id) {
2071-
print_const_expr(cx, cx.tcx.hir.body_owned_by(node_id))
2072-
} else {
2073-
inline::print_inlined_const(cx, def_id)
2074-
}
2075-
} else {
2076-
format!("{:?}", n)
2069+
let n = match n.val {
2070+
ConstVal::Unevaluated(def_id, _) => {
2071+
if let Some(node_id) = cx.tcx.hir.as_local_node_id(def_id) {
2072+
print_const_expr(cx, cx.tcx.hir.body_owned_by(node_id))
2073+
} else {
2074+
inline::print_inlined_const(cx, def_id)
2075+
}
2076+
},
2077+
ConstVal::Value(val) => {
2078+
let mut s = String::new();
2079+
::rustc::mir::print_miri_value(val, n.ty, &mut s).unwrap();
2080+
// array lengths are obviously usize
2081+
if s.ends_with("usize") {
2082+
let n = s.len() - "usize".len();
2083+
s.truncate(n);
2084+
}
2085+
s
2086+
},
20772087
};
20782088
Array(box ty.clean(cx), n)
20792089
},
@@ -2199,14 +2209,24 @@ impl<'tcx> Clean<Type> for Ty<'tcx> {
21992209
};
22002210
n = cx.tcx.const_eval(param_env.and(cid)).unwrap()
22012211
};
2202-
let n = if let ConstVal::Unevaluated(def_id, _) = n.val {
2203-
if let Some(node_id) = cx.tcx.hir.as_local_node_id(def_id) {
2204-
print_const_expr(cx, cx.tcx.hir.body_owned_by(node_id))
2205-
} else {
2206-
inline::print_inlined_const(cx, def_id)
2207-
}
2208-
} else {
2209-
format!("{:?}", n)
2212+
let n = match n.val {
2213+
ConstVal::Unevaluated(def_id, _) => {
2214+
if let Some(node_id) = cx.tcx.hir.as_local_node_id(def_id) {
2215+
print_const_expr(cx, cx.tcx.hir.body_owned_by(node_id))
2216+
} else {
2217+
inline::print_inlined_const(cx, def_id)
2218+
}
2219+
},
2220+
ConstVal::Value(val) => {
2221+
let mut s = String::new();
2222+
::rustc::mir::print_miri_value(val, n.ty, &mut s).unwrap();
2223+
// array lengths are obviously usize
2224+
if s.ends_with("usize") {
2225+
let n = s.len() - "usize".len();
2226+
s.truncate(n);
2227+
}
2228+
s
2229+
},
22102230
};
22112231
Array(box ty.clean(cx), n)
22122232
}

0 commit comments

Comments
 (0)