Skip to content

Commit bd56b11

Browse files
committed
Use SwitchTargets instead of storing in Vec
1 parent b4e144e commit bd56b11

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

compiler/rustc_mir/src/transform/early_otherwise_branch.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ impl<'tcx> MirPass<'tcx> for EarlyOtherwiseBranch {
9999
.info
100100
.second_switch_infos
101101
.iter()
102-
.flat_map(|x| x.targets_with_values.iter())
103-
.cloned();
102+
.flat_map(|x| x.switch_targets.iter().map(|x| (x.0, x.1)));
104103

105104
let targets = SwitchTargets::new(new_targets, first_switch_info.otherwise_bb);
106105

@@ -174,8 +173,8 @@ struct SwitchDiscriminantInfo<'tcx> {
174173
discr_ty: Ty<'tcx>,
175174
/// The basic block that the otherwise branch points to
176175
otherwise_bb: BasicBlock,
177-
/// Target along with the value being branched from. Otherwise is not included
178-
targets_with_values: Vec<(u128, BasicBlock)>,
176+
/// Targets and values for the switch
177+
switch_targets: SwitchTargets,
179178
discr_source_info: SourceInfo,
180179
/// The place of the discriminant used in the switch
181180
discr_used_in_switch: Place<'tcx>,
@@ -212,8 +211,8 @@ impl<'a, 'tcx> Helper<'a, 'tcx> {
212211
// go through each target, finding a discriminant read, and a switch
213212
let mut second_switch_infos = vec![];
214213

215-
for (value, target) in discr.targets_with_values.iter() {
216-
let info = self.find_discriminant_switch_pairing(&discr, *target, *value)?;
214+
for (value, target) in discr.switch_targets.iter() {
215+
let info = self.find_discriminant_switch_pairing(&discr, target, value)?;
217216
second_switch_infos.push(info);
218217
}
219218

@@ -249,8 +248,8 @@ impl<'a, 'tcx> Helper<'a, 'tcx> {
249248
// }
250249
// ```
251250
// We check this by seeing that the value of the first discriminant is the only other discriminant value being used as a target in the second switch
252-
if !(this_bb_discr_info.targets_with_values.len() == 1
253-
&& this_bb_discr_info.targets_with_values[0].0 == value)
251+
if !(this_bb_discr_info.switch_targets.iter().len() == 1
252+
&& this_bb_discr_info.switch_targets.iter().next()?.0 == value)
254253
{
255254
trace!(
256255
"NO: The second switch did not have only 1 target (besides otherwise) that had the same value as the value from the first switch that got us here"
@@ -289,7 +288,7 @@ impl<'a, 'tcx> Helper<'a, 'tcx> {
289288
let discr_decl = &self.body.local_decls()[discr_local];
290289
let discr_ty = discr_decl.ty;
291290
let otherwise_bb = targets.otherwise();
292-
let targets_with_values = targets.iter().collect();
291+
let targets_with_values = targets.clone();
293292

294293
// find the place of the adt where the discriminant is being read from
295294
// assume this is the last statement of the block
@@ -306,7 +305,7 @@ impl<'a, 'tcx> Helper<'a, 'tcx> {
306305
discr_used_in_switch: discr.place()?,
307306
discr_ty,
308307
otherwise_bb,
309-
targets_with_values,
308+
switch_targets: targets_with_values,
310309
discr_source_info: discr_decl.source_info,
311310
place_of_adt_discr_read,
312311
type_adt_matched_on,

0 commit comments

Comments
 (0)