Skip to content

Commit

Permalink
Implement Debug for Place using Place::iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino committed Apr 25, 2019
1 parent e305df1 commit 72cda98
Showing 1 changed file with 86 additions and 52 deletions.
138 changes: 86 additions & 52 deletions src/librustc/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2156,61 +2156,95 @@ impl<'p, 'tcx> FusedIterator for PlaceProjectionsIter<'p, 'tcx> {}

impl<'tcx> Debug for Place<'tcx> {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
use self::Place::*;

match *self {
Base(PlaceBase::Local(id)) => write!(fmt, "{:?}", id),
Base(PlaceBase::Static(box self::Static { ty, kind: StaticKind::Static(def_id) })) => {
write!(
fmt,
"({}: {:?})",
ty::tls::with(|tcx| tcx.def_path_str(def_id)),
ty
)
},
Base(PlaceBase::Static(
box self::Static { ty, kind: StaticKind::Promoted(promoted) })
) => {
write!(
fmt,
"({:?}: {:?})",
promoted,
ty
)
},
Projection(ref data) => match data.elem {
ProjectionElem::Downcast(Some(name), _index) => {
write!(fmt, "({:?} as {})", data.base, name)
}
ProjectionElem::Downcast(None, index) => {
write!(fmt, "({:?} as variant#{:?})", data.base, index)
}
ProjectionElem::Deref => write!(fmt, "(*{:?})", data.base),
ProjectionElem::Field(field, ty) => {
write!(fmt, "({:?}.{:?}: {:?})", data.base, field.index(), ty)
}
ProjectionElem::Index(ref index) => write!(fmt, "{:?}[{:?}]", data.base, index),
ProjectionElem::ConstantIndex {
offset,
min_length,
from_end: false,
} => write!(fmt, "{:?}[{:?} of {:?}]", data.base, offset, min_length),
ProjectionElem::ConstantIndex {
offset,
min_length,
from_end: true,
} => write!(fmt, "{:?}[-{:?} of {:?}]", data.base, offset, min_length),
ProjectionElem::Subslice { from, to } if to == 0 => {
write!(fmt, "{:?}[{:?}:]", data.base, from)
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 { .. } => {}
}
ProjectionElem::Subslice { from, to } if from == 0 => {
write!(fmt, "{:?}[:-{:?}]", data.base, to)
}
});

self.iterate(|place_base, place_projections| {
match place_base {
PlaceBase::Local(id) => {
write!(fmt, "{:?}", id)?;
}
ProjectionElem::Subslice { from, to } => {
write!(fmt, "{:?}[{:?}:-{:?}]", data.base, from, to)
PlaceBase::Static(box self::Static { ty, kind: StaticKind::Static(def_id) }) => {
write!(
fmt,
"({}: {:?})",
ty::tls::with(|tcx| tcx.def_path_str(*def_id)),
ty
)?;
},
PlaceBase::Static(
box self::Static { ty, kind: StaticKind::Promoted(promoted) }
) => {
write!(
fmt,
"({:?}: {:?})",
promoted,
ty
)?;
},
}

for projection in place_projections {
match projection.elem {
ProjectionElem::Downcast(Some(name), _index) => {
write!(fmt, " as {})", name)?;
}
ProjectionElem::Downcast(None, index) => {
write!(fmt, " as variant#{:?})", index)?;
}
ProjectionElem::Deref => {
write!(fmt, ")")?;
}
ProjectionElem::Field(field, ty) => {
write!(fmt, ".{:?}: {:?})", field.index(), ty)?;
}
ProjectionElem::Index(ref index) => {
write!(fmt, "[{:?}]", index)?;
}
ProjectionElem::ConstantIndex {
offset,
min_length,
from_end: false,
} => {
write!(fmt, "[{:?} of {:?}]", offset, min_length)?;
}
ProjectionElem::ConstantIndex {
offset,
min_length,
from_end: true,
} => {
write!(fmt, "[-{:?} of {:?}]", offset, min_length)?;
}
ProjectionElem::Subslice { from, to } if to == 0 => {
write!(fmt, "[{:?}:]", from)?;
}
ProjectionElem::Subslice { from, to } if from == 0 => {
write!(fmt, "[:-{:?}]", to)?;
}
ProjectionElem::Subslice { from, to } => {
write!(fmt, "[{:?}:-{:?}]", from, to)?;
}
}
},
}
}

Ok(())
})
}
}

Expand Down

0 comments on commit 72cda98

Please sign in to comment.