Skip to content

Commit cf82b41

Browse files
committed
Remove another use of as_results_cursor.
The new code is a little clunky, but I couldn't see how to make it better.
1 parent 3dea72a commit cf82b41

File tree

3 files changed

+62
-40
lines changed

3 files changed

+62
-40
lines changed

compiler/rustc_mir_dataflow/src/framework/engine.rs

+34-22
Original file line numberDiff line numberDiff line change
@@ -271,29 +271,31 @@ where
271271
);
272272
}
273273

274-
let mut results = Results { analysis, entry_sets, _marker: PhantomData };
274+
let results = Results { analysis, entry_sets, _marker: PhantomData };
275275

276276
if tcx.sess.opts.unstable_opts.dump_mir_dataflow {
277-
let res = write_graphviz_results(tcx, body, &mut results, pass_name);
277+
let (res, results) = write_graphviz_results(tcx, body, results, pass_name);
278278
if let Err(e) = res {
279279
error!("Failed to write graphviz dataflow results: {}", e);
280280
}
281+
results
282+
} else {
283+
results
281284
}
282-
283-
results
284285
}
285286
}
286287

287288
// Graphviz
288289

289290
/// Writes a DOT file containing the results of a dataflow analysis if the user requested it via
290-
/// `rustc_mir` attributes and `-Z dump-mir-dataflow`.
291+
/// `rustc_mir` attributes and `-Z dump-mir-dataflow`. The `Result` in and the `Results` out are
292+
/// the same.
291293
fn write_graphviz_results<'tcx, A>(
292294
tcx: TyCtxt<'tcx>,
293295
body: &mir::Body<'tcx>,
294-
results: &mut Results<'tcx, A>,
296+
results: Results<'tcx, A>,
295297
pass_name: Option<&'static str>,
296-
) -> std::io::Result<()>
298+
) -> (std::io::Result<()>, Results<'tcx, A>)
297299
where
298300
A: Analysis<'tcx>,
299301
A::Domain: DebugWithContext<A>,
@@ -304,23 +306,30 @@ where
304306
let def_id = body.source.def_id();
305307
let Ok(attrs) = RustcMirAttrs::parse(tcx, def_id) else {
306308
// Invalid `rustc_mir` attrs are reported in `RustcMirAttrs::parse`
307-
return Ok(());
309+
return (Ok(()), results);
308310
};
309311

310-
let mut file = match attrs.output_path(A::NAME) {
311-
Some(path) => {
312-
debug!("printing dataflow results for {:?} to {}", def_id, path.display());
313-
if let Some(parent) = path.parent() {
314-
fs::create_dir_all(parent)?;
312+
let file = try {
313+
match attrs.output_path(A::NAME) {
314+
Some(path) => {
315+
debug!("printing dataflow results for {:?} to {}", def_id, path.display());
316+
if let Some(parent) = path.parent() {
317+
fs::create_dir_all(parent)?;
318+
}
319+
let f = fs::File::create(&path)?;
320+
io::BufWriter::new(f)
315321
}
316-
io::BufWriter::new(fs::File::create(&path)?)
317-
}
318322

319-
None if dump_enabled(tcx, A::NAME, def_id) => {
320-
create_dump_file(tcx, ".dot", false, A::NAME, &pass_name.unwrap_or("-----"), body)?
321-
}
323+
None if dump_enabled(tcx, A::NAME, def_id) => {
324+
create_dump_file(tcx, ".dot", false, A::NAME, &pass_name.unwrap_or("-----"), body)?
325+
}
322326

323-
_ => return Ok(()),
327+
_ => return (Ok(()), results),
328+
}
329+
};
330+
let mut file = match file {
331+
Ok(f) => f,
332+
Err(e) => return (Err(e), results),
324333
};
325334

326335
let style = match attrs.formatter {
@@ -336,11 +345,14 @@ where
336345
if tcx.sess.opts.unstable_opts.graphviz_dark_mode {
337346
render_opts.push(dot::RenderOption::DarkTheme);
338347
}
339-
with_no_trimmed_paths!(dot::render_opts(&graphviz, &mut buf, &render_opts)?);
348+
let r = with_no_trimmed_paths!(dot::render_opts(&graphviz, &mut buf, &render_opts));
340349

341-
file.write_all(&buf)?;
350+
let lhs = try {
351+
r?;
352+
file.write_all(&buf)?;
353+
};
342354

343-
Ok(())
355+
(lhs, graphviz.into_results())
344356
}
345357

346358
#[derive(Default)]

compiler/rustc_mir_dataflow/src/framework/graphviz.rs

+27-18
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_middle::mir::graphviz_safe_def_name;
1212
use rustc_middle::mir::{self, BasicBlock, Body, Location};
1313

1414
use super::fmt::{DebugDiffWithAdapter, DebugWithAdapter, DebugWithContext};
15-
use super::{Analysis, CallReturnPlaces, Direction, Results, ResultsRefCursor, ResultsVisitor};
15+
use super::{Analysis, CallReturnPlaces, Direction, Results, ResultsCursor, ResultsVisitor};
1616

1717
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
1818
pub(crate) enum OutputStyle {
@@ -29,27 +29,31 @@ impl OutputStyle {
2929
}
3030
}
3131

32-
pub(crate) struct Formatter<'res, 'mir, 'tcx, A>
32+
pub(crate) struct Formatter<'mir, 'tcx, A>
3333
where
3434
A: Analysis<'tcx>,
3535
{
3636
body: &'mir Body<'tcx>,
37-
results: RefCell<&'res mut Results<'tcx, A>>,
37+
results: RefCell<Option<Results<'tcx, A>>>,
3838
style: OutputStyle,
3939
reachable: BitSet<BasicBlock>,
4040
}
4141

42-
impl<'res, 'mir, 'tcx, A> Formatter<'res, 'mir, 'tcx, A>
42+
impl<'mir, 'tcx, A> Formatter<'mir, 'tcx, A>
4343
where
4444
A: Analysis<'tcx>,
4545
{
4646
pub(crate) fn new(
4747
body: &'mir Body<'tcx>,
48-
results: &'res mut Results<'tcx, A>,
48+
results: Results<'tcx, A>,
4949
style: OutputStyle,
5050
) -> Self {
5151
let reachable = mir::traversal::reachable_as_bitset(body);
52-
Formatter { body, results: results.into(), style, reachable }
52+
Formatter { body, results: Some(results).into(), style, reachable }
53+
}
54+
55+
pub(crate) fn into_results(self) -> Results<'tcx, A> {
56+
self.results.into_inner().unwrap()
5357
}
5458
}
5559

@@ -69,7 +73,7 @@ fn dataflow_successors(body: &Body<'_>, bb: BasicBlock) -> Vec<CfgEdge> {
6973
.collect()
7074
}
7175

72-
impl<'tcx, A> dot::Labeller<'_> for Formatter<'_, '_, 'tcx, A>
76+
impl<'tcx, A> dot::Labeller<'_> for Formatter<'_, 'tcx, A>
7377
where
7478
A: Analysis<'tcx>,
7579
A::Domain: DebugWithContext<A>,
@@ -88,14 +92,19 @@ where
8892

8993
fn node_label(&self, block: &Self::Node) -> dot::LabelText<'_> {
9094
let mut label = Vec::new();
91-
let mut results = self.results.borrow_mut();
92-
let mut fmt = BlockFormatter {
93-
results: results.as_results_cursor(self.body),
94-
style: self.style,
95-
bg: Background::Light,
96-
};
95+
self.results.replace_with(|results| {
96+
// `Formatter::result` is a `RefCell<Option<_>>` so we can replace
97+
// the value with `None`, move it into the results cursor, move it
98+
// back out, and return it to the refcell wrapped in `Some`.
99+
let mut fmt = BlockFormatter {
100+
results: results.take().unwrap().into_results_cursor(self.body),
101+
style: self.style,
102+
bg: Background::Light,
103+
};
97104

98-
fmt.write_node_label(&mut label, *block).unwrap();
105+
fmt.write_node_label(&mut label, *block).unwrap();
106+
Some(fmt.results.into_results())
107+
});
99108
dot::LabelText::html(String::from_utf8(label).unwrap())
100109
}
101110

@@ -109,7 +118,7 @@ where
109118
}
110119
}
111120

