1 use crate::obligation_forest::{ForestObligation, ObligationForest};
5 use std::io::BufWriter;
7 use std::sync::atomic::AtomicUsize;
8 use std::sync::atomic::Ordering;
10 impl<O: ForestObligation> ObligationForest<O> {
11 /// Creates a graphviz representation of the obligation forest. Given a directory this will
12 /// create files with name of the format `<counter>_<description>.gv`. The counter is
13 /// global and is maintained internally.
15 /// Calling this will do nothing unless the environment variable
16 /// `DUMP_OBLIGATION_FOREST_GRAPHVIZ` is defined.
18 /// A few post-processing that you might want to do make the forest easier to visualize:
20 /// * `sed 's,std::[a-z]*::,,g'` — Deletes the `std::<package>::` prefix of paths.
21 /// * `sed 's,"Binder(TraitPredicate(<\(.*\)>)) (\([^)]*\))","\1 (\2)",'` — Transforms
22 /// `Binder(TraitPredicate(<predicate>))` into just `<predicate>`.
24 pub fn dump_graphviz<P: AsRef<Path>>(&self, dir: P, description: &str) {
25 static COUNTER: AtomicUsize = AtomicUsize::new(0);
27 if var_os("DUMP_OBLIGATION_FOREST_GRAPHVIZ").is_none() {
31 let counter = COUNTER.fetch_add(1, Ordering::AcqRel);
33 let file_path = dir.as_ref().join(format!("{:010}_{}.gv", counter, description));
35 let mut gv_file = BufWriter::new(File::create(file_path).unwrap());
37 dot::render(&self, &mut gv_file).unwrap();
41 impl<'a, O: ForestObligation + 'a> dot::Labeller<'a> for &'a ObligationForest<O> {
43 type Edge = (usize, usize);
45 fn graph_id(&self) -> dot::Id<'_> {
46 dot::Id::new("trait_obligation_forest").unwrap()
49 fn node_id(&self, index: &Self::Node) -> dot::Id<'_> {
50 dot::Id::new(format!("obligation_{}", index)).unwrap()
53 fn node_label(&self, index: &Self::Node) -> dot::LabelText<'_> {
54 let node = &self.nodes[*index];
55 let label = format!("{:?} ({:?})", node.obligation.as_cache_key(), node.state.get());
57 dot::LabelText::LabelStr(label.into())
60 fn edge_label(&self, (_index_source, _index_target): &Self::Edge) -> dot::LabelText<'_> {
61 dot::LabelText::LabelStr("".into())
65 impl<'a, O: ForestObligation + 'a> dot::GraphWalk<'a> for &'a ObligationForest<O> {
67 type Edge = (usize, usize);
69 fn nodes(&self) -> dot::Nodes<'_, Self::Node> {
70 (0..self.nodes.len()).collect()
73 fn edges(&self) -> dot::Edges<'_, Self::Edge> {
76 let node = &self.nodes[i];
78 node.dependents.iter().map(move |&d| (d, i))
83 fn source(&self, (s, _): &Self::Edge) -> Self::Node {
87 fn target(&self, (_, t): &Self::Edge) -> Self::Node {