Skip to content

Commit 72cda98

Browse files
committed
Implement Debug for Place using Place::iterate
1 parent e305df1 commit 72cda98

File tree

1 file changed

+86
-52
lines changed

1 file changed

+86
-52
lines changed

src/librustc/mir/mod.rs

+86-52
Original file line numberDiff line numberDiff line change
@@ -2156,61 +2156,95 @@ impl<'p, 'tcx> FusedIterator for PlaceProjectionsIter<'p, 'tcx> {}
21562156

21572157
impl<'tcx> Debug for Place<'tcx> {
21582158
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
2159-
use self::Place::*;
2160-
2161-
match *self {
2162-
Base(PlaceBase::Local(id)) => write!(fmt, "{:?}", id),
2163-
Base(PlaceBase::Static(box self::Static { ty, kind: StaticKind::Static(def_id) })) => {
2164-
write!(
2165-
fmt,
2166-
"({}: {:?})",
2167-
ty::tls::with(|tcx| tcx.def_path_str(def_id)),
2168-
ty
2169-
)
2170-
},
2171-
Base(PlaceBase::Static(
2172-
box self::Static { ty, kind: StaticKind::Promoted(promoted) })
2173-
) => {
2174-
write!(
2175-
fmt,
2176-
"({:?}: {:?})",
2177-
promoted,
2178-
ty
2179-
)
2180-
},
2181-
Projection(ref data) => match data.elem {
2182-
ProjectionElem::Downcast(Some(name), _index) => {
2183-
write!(fmt, "({:?} as {})", data.base, name)
2184-
}
2185-
ProjectionElem::Downcast(None, index) => {
2186-
write!(fmt, "({:?} as variant#{:?})", data.base, index)
2187-
}
2188-
ProjectionElem::Deref => write!(fmt, "(*{:?})", data.base),
2189-
ProjectionElem::Field(field, ty) => {
2190-
write!(fmt, "({:?}.{:?}: {:?})", data.base, field.index(), ty)
2191-
}
2192-
ProjectionElem::Index(ref index) => write!(fmt, "{:?}[{:?}]", data.base, index),
2193-
ProjectionElem::ConstantIndex {
2194-
offset,
2195-
min_length,
2196-
from_end: false,
2197-
} => write!(fmt, "{:?}[{:?} of {:?}]", data.base, offset, min_length),
2198-
ProjectionElem::ConstantIndex {
2199-
offset,
2200-
min_length,
2201-
from_end: true,
2202-
} => write!(fmt, "{:?}[-{:?} of {:?}]", data.base, offset, min_length),
2203-
ProjectionElem::Subslice { from, to } if to == 0 => {
2204-
write!(fmt, "{:?}[{:?}:]", data.base, from)
2159+
self.iterate(|_place_base, place_projections| {
2160+
// FIXME: remove this collect once we have migrated to slices
2161+
let projs_vec: Vec<_> = place_projections.collect();
2162+
for projection in projs_vec.iter().rev() {
2163+
match projection.elem {
2164+
ProjectionElem::Downcast(_, _) |
2165+
ProjectionElem::Field(_, _) => {
2166+
write!(fmt, "(").unwrap();
2167+
}
2168+
ProjectionElem::Deref => {
2169+
write!(fmt, "(*").unwrap();
2170+
}
2171+
ProjectionElem::Index(_) |
2172+
ProjectionElem::ConstantIndex { .. } |
2173+
ProjectionElem::Subslice { .. } => {}
22052174
}
2206-
ProjectionElem::Subslice { from, to } if from == 0 => {
2207-
write!(fmt, "{:?}[:-{:?}]", data.base, to)
2175+
}
2176+
});
2177+
2178+
self.iterate(|place_base, place_projections| {
2179+
match place_base {
2180+
PlaceBase::Local(id) => {
2181+
write!(fmt, "{:?}", id)?;
22082182
}
2209-
ProjectionElem::Subslice { from, to } => {
2210-
write!(fmt, "{:?}[{:?}:-{:?}]", data.base, from, to)
2183+
PlaceBase::Static(box self::Static { ty, kind: StaticKind::Static(def_id) }) => {
2184+
write!(
2185+
fmt,
2186+
"({}: {:?})",
2187+
ty::tls::with(|tcx| tcx.def_path_str(*def_id)),
2188+
ty
2189+
)?;
2190+
},
2191+
PlaceBase::Static(
2192+
box self::Static { ty, kind: StaticKind::Promoted(promoted) }
2193+
) => {
2194+
write!(
2195+
fmt,
2196+
"({:?}: {:?})",
2197+
promoted,
2198+
ty
2199+
)?;
2200+
},
2201+
}
2202+
2203+
for projection in place_projections {
2204+
match projection.elem {
2205+
ProjectionElem::Downcast(Some(name), _index) => {
2206+
write!(fmt, " as {})", name)?;
2207+
}
2208+
ProjectionElem::Downcast(None, index) => {
2209+
write!(fmt, " as variant#{:?})", index)?;
2210+
}
2211+
ProjectionElem::Deref => {
2212+
write!(fmt, ")")?;
2213+
}
2214+
ProjectionElem::Field(field, ty) => {
2215+
write!(fmt, ".{:?}: {:?})", field.index(), ty)?;
2216+
}
2217+
ProjectionElem::Index(ref index) => {
2218+
write!(fmt, "[{:?}]", index)?;
2219+
}
2220+
ProjectionElem::ConstantIndex {
2221+
offset,
2222+
min_length,
2223+
from_end: false,
2224+
} => {
2225+
write!(fmt, "[{:?} of {:?}]", offset, min_length)?;
2226+
}
2227+
ProjectionElem::ConstantIndex {
2228+
offset,
2229+
min_length,
2230+
from_end: true,
2231+
} => {
2232+
write!(fmt, "[-{:?} of {:?}]", offset, min_length)?;
2233+
}
2234+
ProjectionElem::Subslice { from, to } if to == 0 => {
2235+
write!(fmt, "[{:?}:]", from)?;
2236+
}
2237+
ProjectionElem::Subslice { from, to } if from == 0 => {
2238+
write!(fmt, "[:-{:?}]", to)?;
2239+
}
2240+
ProjectionElem::Subslice { from, to } => {
2241+
write!(fmt, "[{:?}:-{:?}]", from, to)?;
2242+
}
22112243
}
2212-
},
2213-
}
2244+
}
2245+
2246+
Ok(())
2247+
})
22142248
}
22152249
}
22162250

0 commit comments

Comments
 (0)