Skip to content

Commit 796c466

Browse files
authored
Rollup merge of rust-lang#97832 - tmiasko:const-direction, r=cjgillot
Change `Direction::{is_forward,is_backward}` functions into constants Make it explicit that the analysis direction is constant. This also makes the value immediately available for optimizations. Previously those functions were neither inline nor generic and so their definition was unavailable when using data flow framework from other crates.
2 parents c9cf8ae + 39de03d commit 796c466

File tree

5 files changed

+16
-22
lines changed

5 files changed

+16
-22
lines changed

compiler/rustc_mir_dataflow/src/framework/cursor.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ where
109109
/// For backward analyses, this is the state that will be propagated to its
110110
/// predecessors (ignoring edge-specific effects).
111111
pub fn seek_to_block_start(&mut self, block: BasicBlock) {
112-
if A::Direction::is_forward() {
112+
if A::Direction::IS_FORWARD {
113113
self.seek_to_block_entry(block)
114114
} else {
115115
self.seek_after(Location { block, statement_index: 0 }, Effect::Primary)
@@ -123,7 +123,7 @@ where
123123
/// For forward analyses, this is the state that will be propagated to its
124124
/// successors (ignoring edge-specific effects).
125125
pub fn seek_to_block_end(&mut self, block: BasicBlock) {
126-
if A::Direction::is_backward() {
126+
if A::Direction::IS_BACKWARD {
127127
self.seek_to_block_entry(block)
128128
} else {
129129
self.seek_after(self.body.terminator_loc(block), Effect::Primary)
@@ -157,7 +157,7 @@ where
157157
self.seek_to_block_entry(target.block);
158158
} else if let Some(curr_effect) = self.pos.curr_effect_index {
159159
let mut ord = curr_effect.statement_index.cmp(&target.statement_index);
160-
if A::Direction::is_backward() {
160+
if A::Direction::IS_BACKWARD {
161161
ord = ord.reverse()
162162
}
163163

@@ -173,7 +173,7 @@ where
173173
debug_assert_eq!(target.block, self.pos.block);
174174

175175
let block_data = &self.body[target.block];
176-
let next_effect = if A::Direction::is_forward() {
176+
let next_effect = if A::Direction::IS_FORWARD {
177177
#[rustfmt::skip]
178178
self.pos.curr_effect_index.map_or_else(
179179
|| Effect::Before.at_index(0),

compiler/rustc_mir_dataflow/src/framework/direction.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ use super::{
99
};
1010

1111
pub trait Direction {
12-
fn is_forward() -> bool;
12+
const IS_FORWARD: bool;
1313

14-
fn is_backward() -> bool {
15-
!Self::is_forward()
16-
}
14+
const IS_BACKWARD: bool = !Self::IS_FORWARD;
1715

1816
/// Applies all effects between the given `EffectIndex`s.
1917
///
@@ -68,9 +66,7 @@ pub trait Direction {
6866
pub struct Backward;
6967

7068
impl Direction for Backward {
71-
fn is_forward() -> bool {
72-
false
73-
}
69+
const IS_FORWARD: bool = false;
7470

7571
fn apply_effects_in_block<'tcx, A>(
7672
analysis: &A,
@@ -338,9 +334,7 @@ where
338334
pub struct Forward;
339335

340336
impl Direction for Forward {
341-
fn is_forward() -> bool {
342-
true
343-
}
337+
const IS_FORWARD: bool = true;
344338

345339
fn apply_effects_in_block<'tcx, A>(
346340
analysis: &A,

compiler/rustc_mir_dataflow/src/framework/engine.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ where
147147
let mut entry_sets = IndexVec::from_elem(bottom_value.clone(), body.basic_blocks());
148148
analysis.initialize_start_block(body, &mut entry_sets[mir::START_BLOCK]);
149149

150-
if A::Direction::is_backward() && entry_sets[mir::START_BLOCK] != bottom_value {
150+
if A::Direction::IS_BACKWARD && entry_sets[mir::START_BLOCK] != bottom_value {
151151
bug!("`initialize_start_block` is not yet supported for backward dataflow analyses");
152152
}
153153

@@ -200,7 +200,7 @@ where
200200
let mut dirty_queue: WorkQueue<BasicBlock> =
201201
WorkQueue::with_none(body.basic_blocks().len());
202202

203-
if A::Direction::is_forward() {
203+
if A::Direction::IS_FORWARD {
204204
for (bb, _) in traversal::reverse_postorder(body) {
205205
dirty_queue.insert(bb);
206206
}

compiler/rustc_mir_dataflow/src/framework/graphviz.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ where
216216
// Write the full dataflow state immediately after the terminator if it differs from the
217217
// state at block entry.
218218
self.results.seek_to_block_end(block);
219-
if self.results.get() != &block_start_state || A::Direction::is_backward() {
219+
if self.results.get() != &block_start_state || A::Direction::IS_BACKWARD {
220220
let after_terminator_name = match terminator.kind {
221221
mir::TerminatorKind::Call { target: Some(_), .. } => "(on unwind)",
222222
_ => "(on end)",
@@ -390,7 +390,7 @@ where
390390
let mut afters = diffs.after.into_iter();
391391

392392
let next_in_dataflow_order = |it: &mut std::vec::IntoIter<_>| {
393-
if A::Direction::is_forward() { it.next().unwrap() } else { it.next_back().unwrap() }
393+
if A::Direction::IS_FORWARD { it.next().unwrap() } else { it.next_back().unwrap() }
394394
};
395395

396396
for (i, statement) in body[block].statements.iter().enumerate() {
@@ -527,7 +527,7 @@ where
527527
_block_data: &mir::BasicBlockData<'tcx>,
528528
_block: BasicBlock,
529529
) {
530-
if A::Direction::is_forward() {
530+
if A::Direction::IS_FORWARD {
531531
self.prev_state.clone_from(state);
532532
}
533533
}
@@ -538,7 +538,7 @@ where
538538
_block_data: &mir::BasicBlockData<'tcx>,
539539
_block: BasicBlock,
540540
) {
541-
if A::Direction::is_backward() {
541+
if A::Direction::IS_BACKWARD {
542542
self.prev_state.clone_from(state);
543543
}
544544
}

compiler/rustc_mir_dataflow/src/framework/tests.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<D: Direction> MockAnalysis<'_, D> {
140140
SeekTarget::After(loc) => Effect::Primary.at_index(loc.statement_index),
141141
};
142142

143-
let mut pos = if D::is_forward() {
143+
let mut pos = if D::IS_FORWARD {
144144
Effect::Before.at_index(0)
145145
} else {
146146
Effect::Before.at_index(self.body[block].statements.len())
@@ -153,7 +153,7 @@ impl<D: Direction> MockAnalysis<'_, D> {
153153
return ret;
154154
}
155155

156-
if D::is_forward() {
156+
if D::IS_FORWARD {
157157
pos = pos.next_in_forward_order();
158158
} else {
159159
pos = pos.next_in_backward_order();

0 commit comments

Comments
 (0)