@@ -1184,18 +1184,27 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
1184
1184
1185
1185
// Form the optional using address operations if the type is address-only or
1186
1186
// if we already have an address to use.
1187
- bool isByAddress = usingProvidedContext || optTL.isAddressOnly ();
1187
+ bool isByAddress = ((usingProvidedContext || optTL.isAddressOnly ()) &&
1188
+ SGF.silConv .useLoweredAddresses ());
1188
1189
1189
1190
std::unique_ptr<TemporaryInitialization> optTemp;
1190
- if (!usingProvidedContext && isByAddress) {
1191
+ if (!isByAddress) {
1192
+ // If the caller produced a context for us, but we're not going
1193
+ // to use it, make sure we don't.
1194
+ optInit = nullptr ;
1195
+ } else if (!usingProvidedContext) {
1191
1196
// Allocate the temporary for the Optional<T> if we didn't get one from the
1192
- // context.
1197
+ // context. This needs to happen outside of the cleanups scope we're about
1198
+ // to push.
1193
1199
optTemp = SGF.emitTemporary (E, optTL);
1194
1200
optInit = optTemp.get ();
1195
- } else if (!usingProvidedContext) {
1196
- // If the caller produced a context for us, but we can't use it, then don't.
1197
- optInit = nullptr ;
1198
1201
}
1202
+ assert (isByAddress == (optInit != nullptr ));
1203
+
1204
+ // Acquire the address to emit into outside of the cleanups scope.
1205
+ SILValue optAddr;
1206
+ if (isByAddress)
1207
+ optAddr = optInit->getAddressForInPlaceInitialization (SGF, E);
1199
1208
1200
1209
FullExpr localCleanups (SGF.Cleanups , E);
1201
1210
@@ -1208,8 +1217,7 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
1208
1217
SILValue branchArg;
1209
1218
if (shouldWrapInOptional) {
1210
1219
if (isByAddress) {
1211
- assert (optInit);
1212
- SILValue optAddr = optInit->getAddressForInPlaceInitialization (SGF, E);
1220
+ assert (optAddr);
1213
1221
SGF.emitInjectOptionalValueInto (E, E->getSubExpr (), optAddr, optTL);
1214
1222
} else {
1215
1223
ManagedValue subExprValue = SGF.emitRValueAsSingleValue (E->getSubExpr ());
@@ -1219,8 +1227,11 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
1219
1227
}
1220
1228
else {
1221
1229
if (isByAddress) {
1222
- assert (optInit);
1223
- SGF.emitExprInto (E->getSubExpr (), optInit);
1230
+ assert (optAddr);
1231
+ // We've already computed the address where we want the result.
1232
+ KnownAddressInitialization normalInit (optAddr);
1233
+ SGF.emitExprInto (E->getSubExpr (), &normalInit);
1234
+ normalInit.finishInitialization (SGF);
1224
1235
} else {
1225
1236
ManagedValue subExprValue = SGF.emitRValueAsSingleValue (E->getSubExpr ());
1226
1237
branchArg = subExprValue.forward (SGF);
@@ -1238,10 +1249,8 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
1238
1249
if (!isByAddress)
1239
1250
return RValue (SGF, E,
1240
1251
SGF.emitManagedRValueWithCleanup (branchArg, optTL));
1241
-
1242
- if (shouldWrapInOptional) {
1243
- optInit->finishInitialization (SGF);
1244
- }
1252
+
1253
+ optInit->finishInitialization (SGF);
1245
1254
1246
1255
// If we emitted into the provided context, we're done.
1247
1256
if (usingProvidedContext)
@@ -1268,8 +1277,7 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
1268
1277
catchCleanups.pop ();
1269
1278
1270
1279
if (isByAddress) {
1271
- SGF.emitInjectOptionalNothingInto (E,
1272
- optInit->getAddressForInPlaceInitialization (SGF, E), optTL);
1280
+ SGF.emitInjectOptionalNothingInto (E, optAddr, optTL);
1273
1281
SGF.B .createBranch (E, contBB);
1274
1282
} else {
1275
1283
auto branchArg = SGF.getOptionalNoneValue (E, optTL);
@@ -1287,9 +1295,7 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
1287
1295
return RValue (SGF, E, SGF.emitManagedRValueWithCleanup (arg, optTL));
1288
1296
}
1289
1297
1290
- if (shouldWrapInOptional) {
1291
- optInit->finishInitialization (SGF);
1292
- }
1298
+ optInit->finishInitialization (SGF);
1293
1299
1294
1300
// If we emitted into the provided context, we're done.
1295
1301
if (usingProvidedContext)
0 commit comments