@@ -1120,7 +1120,11 @@ namespace {
1120
1120
SGF.B .createInjectEnumAddr (Loc, optionalBuf, someDecl);
1121
1121
1122
1122
auto payload = tupleTemp->getManagedAddress ();
1123
- return ManagedValue (optionalBuf, payload.getCleanup ());
1123
+ if (payload.hasCleanup ()) {
1124
+ payload.forward (SGF);
1125
+ return SGF.emitManagedBufferWithCleanup (optionalBuf);
1126
+ }
1127
+ return ManagedValue::forUnmanaged (optionalBuf);
1124
1128
}
1125
1129
}
1126
1130
@@ -1150,15 +1154,20 @@ namespace {
1150
1154
1151
1155
auto payload = tupleTemp->getManagedAddress ();
1152
1156
if (SGF.silConv .useLoweredAddresses ()) {
1153
- return ManagedValue (existentialBuf, payload.getCleanup ());
1157
+ // We always need to return the existential buf with a cleanup even if
1158
+ // we stored trivial values, since SILGen maintains the invariant that
1159
+ // forwarding a non-trivial value (i.e. an Any) into memory must be done
1160
+ // at +1.
1161
+ payload.forward (SGF);
1162
+ return SGF.emitManagedBufferWithCleanup (existentialBuf);
1154
1163
}
1164
+
1155
1165
// We are under opaque value(s) mode - load the any and init an opaque
1156
- auto loadedPayload = SGF.emitManagedLoadCopy (Loc, payload. getValue () );
1166
+ auto loadedPayload = SGF.B . createLoadCopy (Loc, payload);
1157
1167
auto &anyTL = SGF.getTypeLowering (opaque, outputSubstType);
1158
- SILValue loadedOpaque = SGF.B .createInitExistentialValue (
1159
- Loc, anyTL.getLoweredType (), inputTupleType, loadedPayload. getValue () ,
1168
+ return SGF.B .createInitExistentialValue (
1169
+ Loc, anyTL.getLoweredType (), inputTupleType, loadedPayload,
1160
1170
/* Conformances=*/ {});
1161
- return ManagedValue (loadedOpaque, loadedPayload.getCleanup ());
1162
1171
}
1163
1172
1164
1173
// / Handle a tuple that has been exploded in both the input and
0 commit comments