Skip to content

Commit 0e0ffbd

Browse files
committed
Deparameterize Results and ResultsCursor.
They both now only ever contain a `Results<'tcx, A>`. This means `AnalysisResults` can be removed, as can many `borrow`/`borrow_mut` calls. Also `Results` no longer needs a `PhantomData` because `'tcx` is now named by `entry_sets`.
1 parent 2911eca commit 0e0ffbd

File tree

5 files changed

+27
-72
lines changed

5 files changed

+27
-72
lines changed

compiler/rustc_mir_dataflow/src/framework/cursor.rs

+17-54
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,26 @@
22
33
use crate::framework::BitSetExt;
44

5-
use std::borrow::{Borrow, BorrowMut};
65
use std::cmp::Ordering;
76

87
#[cfg(debug_assertions)]
98
use rustc_index::bit_set::BitSet;
109
use rustc_middle::mir::{self, BasicBlock, Location};
1110

12-
use super::{Analysis, Direction, Effect, EffectIndex, EntrySets, Results};
13-
14-
// `AnalysisResults` is needed as an impl such as the following has an unconstrained type
15-
// parameter:
16-
// ```
17-
// impl<'tcx, A, E, R> ResultsCursor<'_, 'tcx, A, R>
18-
// where
19-
// A: Analysis<'tcx>,
20-
// E: Borrow<EntrySets<'tcx, A>>,
21-
// R: Results<'tcx, A, E>,
22-
// {}
23-
// ```
24-
25-
/// A type representing the analysis results consumed by a `ResultsCursor`.
26-
pub trait AnalysisResults<'tcx, A>: BorrowMut<Results<'tcx, A, Self::EntrySets>>
27-
where
28-
A: Analysis<'tcx>,
29-
{
30-
/// The type containing the entry sets for this `Results` type.
31-
///
32-
/// Should be either `EntrySets<'tcx, A>` or `&EntrySets<'tcx, A>`.
33-
type EntrySets: Borrow<EntrySets<'tcx, A>>;
34-
}
35-
impl<'tcx, A, E> AnalysisResults<'tcx, A> for Results<'tcx, A, E>
36-
where
37-
A: Analysis<'tcx>,
38-
E: Borrow<EntrySets<'tcx, A>>,
39-
{
40-
type EntrySets = E;
41-
}
11+
use super::{Analysis, Direction, Effect, EffectIndex, Results};
4212

4313
/// Allows random access inspection of the results of a dataflow analysis.
4414
///
4515
/// This cursor only has linear performance within a basic block when its statements are visited in
4616
/// the same order as the `DIRECTION` of the analysis. In the worst case—when statements are
4717
/// visited in *reverse* order—performance will be quadratic in the number of statements in the
4818
/// block. The order in which basic blocks are inspected has no impact on performance.
49-
pub struct ResultsCursor<'mir, 'tcx, A, R = Results<'tcx, A>>
19+
pub struct ResultsCursor<'mir, 'tcx, A>
5020
where
5121
A: Analysis<'tcx>,
5222
{
5323
body: &'mir mir::Body<'tcx>,
54-
results: R,
24+
results: Results<'tcx, A>,
5525
state: A::Domain,
5626

5727
pos: CursorPosition,
@@ -65,7 +35,7 @@ where
6535
reachable_blocks: BitSet<BasicBlock>,
6636
}
6737

68-
impl<'mir, 'tcx, A, R> ResultsCursor<'mir, 'tcx, A, R>
38+
impl<'mir, 'tcx, A> ResultsCursor<'mir, 'tcx, A>
6939
where
7040
A: Analysis<'tcx>,
7141
{
@@ -80,19 +50,13 @@ where
8050
}
8151

8252
/// Unwraps this cursor, returning the underlying `Results`.
83-
pub fn into_results(self) -> R {
53+
pub fn into_results(self) -> Results<'tcx, A> {
8454
self.results
8555
}
86-
}
8756

