@@ -1210,18 +1210,27 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
1210
1210
1211
1211
// Form the optional using address operations if the type is address-only or
1212
1212
// if we already have an address to use.
1213
- bool isByAddress = usingProvidedContext || optTL.isAddressOnly ();
1213
+ bool isByAddress = ((usingProvidedContext || optTL.isAddressOnly ()) &&
1214
+ SGF.silConv .useLoweredAddresses ());
1214
1215
1215
1216
std::unique_ptr<TemporaryInitialization> optTemp;
1216
- if (!usingProvidedContext && isByAddress) {
1217
+ if (!isByAddress) {
1218
+ // If the caller produced a context for us, but we're not going
1219
+ // to use it, make sure we don't.
1220
+ optInit = nullptr ;
1221
+ } else if (!usingProvidedContext) {
1217
1222
// Allocate the temporary for the Optional<T> if we didn't get one from the
1218
- // context.
1223
+ // context. This needs to happen outside of the cleanups scope we're about
1224
+ // to push.
1219
1225
optTemp = SGF.emitTemporary (E, optTL);
1220
1226
optInit = optTemp.get ();
1221
- } else if (!usingProvidedContext) {
1222
- // If the caller produced a context for us, but we can't use it, then don't.
1223
- optInit = nullptr ;
1224
1227
}
1228
+ assert (isByAddress == (optInit != nullptr ));
1229
+
1230
+ // Acquire the address to emit into outside of the cleanups scope.
1231
+ SILValue optAddr;
1232
+ if (isByAddress)
1233
+ optAddr = optInit->getAddressForInPlaceInitialization (SGF, E);
1225
1234
1226
1235
FullExpr localCleanups (SGF.Cleanups , E);
1227
1236
@@ -1234,8 +1243,7 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
1234
1243
SILValue branchArg;
1235
1244
if (shouldWrapInOptional) {
1236
1245
if (isByAddress) {
1237
- assert (optInit);
1238
- SILValue optAddr = optInit->getAddressForInPlaceInitialization (SGF, E);
1246
+ assert (optAddr);
1239
1247
SGF.emitInjectOptionalValueInto (E, E->getSubExpr (), optAddr, optTL);
1240
1248
} else {
1241
1249
ManagedValue subExprValue = SGF.emitRValueAsSingleValue (E->getSubExpr ());
@@ -1245,8 +1253,11 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
1245
1253
}
1246
1254
else {
1247
1255
if (isByAddress) {
1248
- assert (optInit);
1249
- SGF.emitExprInto (E->getSubExpr (), optInit);
1256
+ assert (optAddr);
1257
+ // We've already computed the address where we want the result.
1258
+ KnownAddressInitialization normalInit (optAddr);
1259
+ SGF.emitExprInto (E->getSubExpr (), &normalInit);
1260
+ normalInit.finishInitialization (SGF);
1250
1261
} else {
1251
1262
ManagedValue subExprValue = SGF.emitRValueAsSingleValue (E->getSubExpr ());
1252
1263
branchArg = subExprValue.forward (SGF);
@@ -1264,10 +1275,8 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
1264
1275
if (!isByAddress)
1265
1276
return RValue (SGF, E,
1266
1277
SGF.emitManagedRValueWithCleanup (branchArg, optTL));
1267
-
1268
- if (shouldWrapInOptional) {
1269
- optInit->finishInitialization (SGF);
1270
- }
1278
+
1279
+ optInit->finishInitialization (SGF);
1271
1280
1272
1281
// If we emitted into the provided context, we're done.
1273
1282
if (usingProvidedContext)
@@ -1294,8 +1303,7 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
1294
1303
catchCleanups.pop ();
1295
1304
1296
1305
if (isByAddress) {
1297
- SGF.emitInjectOptionalNothingInto (E,
1298
- optInit->getAddressForInPlaceInitialization (SGF, E), optTL);
1306
+ SGF.emitInjectOptionalNothingInto (E, optAddr, optTL);
1299
1307
SGF.B .createBranch (E, contBB);
1300
1308
} else {
1301
1309
auto branchArg = SGF.getOptionalNoneValue (E, optTL);
@@ -1313,9 +1321,7 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
1313
1321
return RValue (SGF, E, SGF.emitManagedRValueWithCleanup (arg, optTL));
1314
1322
}
1315
1323
1316
- if (shouldWrapInOptional) {
1317
- optInit->finishInitialization (SGF);
1318
- }
1324
+ optInit->finishInitialization (SGF);
1319
1325
1320
1326
// If we emitted into the provided context, we're done.
1321
1327
if (usingProvidedContext)
0 commit comments