Skip to content

Commit 0ac6d8a

Browse files
committed
Simplify verify_candidate_branch.
Let chains are perfect for this kind of function.
1 parent 9ca823d commit 0ac6d8a

File tree

1 file changed

+23
-37
lines changed

1 file changed

+23
-37
lines changed

compiler/rustc_mir_transform/src/early_otherwise_branch.rs

Lines changed: 23 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -310,42 +310,28 @@ fn verify_candidate_branch<'tcx>(
310310
) -> bool {
311311
// In order for the optimization to be correct, the branch must...
312312
// ...have exactly one statement
313-
let [statement] = branch.statements.as_slice() else {
314-
return false;
315-
};
316-
// ...assign the discriminant of `place` in that statement
317-
let StatementKind::Assign(boxed) = &statement.kind else { return false };
318-
let (discr_place, Rvalue::Discriminant(from_place)) = &**boxed else { return false };
319-
if *from_place != place {
320-
return false;
321-
}
322-
// ...make that assignment to a local
323-
if discr_place.projection.len() != 0 {
324-
return false;
325-
}
326-
// ...terminate on a `SwitchInt` that invalidates that local
327-
let TerminatorKind::SwitchInt { discr: switch_op, targets, .. } = &branch.terminator().kind
328-
else {
329-
return false;
330-
};
331-
if *switch_op != Operand::Move(*discr_place) {
332-
return false;
333-
}
334-
// ...fall through to `destination` if the switch misses
335-
if destination != targets.otherwise() {
336-
return false;
337-
}
338-
// ...have a branch for value `value`
339-
let mut iter = targets.iter();
340-
let Some((target_value, _)) = iter.next() else {
341-
return false;
342-
};
343-
if target_value != value {
344-
return false;
345-
}
346-
// ...and have no more branches
347-
if let Some(_) = iter.next() {
348-
return false;
313+
if let [statement] = branch.statements.as_slice()
314+
// ...assign the discriminant of `place` in that statement
315+
&& let StatementKind::Assign(boxed) = &statement.kind
316+
&& let (discr_place, Rvalue::Discriminant(from_place)) = &**boxed
317+
&& *from_place == place
318+
// ...make that assignment to a local
319+
&& discr_place.projection.len() == 0
320+
// ...terminate on a `SwitchInt` that invalidates that local
321+
&& let TerminatorKind::SwitchInt { discr: switch_op, targets, .. } =
322+
&branch.terminator().kind
323+
&& *switch_op == Operand::Move(*discr_place)
324+
// ...fall through to `destination` if the switch misses
325+
&& destination == targets.otherwise()
326+
// ...have a branch for value `value`
327+
&& let mut iter = targets.iter()
328+
&& let Some((target_value, _)) = iter.next()
329+
&& target_value == value
330+
// ...and have no more branches
331+
&& iter.next().is_none()
332+
{
333+
true
334+
} else {
335+
false
349336
}
350-
true
351337
}

0 commit comments

Comments
 (0)