@@ -1148,14 +1148,15 @@ SILGenFunction::ForceTryEmission::ForceTryEmission(SILGenFunction &SGF,
11481148 // Set up a "catch" block for when an error occurs.
11491149 SILBasicBlock *catchBB = SGF.createBasicBlock (FunctionSection::Postmatter);
11501150
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+ }
11551156
11561157 SGF.ThrowDest = JumpDest (catchBB, SGF.Cleanups .getCleanupsDepth (),
11571158 CleanupLocation (loc),
1158- ThrownErrorInfo ( /* indirectErrorAddr= */ nullptr ));
1159+ ThrownErrorInfo::forDiscard ( ));
11591160}
11601161
11611162void SILGenFunction::ForceTryEmission::finish () {
@@ -1171,26 +1172,34 @@ void SILGenFunction::ForceTryEmission::finish() {
11711172 // Otherwise, we need to emit it.
11721173 SILGenSavedInsertionPoint scope (SGF, catchBB, FunctionSection::Postmatter);
11731174
1174- // FIXME: typed throws
1175- auto error = ManagedValue::forForwardedRValue (SGF, catchBB->getArgument (0 ));
1176-
11771175 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+ }
11931201 }
1202+
11941203 SGF.B .createUnreachable (Loc);
11951204 }
11961205
@@ -1241,19 +1250,22 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
12411250 if (isByAddress)
12421251 optAddr = optInit->getAddressForInPlaceInitialization (SGF, E);
12431252
1244- FullExpr localCleanups (SGF.Cleanups , E);
1245-
12461253 // Set up a "catch" block for when an error occurs.
12471254 SILBasicBlock *catchBB = SGF.createBasicBlock (FunctionSection::Postmatter);
12481255
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);
12521264
12531265 llvm::SaveAndRestore<JumpDest> throwDest{
12541266 SGF.ThrowDest ,
12551267 JumpDest (catchBB, SGF.Cleanups .getCleanupsDepth (), E,
1256- ThrownErrorInfo ( /* indirectErrorAddr= */ nullptr ))};
1268+ ThrownErrorInfo::forDiscard ( ))};
12571269
12581270 SILValue branchArg;
12591271 if (shouldWrapInOptional) {
@@ -1308,11 +1320,14 @@ RValue RValueEmitter::visitOptionalTryExpr(OptionalTryExpr *E, SGFContext C) {
13081320 else
13091321 SGF.B .createBranch (E, contBB, branchArg);
13101322
1311- // If control branched to the failure block, inject .None into the
1323+ // If control branched to the failure block, inject .none into the
13121324 // result type.
13131325 SGF.B .emitBlock (catchBB);
13141326 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 ));
13161331 catchCleanups.pop ();
13171332
13181333 if (isByAddress) {
0 commit comments