Skip to content

Commit 5837955

Browse files
committed
update MaybeUninitializedPlaces and MaybeInitializedPlaces for switchInt otherwise target
1 parent 868bf2d commit 5837955

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

compiler/rustc_mir_dataflow/src/drop_flag_effects.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,16 @@ where
156156
}
157157

158158
/// Calls `handle_inactive_variant` for each descendant move path of `enum_place` that contains a
159-
/// `Downcast` to a variant besides the `active_variant`.
159+
/// `Downcast` to a variant besides the `active_variant`, otherwise calls `handle_active_variant`.
160160
///
161-
/// NOTE: If there are no move paths corresponding to an inactive variant,
162-
/// `handle_inactive_variant` will not be called for that variant.
161+
/// NOTE: If there are no move paths corresponding to an inactive variant, `handle_active_variant`
162+
/// or `handle_inactive_variant` will not be called for that variant.
163163
pub(crate) fn on_all_inactive_variants<'tcx>(
164164
move_data: &MoveData<'tcx>,
165165
enum_place: mir::Place<'tcx>,
166166
active_variant: VariantIdx,
167167
mut handle_inactive_variant: impl FnMut(MovePathIndex),
168+
mut handle_active_variant: impl FnMut(MovePathIndex),
168169
) {
169170
let LookupResult::Exact(enum_mpi) = move_data.rev_lookup.find(enum_place.as_ref()) else {
170171
return;
@@ -184,6 +185,8 @@ pub(crate) fn on_all_inactive_variants<'tcx>(
184185

185186
if variant_idx != active_variant {
186187
on_all_children_bits(move_data, variant_mpi, |mpi| handle_inactive_variant(mpi));
188+
} else {
189+
on_all_children_bits(move_data, variant_mpi, |mpi| handle_active_variant(mpi));
187190
}
188191
}
189192
}

compiler/rustc_mir_dataflow/src/framework/direction.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl Direction for Backward {
117117
let mut tmp = analysis.bottom_value(body);
118118
for &value in &body.basic_blocks.switch_sources()[&(block, pred)] {
119119
tmp.clone_from(exit_state);
120-
analysis.apply_switch_int_edge_effect(&mut data, &mut tmp, value);
120+
analysis.apply_switch_int_edge_effect(&mut data, &mut tmp, value, None);
121121
propagate(pred, &tmp);
122122
}
123123
} else {
@@ -287,23 +287,21 @@ impl Direction for Forward {
287287
TerminatorEdges::SwitchInt { targets, discr } => {
288288
if let Some(mut data) = analysis.get_switch_int_data(block, discr) {
289289
let mut tmp = analysis.bottom_value(body);
290+
let mut otherwise_state = exit_state.clone();
291+
290292
for (value, target) in targets.iter() {
291293
tmp.clone_from(exit_state);
292294
let value = SwitchTargetValue::Normal(value);
293-
analysis.apply_switch_int_edge_effect(&mut data, &mut tmp, value);
295+
analysis.apply_switch_int_edge_effect(
296+
&mut data,
297+
&mut tmp,
298+
value,
299+
Some(&mut otherwise_state),
300+
);
294301
propagate(target, &tmp);
295302
}
296303

297-
// Once we get to the final, "otherwise" branch, there is no need to preserve
298-
// `exit_state`, so pass it directly to `apply_switch_int_edge_effect` to save
299-
// a clone of the dataflow state.
300-
let otherwise = targets.otherwise();
301-
analysis.apply_switch_int_edge_effect(
302-
&mut data,
303-
exit_state,
304-
SwitchTargetValue::Otherwise,
305-
);
306-
propagate(otherwise, exit_state);
304+
propagate(targets.otherwise(), &otherwise_state);
307305
} else {
308306
for target in targets.all_targets() {
309307
propagate(*target, exit_state);

compiler/rustc_mir_dataflow/src/framework/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ pub trait Analysis<'tcx> {
224224
_data: &mut Self::SwitchIntData,
225225
_state: &mut Self::Domain,
226226
_value: SwitchTargetValue,
227+
_otherwise_state: Option<&mut Self::Domain>,
227228
) {
228229
unreachable!();
229230
}

compiler/rustc_mir_dataflow/src/impls/initialized.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,7 @@ impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
431431
data: &mut Self::SwitchIntData,
432432
state: &mut Self::Domain,
433433
value: SwitchTargetValue,
434+
mut otherwise_state: Option<&mut Self::Domain>,
434435
) {
435436
if let SwitchTargetValue::Normal(value) = value {
436437
// Kill all move paths that correspond to variants we know to be inactive along this
@@ -440,6 +441,9 @@ impl<'tcx> Analysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
440441
data.enum_place,
441442
data.next_discr(value),
442443
|mpi| state.kill(mpi),
444+
move |mpi| {
445+
otherwise_state.as_mut().map(|state| state.kill(mpi));
446+
},
443447
);
444448
}
445449
}
@@ -544,6 +548,7 @@ impl<'tcx> Analysis<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
544548
data: &mut Self::SwitchIntData,
545549
state: &mut Self::Domain,
546550
value: SwitchTargetValue,
551+
mut otherwise_state: Option<&mut Self::Domain>,
547552
) {
548553
if let SwitchTargetValue::Normal(value) = value {
549554
// Mark all move paths that correspond to variants other than this one as maybe
@@ -553,6 +558,9 @@ impl<'tcx> Analysis<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
553558
data.enum_place,
554559
data.next_discr(value),
555560
|mpi| state.gen_(mpi),
561+
|mpi| {
562+
otherwise_state.as_mut().map(|state| state.gen_(mpi));
563+
},
556564
);
557565
}
558566
}

0 commit comments

Comments
 (0)