1 //! Custom formatting traits used when outputting Graphviz diagrams with the results of a dataflow
4 use rustc_index::bit_set::{BitSet, ChunkedBitSet, HybridBitSet};
5 use rustc_index::vec::Idx;
8 /// An extension to `fmt::Debug` for data that can be better printed with some auxiliary data `C`.
9 pub trait DebugWithContext<C>: Eq + fmt::Debug {
10 fn fmt_with(&self, _ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
11 fmt::Debug::fmt(self, f)
14 /// Print the difference between `self` and `old`.
16 /// This should print nothing if `self == old`.
18 /// `+` and `-` are typically used to indicate differences. However, these characters are
19 /// fairly common and may be needed to print a types representation. If using them to indicate
20 /// a diff, prefix them with the "Unit Separator" control character (␟ U+001F).
21 fn fmt_diff_with(&self, old: &Self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
26 write!(f, "\u{001f}+")?;
27 self.fmt_with(ctxt, f)?;
35 write!(f, "\u{001f}-")?;
40 /// Implements `fmt::Debug` by deferring to `<T as DebugWithContext<C>>::fmt_with`.
41 pub struct DebugWithAdapter<'a, T, C> {
46 impl<T, C> fmt::Debug for DebugWithAdapter<'_, T, C>
48 T: DebugWithContext<C>,
50 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51 self.this.fmt_with(self.ctxt, f)
55 /// Implements `fmt::Debug` by deferring to `<T as DebugWithContext<C>>::fmt_diff_with`.
56 pub struct DebugDiffWithAdapter<'a, T, C> {
62 impl<T, C> fmt::Debug for DebugDiffWithAdapter<'_, T, C>
64 T: DebugWithContext<C>,
66 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
67 self.new.fmt_diff_with(&self.old, self.ctxt, f)
73 impl<T, C> DebugWithContext<C> for BitSet<T>
75 T: Idx + DebugWithContext<C>,
77 fn fmt_with(&self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78 f.debug_set().entries(self.iter().map(|i| DebugWithAdapter { this: i, ctxt })).finish()
81 fn fmt_diff_with(&self, old: &Self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82 let size = self.domain_size();
83 assert_eq!(size, old.domain_size());
85 let mut set_in_self = HybridBitSet::new_empty(size);
86 let mut cleared_in_self = HybridBitSet::new_empty(size);
88 for i in (0..size).map(T::new) {
89 match (self.contains(i), old.contains(i)) {
90 (true, false) => set_in_self.insert(i),
91 (false, true) => cleared_in_self.insert(i),
96 fmt_diff(&set_in_self, &cleared_in_self, ctxt, f)
100 impl<T, C> DebugWithContext<C> for ChunkedBitSet<T>
102 T: Idx + DebugWithContext<C>,
104 fn fmt_with(&self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105 f.debug_set().entries(self.iter().map(|i| DebugWithAdapter { this: i, ctxt })).finish()
108 fn fmt_diff_with(&self, old: &Self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
109 let size = self.domain_size();
110 assert_eq!(size, old.domain_size());
112 let mut set_in_self = HybridBitSet::new_empty(size);
113 let mut cleared_in_self = HybridBitSet::new_empty(size);
115 for i in (0..size).map(T::new) {
116 match (self.contains(i), old.contains(i)) {
117 (true, false) => set_in_self.insert(i),
118 (false, true) => cleared_in_self.insert(i),
123 fmt_diff(&set_in_self, &cleared_in_self, ctxt, f)
128 inserted: &HybridBitSet<T>,
129 removed: &HybridBitSet<T>,
131 f: &mut fmt::Formatter<'_>,
134 T: Idx + DebugWithContext<C>,
136 let mut first = true;
137 for idx in inserted.iter() {
138 let delim = if first {
140 } else if f.alternate() {
146 write!(f, "{}", delim)?;
147 idx.fmt_with(ctxt, f)?;
153 if !inserted.is_empty() && !removed.is_empty() {
158 for idx in removed.iter() {
159 let delim = if first {
161 } else if f.alternate() {
167 write!(f, "{}", delim)?;
168 idx.fmt_with(ctxt, f)?;
175 impl<T, C> DebugWithContext<C> for &'_ T
177 T: DebugWithContext<C>,
179 fn fmt_with(&self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
180 (*self).fmt_with(ctxt, f)
183 fn fmt_diff_with(&self, old: &Self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
184 (*self).fmt_diff_with(*old, ctxt, f)
188 impl<C> DebugWithContext<C> for rustc_middle::mir::Local {}
189 impl<C> DebugWithContext<C> for crate::move_paths::InitIndex {}
191 impl<'tcx, C> DebugWithContext<C> for crate::move_paths::MovePathIndex
193 C: crate::move_paths::HasMoveData<'tcx>,
195 fn fmt_with(&self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
196 write!(f, "{}", ctxt.move_data().move_paths[*self])
200 impl<T, C> DebugWithContext<C> for crate::lattice::Dual<T>
202 T: DebugWithContext<C>,
204 fn fmt_with(&self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
205 (self.0).fmt_with(ctxt, f)
208 fn fmt_diff_with(&self, old: &Self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
209 (self.0).fmt_diff_with(&old.0, ctxt, f)