1 //! Custom formatting traits used when outputting Graphviz diagrams with the results of a dataflow
4 use rustc_index::bit_set::{BitSet, 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}-")?;
36 self.fmt_with(ctxt, f)
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),
97 for idx in set_in_self.iter() {
98 let delim = if first {
100 } else if f.alternate() {
106 write!(f, "{}", delim)?;
107 idx.fmt_with(ctxt, f)?;
113 if !set_in_self.is_empty() && !cleared_in_self.is_empty() {
118 for idx in cleared_in_self.iter() {
119 let delim = if first {
121 } else if f.alternate() {
127 write!(f, "{}", delim)?;
128 idx.fmt_with(ctxt, f)?;
136 impl<T, C> DebugWithContext<C> for &'_ T
138 T: DebugWithContext<C>,
140 fn fmt_with(&self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
141 (*self).fmt_with(ctxt, f)
144 fn fmt_diff_with(&self, old: &Self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
145 (*self).fmt_diff_with(*old, ctxt, f)
149 impl<C> DebugWithContext<C> for rustc_middle::mir::Local {}
150 impl<C> DebugWithContext<C> for crate::dataflow::move_paths::InitIndex {}
152 impl<'tcx, C> DebugWithContext<C> for crate::dataflow::move_paths::MovePathIndex
154 C: crate::dataflow::move_paths::HasMoveData<'tcx>,
156 fn fmt_with(&self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
157 write!(f, "{}", ctxt.move_data().move_paths[*self])
161 impl<T, C> DebugWithContext<C> for crate::dataflow::lattice::Dual<T>
163 T: DebugWithContext<C>,
165 fn fmt_with(&self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
166 (self.0).fmt_with(ctxt, f)
169 fn fmt_diff_with(&self, old: &Self, ctxt: &C, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170 (self.0).fmt_diff_with(&old.0, ctxt, f)