1
Fork 0

Remove another use of as_results_cursor.

The new code is a little clunky, but I couldn't see how to make it
better.
This commit is contained in:
Nicholas Nethercote 2023-11-23 18:19:43 +11:00
parent 3dea72aa1b
commit cf82b410f9
3 changed files with 62 additions and 40 deletions

View file

@ -12,7 +12,7 @@ use rustc_middle::mir::graphviz_safe_def_name;
use rustc_middle::mir::{self, BasicBlock, Body, Location};
use super::fmt::{DebugDiffWithAdapter, DebugWithAdapter, DebugWithContext};
use super::{Analysis, CallReturnPlaces, Direction, Results, ResultsRefCursor, ResultsVisitor};
use super::{Analysis, CallReturnPlaces, Direction, Results, ResultsCursor, ResultsVisitor};
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) enum OutputStyle {
@ -29,27 +29,31 @@ impl OutputStyle {
}
}
pub(crate) struct Formatter<'res, 'mir, 'tcx, A>
pub(crate) struct Formatter<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
body: &'mir Body<'tcx>,
results: RefCell<&'res mut Results<'tcx, A>>,
results: RefCell<Option<Results<'tcx, A>>>,
style: OutputStyle,
reachable: BitSet<BasicBlock>,
}
impl<'res, 'mir, 'tcx, A> Formatter<'res, 'mir, 'tcx, A>
impl<'mir, 'tcx, A> Formatter<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
pub(crate) fn new(
body: &'mir Body<'tcx>,
results: &'res mut Results<'tcx, A>,
results: Results<'tcx, A>,
style: OutputStyle,
) -> Self {
let reachable = mir::traversal::reachable_as_bitset(body);
Formatter { body, results: results.into(), style, reachable }
Formatter { body, results: Some(results).into(), style, reachable }
}
pub(crate) fn into_results(self) -> Results<'tcx, A> {
self.results.into_inner().unwrap()
}
}
@ -69,7 +73,7 @@ fn dataflow_successors(body: &Body<'_>, bb: BasicBlock) -> Vec<CfgEdge> {
.collect()
}
impl<'tcx, A> dot::Labeller<'_> for Formatter<'_, '_, 'tcx, A>
impl<'tcx, A> dot::Labeller<'_> for Formatter<'_, 'tcx, A>
where
A: Analysis<'tcx>,
A::Domain: DebugWithContext<A>,
@ -88,14 +92,19 @@ where
fn node_label(&self, block: &Self::Node) -> dot::LabelText<'_> {
let mut label = Vec::new();
let mut results = self.results.borrow_mut();
let mut fmt = BlockFormatter {
results: results.as_results_cursor(self.body),
style: self.style,
bg: Background::Light,
};
self.results.replace_with(|results| {
// `Formatter::result` is a `RefCell<Option<_>>` so we can replace
// the value with `None`, move it into the results cursor, move it
// back out, and return it to the refcell wrapped in `Some`.
let mut fmt = BlockFormatter {
results: results.take().unwrap().into_results_cursor(self.body),
style: self.style,
bg: Background::Light,
};
fmt.write_node_label(&mut label, *block).unwrap();
fmt.write_node_label(&mut label, *block).unwrap();
Some(fmt.results.into_results())
});
dot::LabelText::html(String::from_utf8(label).unwrap())
}
@ -109,7 +118,7 @@ where
}
}
impl<'mir, 'tcx, A> dot::GraphWalk<'mir> for Formatter<'_, 'mir, 'tcx, A>
impl<'mir, 'tcx, A> dot::GraphWalk<'mir> for Formatter<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
@ -143,16 +152,16 @@ where
}
}
struct BlockFormatter<'res, 'mir, 'tcx, A>
struct BlockFormatter<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
{
results: ResultsRefCursor<'res, 'mir, 'tcx, A>,
results: ResultsCursor<'mir, 'tcx, A>,
bg: Background,
style: OutputStyle,
}
impl<'res, 'mir, 'tcx, A> BlockFormatter<'res, 'mir, 'tcx, A>
impl<'mir, 'tcx, A> BlockFormatter<'mir, 'tcx, A>
where
A: Analysis<'tcx>,
A::Domain: DebugWithContext<A>,