@@ -1148,14 +1148,15 @@ SILGenFunction::ForceTryEmission::ForceTryEmission(SILGenFunction &SGF,
1148
1148
// Set up a "catch" block for when an error occurs.
1149
1149
SILBasicBlock *catchBB = SGF.createBasicBlock (FunctionSection::Postmatter);
1150
1150
1151
- // FIXME: typed throws
1152
- ASTContext &ctx = SGF.getASTContext ();
1153
- (void ) catchBB->createPhiArgument (SILType::getExceptionType (ctx),
1154
- OwnershipKind::Owned);
1151
+ auto &errorTL = SGF.getTypeLowering (loc->getThrownError ());
1152
+ if (!errorTL.isAddressOnly ()) {
1153
+ (void ) catchBB->createPhiArgument (errorTL.getLoweredType (),
1154
+ OwnershipKind::Owned);
1155
+ }
1155
1156
1156
1157
SGF.ThrowDest = JumpDest (catchBB, SGF.Cleanups .getCleanupsDepth (),
1157
1158
CleanupLocation (loc),
1158
- ThrownErrorInfo ( /* indirectErrorAddr= */ nullptr ));
1159
+ ThrownErrorInfo::forDiscard ( ));
1159
1160
}
1160
1161
1161
1162
void SILGenFunction::ForceTryEmission::finish () {
@@ -1171,26 +1172,34 @@ void SILGenFunction::ForceTryEmission::finish() {
1171
1172
// Otherwise, we need to emit it.
1172
1173
SILGenSavedInsertionPoint scope (SGF, catchBB, FunctionSection::Postmatter);
1173
1174
1174
- // FIXME: typed throws
1175
- auto error = ManagedValue::forForwardedRValue (SGF, catchBB->getArgument (0 ));
1176
-
1177
1175
ASTContext &ctx = SGF.getASTContext ();
1178
- if (auto diagnoseError = ctx.getDiagnoseUnexpectedError ()) {
1179
- auto args = SGF.emitSourceLocationArgs (Loc->getExclaimLoc (), Loc);
1180
-
1181
- SGF.emitApplyOfLibraryIntrinsic (
1182
- Loc,
1183
- diagnoseError,
1184
- SubstitutionMap (),
1185
- {
1186
- error,
1187
- args.filenameStartPointer ,
1188
- args.filenameLength ,
1189
- args.filenameIsAscii ,
1190
- args.line
1191
- },
1192
- SGFContext ());
1176
+
1177
+ // Consume the thrown error.
1178
+ if (catchBB->getNumArguments () == 1 ) {
1179
+ auto error = ManagedValue::forForwardedRValue (SGF, catchBB->getArgument (0 ));
1180
+
1181
+ // FIXME: for typed throws, we need a new version of this entry point that
1182
+ // takes a generic rather than an existential.
1183
+ if (error.getType () == SILType::getExceptionType (ctx)) {
1184
+ if (auto diagnoseError = ctx.getDiagnoseUnexpectedError ()) {
1185
+ auto args = SGF.emitSourceLocationArgs (Loc->getExclaimLoc (), Loc);
1186
+
1187
+ SGF.emitApplyOfLibraryIntrinsic (
1188
+ Loc,
1189
+ diagnoseError,
1190
+ SubstitutionMap (),
1191
+ {
1192
+ error,
1193
+ args.filenameStartPointer ,
1194
+ args.filenameLength ,
1195
+ args.filenameIsAscii ,
1196
+ args.line
1197
+ },
1198
+ SGFContext ());
1199
+ }
1200
+ }
1193
1201
}
1202
+
1194
1203
SGF.B .createUnreachable (Loc);
1195
1204
}
1196
1205
@@ -1241,19 +1250,22 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
1241
1250
if (isByAddress)
1242
1251
optAddr = optInit->getAddressForInPlaceInitialization (SGF, E);
1243
1252
1244
- FullExpr localCleanups (SGF.Cleanups , E);
1245
-
1246
1253
// Set up a "catch" block for when an error occurs.
1247
1254
SILBasicBlock *catchBB = SGF.createBasicBlock (FunctionSection::Postmatter);
1248
1255
1249
- // FIXME: typed throws
1250
- auto *errorArg = catchBB->createPhiArgument (
1251
- SILType::getExceptionType (SGF.getASTContext ()), OwnershipKind::Owned);
1256
+ // FIXME: opaque values
1257
+ auto &errorTL = SGF.getTypeLowering (E->getThrownError ());
1258
+ if (!errorTL.isAddressOnly ()) {
1259
+ (void ) catchBB->createPhiArgument (errorTL.getLoweredType (),
1260
+ OwnershipKind::Owned);
1261
+ }
1262
+
1263
+ FullExpr localCleanups (SGF.Cleanups , E);
1252
1264
1253
1265
llvm::SaveAndRestore<JumpDest> throwDest{
1254
1266
SGF.ThrowDest ,
1255
1267
JumpDest (catchBB, SGF.Cleanups .getCleanupsDepth (), E,
1256
- ThrownErrorInfo ( /* indirectErrorAddr= */ nullptr ))};
1268
+ ThrownErrorInfo::forDiscard ( ))};
1257
1269
1258
1270
SILValue branchArg;
1259
1271
if (shouldWrapInOptional) {
@@ -1308,11 +1320,14 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
1308
1320
else
1309
1321
SGF.B .createBranch (E, contBB, branchArg);
1310
1322
1311
- // If control branched to the failure block, inject .None into the
1323
+ // If control branched to the failure block, inject .none into the
1312
1324
// result type.
1313
1325
SGF.B .emitBlock (catchBB);
1314
1326
FullExpr catchCleanups (SGF.Cleanups , E);
1315
- (void ) SGF.emitManagedRValueWithCleanup (errorArg);
1327
+
1328
+ // Consume the thrown error.
1329
+ if (!errorTL.isAddressOnly ())
1330
+ (void ) SGF.emitManagedRValueWithCleanup (catchBB->getArgument (0 ));
1316
1331
catchCleanups.pop ();
1317
1332
1318
1333
if (isByAddress) {
0 commit comments