112-
impl<'mir, 'tcx, A> dot::GraphWalk<'mir> for Formatter<'_, 'mir, 'tcx, A>
121+
impl<'mir, 'tcx, A> dot::GraphWalk<'mir> for Formatter<'mir, 'tcx, A>
113122
where
114123
A: Analysis<'tcx>,
115124
{
@@ -143,16 +152,16 @@ where
143152
}
144153
}
145154

146-
struct BlockFormatter<'res, 'mir, 'tcx, A>
155+
struct BlockFormatter<'mir, 'tcx, A>
147156
where
148157
A: Analysis<'tcx>,
149158
{
150-
results: ResultsRefCursor<'res, 'mir, 'tcx, A>,
159+
results: ResultsCursor<'mir, 'tcx, A>,
151160
bg: Background,
152161
style: OutputStyle,
153162
}
154163

155-
impl<'res, 'mir, 'tcx, A> BlockFormatter<'res, 'mir, 'tcx, A>
164+
impl<'mir, 'tcx, A> BlockFormatter<'mir, 'tcx, A>
156165
where
157166
A: Analysis<'tcx>,
158167
A::Domain: DebugWithContext<A>,

compiler/rustc_mir_dataflow/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![feature(let_chains)]
55
#![feature(min_specialization)]
66
#![feature(stmt_expr_attributes)]
7+
#![feature(try_blocks)]
78
#![recursion_limit = "256"]
89
#![deny(rustc::untranslatable_diagnostic)]
910
#![deny(rustc::diagnostic_outside_of_impl)]

0 commit comments

Comments
 (0)