1
Fork 0

Change SwitchTarget representation

The new structure encodes its invariant, which reduces the likelihood
of having an inconsistent representation. It is also more intuitive and
user friendly.

I encapsulated the structure for now in case we decide to change it back.
This commit is contained in:
Celina G. Val 2023-11-29 12:49:32 -08:00
parent 07921b50ba
commit 3e0b2fac5d
4 changed files with 72 additions and 44 deletions

View file

@ -579,13 +579,12 @@ impl<'tcx> Stable<'tcx> for mir::TerminatorKind<'tcx> {
mir::TerminatorKind::SwitchInt { discr, targets } => TerminatorKind::SwitchInt {
discr: discr.stable(tables),
targets: {
let (value_vec, mut target_vec): (Vec<_>, Vec<_>) =
targets.iter().map(|(value, target)| (value, target.as_usize())).unzip();
// We need to push otherwise as last element to ensure it's same as in MIR.
target_vec.push(targets.otherwise().as_usize());
stable_mir::mir::SwitchTargets { value: value_vec, targets: target_vec }
let branches = targets.iter().map(|(val, target)| (val, target.as_usize()));
stable_mir::mir::SwitchTargets::new(
branches.collect(),
targets.otherwise().as_usize(),
)
},
otherwise: targets.otherwise().as_usize(),
},
mir::TerminatorKind::UnwindResume => TerminatorKind::Resume,
mir::TerminatorKind::UnwindTerminate(_) => TerminatorKind::Abort,