Skip to content

Commit cc09ab3

Browse files
committed
Simplify verify_candidate_branch.
Let chains are perfect for this kind of function.
1 parent ec6fe4e commit cc09ab3

1 file changed

Lines changed: 23 additions & 37 deletions

File tree

‎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-
returnfalse;
315-
};
316-
// ...assign the discriminant of `place` in that statement
317-
letStatementKind::Assign(boxed) = &statement.kindelse{returnfalse};
318-
let(discr_place,Rvalue::Discriminant(from_place)) = &**boxed else{returnfalse};
319-
if*from_place != place {
320-
returnfalse;
321-
}
322-
// ...make that assignment to a local
323-
if discr_place.projection.len() != 0{
324-
returnfalse;
325-
}
326-
// ...terminate on a `SwitchInt` that invalidates that local
327-
letTerminatorKind::SwitchInt{discr: switch_op, targets, .. } = &branch.terminator().kind
328-
else{
329-
returnfalse;
330-
};
331-
if*switch_op != Operand::Move(*discr_place){
332-
returnfalse;
333-
}
334-
// ...fall through to `destination` if the switch misses
335-
if destination != targets.otherwise(){
336-
returnfalse;
337-
}
338-
// ...have a branch for value `value`
339-
letmut iter = targets.iter();
340-
letSome((target_value, _)) = iter.next()else{
341-
returnfalse;
342-
};
343-
if target_value != value {
344-
returnfalse;
345-
}
346-
// ...and have no more branches
347-
ifletSome(_) = iter.next(){
348-
returnfalse;
313+
iflet[statement] = branch.statements.as_slice()
314+
// ...assign the discriminant of `place` in that statement
315+
&& letStatementKind::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.is_empty()
320+
// ...terminate on a `SwitchInt` that invalidates that local
321+
&& letTerminatorKind::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+
&& letmut iter = targets.iter()
328+
&& letSome((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)