Skip to content

Commit 83f3d20

Browse files
committed
Remove opt_clone_from_or_clone.
Switches to the idiom used elsewhere of calling `Analysis::bottom_value` to initialize a `state` value outside a loop, and then using `clone_from` to update it within the loop. This is simpler and has no impact on performance.
1 parent c8c50f4 commit 83f3d20

File tree

1 file changed

+13
-30
lines changed

1 file changed

+13
-30
lines changed

compiler/rustc_mir_dataflow/src/framework/direction.rs

+13-30
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,11 @@ impl Direction for Backward {
118118
let targets =
119119
values.iter().map(|&value| SwitchIntTarget { value, target: block });
120120

121-
let mut tmp = None;
121+
let mut tmp = analysis.bottom_value(body);
122122
for target in targets {
123-
let tmp = opt_clone_from_or_clone(&mut tmp, exit_state);
124-
analysis.apply_switch_int_edge_effect(&mut data, tmp, target);
125-
propagate(pred, tmp);
123+
tmp.clone_from(&exit_state);
124+
analysis.apply_switch_int_edge_effect(&mut data, &mut tmp, target);
125+
propagate(pred, &tmp);
126126
}
127127
} else {
128128
propagate(pred, exit_state)
@@ -251,7 +251,7 @@ impl Direction for Forward {
251251

252252
fn apply_effects_in_block<'mir, 'tcx, A>(
253253
analysis: &mut A,
254-
_body: &mir::Body<'tcx>,
254+
body: &mir::Body<'tcx>,
255255
state: &mut A::Domain,
256256
block: BasicBlock,
257257
block_data: &'mir mir::BasicBlockData<'tcx>,
@@ -292,14 +292,15 @@ impl Direction for Forward {
292292
}
293293
TerminatorEdges::SwitchInt { targets, discr } => {
294294
if let Some(mut data) = analysis.get_switch_int_data(block, discr) {
295-
let mut tmp = None;
295+
let mut tmp = analysis.bottom_value(body);
296296
for (value, target) in targets.iter() {
297-
let tmp = opt_clone_from_or_clone(&mut tmp, exit_state);
298-
analysis.apply_switch_int_edge_effect(&mut data, tmp, SwitchIntTarget {
299-
value: Some(value),
300-
target,
301-
});
302-
propagate(target, tmp);
297+
tmp.clone_from(&exit_state);
298+
analysis.apply_switch_int_edge_effect(
299+
&mut data,
300+
&mut tmp,
301+
SwitchIntTarget { value: Some(value), target },
302+
);
303+
propagate(target, &tmp);
303304
}
304305

305306
// Once we get to the final, "otherwise" branch, there is no need to preserve
@@ -425,21 +426,3 @@ impl Direction for Forward {
425426
vis.visit_block_end(state);
426427
}
427428
}
428-
429-
/// An analogue of `Option::get_or_insert_with` that stores a clone of `val` into `opt`, but uses
430-
/// the more efficient `clone_from` if `opt` was `Some`.
431-
///
432-
/// Returns a mutable reference to the new clone that resides in `opt`.
433-
//
434-
// FIXME: Figure out how to express this using `Option::clone_from`, or maybe lift it into the
435-
// standard library?
436-
fn opt_clone_from_or_clone<'a, T: Clone>(opt: &'a mut Option<T>, val: &T) -> &'a mut T {
437-
if opt.is_some() {
438-
let ret = opt.as_mut().unwrap();
439-
ret.clone_from(val);
440-
ret
441-
} else {
442-
*opt = Some(val.clone());
443-
opt.as_mut().unwrap()
444-
}
445-
}

0 commit comments

Comments
 (0)