Skip to content

Commit 61b6383

Browse files
committed
[move-only] Now that borrow to destructure transform knows how to handle switch_enum, emit move only enum switches at +0.
1 parent a624fec commit 61b6383

File tree

3 files changed

+222
-60
lines changed

3 files changed

+222
-60
lines changed

lib/SILGen/SILGenPattern.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2843,14 +2843,28 @@ void SILGenFunction::emitSwitchStmt(SwitchStmt *S) {
28432843

28442844
// Inline constructor for subject.
28452845
auto subject = ([&]() -> ConsumableManagedValue {
2846-
// If we have a move only value, ensure plus one and convert it. Switches
2847-
// always consume move only values.
2846+
// If we have a noImplicitCopy value, ensure plus one and convert
2847+
// it. Switches always consume move only values.
2848+
//
2849+
// NOTE: We purposely do not do this for pure move only types since for them
2850+
// we emit everything at +0 and then run the BorrowToDestructure transform
2851+
// upon them. The reason that we do this is that internally to
2852+
// SILGenPattern, we always attempt to move from +1 -> +0 meaning that even
2853+
// if we start at +1, we will go back to +0 given enough patterns to go
2854+
// through. It is simpler to just let SILGenPattern do what it already wants
2855+
// to do, rather than fight it or try to resusitate the "fake owned borrow"
2856+
// path that we still use for address only types (and that we want to delete
2857+
// once we have opaque values).
28482858
if (subjectMV.getType().isMoveOnly() && subjectMV.getType().isObject()) {
28492859
if (subjectMV.getType().isMoveOnlyWrapped()) {
28502860
subjectMV = B.createOwnedMoveOnlyWrapperToCopyableValue(
28512861
S, subjectMV.ensurePlusOne(*this, S));
28522862
} else {
2853-
subjectMV = B.createMoveValue(S, subjectMV.ensurePlusOne(*this, S));
2863+
// If we have a pure move only type and it is owned, borrow it so that
2864+
// BorrowToDestructure can handle it.
2865+
if (subjectMV.getOwnershipKind() == OwnershipKind::Owned) {
2866+
subjectMV = subjectMV.borrow(*this, S);
2867+
}
28542868
}
28552869
}
28562870

0 commit comments

Comments
 (0)