Skip to content

Commit 5223544

Browse files
committed
Pretty-print aggregates more prettily in MIR.
1 parent 9db76f3 commit 5223544

File tree

1 file changed

+59
-1
lines changed

1 file changed

+59
-1
lines changed

src/librustc/mir/repr.rs

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,10 +727,68 @@ impl<'tcx> Debug for Rvalue<'tcx> {
727727
BinaryOp(ref op, ref a, ref b) => write!(fmt, "{:?}({:?}, {:?})", op, a, b),
728728
UnaryOp(ref op, ref a) => write!(fmt, "{:?}({:?})", op, a),
729729
Box(ref t) => write!(fmt, "Box({:?})", t),
730-
Aggregate(ref kind, ref lvs) => write!(fmt, "Aggregate<{:?}>{:?}", kind, lvs),
731730
InlineAsm(ref asm) => write!(fmt, "InlineAsm({:?})", asm),
732731
Slice { ref input, from_start, from_end } =>
733732
write!(fmt, "{:?}[{:?}..-{:?}]", input, from_start, from_end),
733+
734+
Aggregate(ref kind, ref lvs) => {
735+
use self::AggregateKind::*;
736+
737+
fn fmt_tuple(fmt: &mut Formatter, name: &str, lvs: &[Operand]) -> fmt::Result {
738+
let mut tuple_fmt = fmt.debug_tuple(name);
739+
for lv in lvs {
740+
tuple_fmt.field(lv);
741+
}
742+
tuple_fmt.finish()
743+
}
744+
745+
match *kind {
746+
Vec => write!(fmt, "{:?}", lvs),
747+
748+
Tuple => {
749+
if lvs.len() == 1 {
750+
write!(fmt, "({:?},)", lvs[0])
751+
} else {
752+
fmt_tuple(fmt, "", lvs)
753+
}
754+
}
755+
756+
Adt(adt_def, variant, _) => {
757+
let variant_def = &adt_def.variants[variant];
758+
let name = ty::tls::with(|tcx| tcx.item_path_str(variant_def.did));
759+
760+
match variant_def.kind() {
761+
ty::VariantKind::Unit => write!(fmt, "{}", name),
762+
ty::VariantKind::Tuple => fmt_tuple(fmt, &name, lvs),
763+
ty::VariantKind::Struct => {
764+
let mut struct_fmt = fmt.debug_struct(&name);
765+
for (field, lv) in variant_def.fields.iter().zip(lvs) {
766+
struct_fmt.field(&field.name.as_str(), lv);
767+
}
768+
struct_fmt.finish()
769+
}
770+
}
771+
}
772+
773+
Closure(def_id, _) => ty::tls::with(|tcx| {
774+
if let Some(node_id) = tcx.map.as_local_node_id(def_id) {
775+
let name = format!("[closure@{:?}]", tcx.map.span(node_id));
776+
let mut struct_fmt = fmt.debug_struct(&name);
777+
778+
tcx.with_freevars(node_id, |freevars| {
779+
for (freevar, lv) in freevars.iter().zip(lvs) {
780+
let var_name = tcx.local_var_name_str(freevar.def.var_id());
781+
struct_fmt.field(&var_name, lv);
782+
}
783+
});
784+
785+
struct_fmt.finish()
786+
} else {
787+
write!(fmt, "[closure]")
788+
}
789+
}),
790+
}
791+
}
734792
}
735793
}
736794
}

0 commit comments

Comments
 (0)