88-
impl<'mir, 'tcx, A, R> ResultsCursor<'mir, 'tcx, A, R>
89-
where
90-
A: Analysis<'tcx>,
91-
R: AnalysisResults<'tcx, A>,
92-
{
9357
/// Returns a new cursor that can inspect `results`.
94-
pub fn new(body: &'mir mir::Body<'tcx>, results: R) -> Self {
95-
let bottom_value = results.borrow().analysis.bottom_value(body);
58+
pub fn new(body: &'mir mir::Body<'tcx>, results: Results<'tcx, A>) -> Self {
59+
let bottom_value = results.analysis.bottom_value(body);
9660
ResultsCursor {
9761
body,
9862
results,
@@ -117,23 +81,23 @@ where
11781
}
11882

11983
/// Returns the underlying `Results`.
120-
pub fn results(&self) -> &Results<'tcx, A, R::EntrySets> {
121-
self.results.borrow()
84+
pub fn results(&self) -> &Results<'tcx, A> {
85+
&self.results
12286
}
12387

12488
/// Returns the underlying `Results`.
125-
pub fn mut_results(&mut self) -> &mut Results<'tcx, A, R::EntrySets> {
126-
self.results.borrow_mut()
89+
pub fn mut_results(&mut self) -> &mut Results<'tcx, A> {
90+
&mut self.results
12791
}
12892

12993
/// Returns the `Analysis` used to generate the underlying `Results`.
13094
pub fn analysis(&self) -> &A {
131-
&self.results.borrow().analysis
95+
&self.results.analysis
13296
}
13397

13498
/// Returns the `Analysis` used to generate the underlying `Results`.
13599
pub fn mut_analysis(&mut self) -> &mut A {
136-
&mut self.results.borrow_mut().analysis
100+
&mut self.results.analysis
137101
}
138102

139103
/// Resets the cursor to hold the entry set for the given basic block.
@@ -145,7 +109,7 @@ where
145109
#[cfg(debug_assertions)]
146110
assert!(self.reachable_blocks.contains(block));
147111

148-
self.state.clone_from(self.results.borrow().entry_set_for_block(block));
112+
self.state.clone_from(self.results.entry_set_for_block(block));
149113
self.pos = CursorPosition::block_entry(block);
150114
self.state_needs_reset = false;
151115
}
@@ -234,11 +198,10 @@ where
234198
)
235199
};
236200

237-
let analysis = &mut self.results.borrow_mut().analysis;
238201
let target_effect_index = effect.at_index(target.statement_index);
239202

240203
A::Direction::apply_effects_in_range(
241-
analysis,
204+
&mut self.results.analysis,
242205
&mut self.state,
243206
target.block,
244207
block_data,
@@ -254,12 +217,12 @@ where
254217
/// This can be used, e.g., to apply the call return effect directly to the cursor without
255218
/// creating an extra copy of the dataflow state.
256219
pub fn apply_custom_effect(&mut self, f: impl FnOnce(&mut A, &mut A::Domain)) {
257-
f(&mut self.results.borrow_mut().analysis, &mut self.state);
220+
f(&mut self.results.analysis, &mut self.state);
258221
self.state_needs_reset = true;
259222
}
260223
}
261224

262-
impl<'mir, 'tcx, A, R> ResultsCursor<'mir, 'tcx, A, R>
225+
impl<'mir, 'tcx, A> ResultsCursor<'mir, 'tcx, A>
263226
where
264227
A: crate::GenKillAnalysis<'tcx>,
265228
A::Domain: BitSetExt<A::Idx>,

compiler/rustc_mir_dataflow/src/framework/engine.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@ use crate::errors::{
55
};
66
use crate::framework::BitSetExt;
77

8-
use std::borrow::Borrow;
98
use std::ffi::OsString;
10-
use std::marker::PhantomData;
119
use std::path::PathBuf;
1210

1311
use rustc_ast as ast;
@@ -32,31 +30,29 @@ pub type EntrySets<'tcx, A> = IndexVec<BasicBlock, <A as AnalysisDomain<'tcx>>::
3230

