@@ -149,6 +149,13 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
149149 if (SubstCalleeTy->hasArchetype () || ConvertCalleeTy->hasArchetype ())
150150 return nullptr ;
151151
152+ // If we converted from a non-throwing to a throwing function which is
153+ // try_apply'd, rewriting would require changing the CFG. Bail for now.
154+ if (!ConvertCalleeTy->hasErrorResult () && isa<TryApplyInst>(AI)) {
155+ assert (SubstCalleeTy->hasErrorResult ());
156+ return nullptr ;
157+ }
158+
152159 // Ok, we can now perform our transformation. Grab AI's operands and the
153160 // relevant types from the ConvertFunction function type and AI.
154161 Builder.setCurrentDebugScope (AI.getDebugScope ());
@@ -240,7 +247,8 @@ SILCombiner::optimizeApplyOfConvertFunctionInst(FullApplySite AI,
240247
241248 Builder.createBranch (AI.getLoc (), TAI->getNormalBB (), branchArgs);
242249 }
243-
250+
251+ Builder.setInsertionPoint (AI.getInstruction ());
244252 return Builder.createTryApply (AI.getLoc (), funcOper, SubstitutionMap (), Args,
245253 normalBB, TAI->getErrorBB (),
246254 TAI->getApplyOptions ());
@@ -1623,6 +1631,10 @@ SILInstruction *SILCombiner::visitTryApplyInst(TryApplyInst *AI) {
16231631 if (isa<PartialApplyInst>(AI->getCallee ()))
16241632 return nullptr ;
16251633
1634+ if (auto *CFI = dyn_cast<ConvertFunctionInst>(AI->getCallee ())) {
1635+ return optimizeApplyOfConvertFunctionInst (AI, CFI);
1636+ }
1637+
16261638 // Optimize readonly functions with no meaningful users.
16271639 SILFunction *Fn = AI->getReferencedFunctionOrNull ();
16281640 if (Fn && Fn->getEffectsKind () < EffectsKind::ReleaseNone) {
0 commit comments