@@ -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 ());
@@ -1627,6 +1635,10 @@ SILInstruction *SILCombiner::visitTryApplyInst(TryApplyInst *AI) {
16271635 if (isa<PartialApplyInst>(AI->getCallee ()))
16281636 return nullptr ;
16291637
1638+ if (auto *CFI = dyn_cast<ConvertFunctionInst>(AI->getCallee ())) {
1639+ return optimizeApplyOfConvertFunctionInst (AI, CFI);
1640+ }
1641+
16301642 // Optimize readonly functions with no meaningful users.
16311643 SILFunction *Fn = AI->getReferencedFunctionOrNull ();
16321644 if (Fn && Fn->getEffectsKind () < EffectsKind::ReleaseNone) {
0 commit comments