1
Fork 0

Implement Debug for Place using Place::iterate

This commit is contained in:
Santiago Pastorino 2019-04-24 19:21:10 -03:00
parent e305df1846
commit 72cda98e48

View file

@ -2156,62 +2156,96 @@ impl<'p, 'tcx> FusedIterator for PlaceProjectionsIter<'p, 'tcx> {}
impl<'tcx> Debug for Place<'tcx> { impl<'tcx> Debug for Place<'tcx> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
use self::Place::*; self.iterate(|_place_base, place_projections| {
// FIXME: remove this collect once we have migrated to slices
let projs_vec: Vec<_> = place_projections.collect();
for projection in projs_vec.iter().rev() {
match projection.elem {
ProjectionElem::Downcast(_, _) |
ProjectionElem::Field(_, _) => {
write!(fmt, "(").unwrap();
}
ProjectionElem::Deref => {
write!(fmt, "(*").unwrap();
}
ProjectionElem::Index(_) |
ProjectionElem::ConstantIndex { .. } |
ProjectionElem::Subslice { .. } => {}
}
}
});
match *self { self.iterate(|place_base, place_projections| {
Base(PlaceBase::Local(id)) => write!(fmt, "{:?}", id), match place_base {
Base(PlaceBase::Static(box self::Static { ty, kind: StaticKind::Static(def_id) })) => { PlaceBase::Local(id) => {
write!(fmt, "{:?}", id)?;
}
PlaceBase::Static(box self::Static { ty, kind: StaticKind::Static(def_id) }) => {
write!( write!(
fmt, fmt,
"({}: {:?})", "({}: {:?})",
ty::tls::with(|tcx| tcx.def_path_str(def_id)), ty::tls::with(|tcx| tcx.def_path_str(*def_id)),
ty ty
) )?;
}, },
Base(PlaceBase::Static( PlaceBase::Static(
box self::Static { ty, kind: StaticKind::Promoted(promoted) }) box self::Static { ty, kind: StaticKind::Promoted(promoted) }
) => { ) => {
write!( write!(
fmt, fmt,
"({:?}: {:?})", "({:?}: {:?})",
promoted, promoted,
ty ty
) )?;
}, },
Projection(ref data) => match data.elem { }
for projection in place_projections {
match projection.elem {
ProjectionElem::Downcast(Some(name), _index) => { ProjectionElem::Downcast(Some(name), _index) => {
write!(fmt, "({:?} as {})", data.base, name) write!(fmt, " as {})", name)?;
} }
ProjectionElem::Downcast(None, index) => { ProjectionElem::Downcast(None, index) => {
write!(fmt, "({:?} as variant#{:?})", data.base, index) write!(fmt, " as variant#{:?})", index)?;
}
ProjectionElem::Deref => {
write!(fmt, ")")?;
} }
ProjectionElem::Deref => write!(fmt, "(*{:?})", data.base),
ProjectionElem::Field(field, ty) => { ProjectionElem::Field(field, ty) => {
write!(fmt, "({:?}.{:?}: {:?})", data.base, field.index(), ty) write!(fmt, ".{:?}: {:?})", field.index(), ty)?;
}
ProjectionElem::Index(ref index) => {
write!(fmt, "[{:?}]", index)?;
} }
ProjectionElem::Index(ref index) => write!(fmt, "{:?}[{:?}]", data.base, index),
ProjectionElem::ConstantIndex { ProjectionElem::ConstantIndex {
offset, offset,
min_length, min_length,
from_end: false, from_end: false,
} => write!(fmt, "{:?}[{:?} of {:?}]", data.base, offset, min_length), } => {
write!(fmt, "[{:?} of {:?}]", offset, min_length)?;
}
ProjectionElem::ConstantIndex { ProjectionElem::ConstantIndex {
offset, offset,
min_length, min_length,
from_end: true, from_end: true,
} => write!(fmt, "{:?}[-{:?} of {:?}]", data.base, offset, min_length), } => {
write!(fmt, "[-{:?} of {:?}]", offset, min_length)?;
}
ProjectionElem::Subslice { from, to } if to == 0 => { ProjectionElem::Subslice { from, to } if to == 0 => {
write!(fmt, "{:?}[{:?}:]", data.base, from) write!(fmt, "[{:?}:]", from)?;
} }
ProjectionElem::Subslice { from, to } if from == 0 => { ProjectionElem::Subslice { from, to } if from == 0 => {
write!(fmt, "{:?}[:-{:?}]", data.base, to) write!(fmt, "[:-{:?}]", to)?;
} }
ProjectionElem::Subslice { from, to } => { ProjectionElem::Subslice { from, to } => {
write!(fmt, "{:?}[{:?}:-{:?}]", data.base, from, to) write!(fmt, "[{:?}:-{:?}]", from, to)?;
} }
},
} }
} }
Ok(())
})
}
} }
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////