3331
/// A dataflow analysis that has converged to fixpoint.
3432
#[derive(Clone)]
35-
pub struct Results<'tcx, A, E = EntrySets<'tcx, A>>
33+
pub struct Results<'tcx, A>
3634
where
3735
A: Analysis<'tcx>,
3836
{
3937
pub analysis: A,
40-
pub(super) entry_sets: E,
41-
pub(super) _marker: PhantomData<&'tcx ()>,
38+
pub(super) entry_sets: EntrySets<'tcx, A>,
4239
}
4340

44-
impl<'tcx, A, E> Results<'tcx, A, E>
41+
impl<'tcx, A> Results<'tcx, A>
4542
where
4643
A: Analysis<'tcx>,
47-
E: Borrow<EntrySets<'tcx, A>>,
4844
{
4945
/// Creates a `ResultsCursor` that can inspect these `Results`.
5046
pub fn into_results_cursor<'mir>(
5147
self,
5248
body: &'mir mir::Body<'tcx>,
53-
) -> ResultsCursor<'mir, 'tcx, A, Self> {
49+
) -> ResultsCursor<'mir, 'tcx, A> {
5450
ResultsCursor::new(body, self)
5551
}
5652

5753
/// Gets the dataflow state for the given block.
5854
pub fn entry_set_for_block(&self, block: BasicBlock) -> &A::Domain {
59-
&self.entry_sets.borrow()[block]
55+
&self.entry_sets[block]
6056
}
6157

6258
pub fn visit_with<'mir>(
@@ -242,7 +238,7 @@ where
242238
);
243239
}
244240

245-
let results = Results { analysis, entry_sets, _marker: PhantomData };
241+
let results = Results { analysis, entry_sets };
246242

247243
if tcx.sess.opts.unstable_opts.dump_mir_dataflow {
248244
let (res, results) = write_graphviz_results(tcx, body, results, pass_name);

compiler/rustc_mir_dataflow/src/framework/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ mod visitor;
4747

4848
pub use self::cursor::ResultsCursor;
4949
pub use self::direction::{Backward, Direction, Forward};
50-
pub use self::engine::{Engine, EntrySets, Results};
50+
pub use self::engine::{Engine, Results};
5151
pub use self::lattice::{JoinSemiLattice, MaybeReachable};
5252
pub use self::visitor::{visit_results, ResultsVisitable, ResultsVisitor};
5353

compiler/rustc_mir_dataflow/src/framework/tests.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ fn test_cursor<D: Direction>(analysis: MockAnalysis<'_, D>) {
267267
let body = analysis.body;
268268

269269
let mut cursor =
270-
Results { entry_sets: analysis.mock_entry_sets(), analysis, _marker: PhantomData }
271-
.into_results_cursor(body);
270+
Results { entry_sets: analysis.mock_entry_sets(), analysis }.into_results_cursor(body);
272271

273272
cursor.allow_unreachable();
274273

compiler/rustc_mir_dataflow/src/framework/visitor.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use std::borrow::Borrow;
2-
31
use rustc_middle::mir::{self, BasicBlock, Location};
42

5-
use super::{Analysis, Direction, EntrySets, Results};
3+
use super::{Analysis, Direction, Results};
64

75
/// Calls the corresponding method in `ResultsVisitor` for every location in a `mir::Body` with the
86
/// dataflow state at that location.
@@ -143,10 +141,9 @@ pub trait ResultsVisitable<'tcx> {
143141
);
144142
}
145143

146-
impl<'tcx, A, E> ResultsVisitable<'tcx> for Results<'tcx, A, E>
144+
impl<'tcx, A> ResultsVisitable<'tcx> for Results<'tcx, A>
147145
where
148146
A: Analysis<'tcx>,
149-
E: Borrow<EntrySets<'tcx, A>>,
150147
{
151148
type FlowState = A::Domain;
152149

0 commit comments

Comments
 (0)