Skip to content

Commit 2133aa7

Browse files
committed
Add _getMainExecutor
The `getMainExecutor` was declared internal so it doesn't show up in the swift interface files. The definition exists though. The missing declaration causes the compiler to crash while compiling programs with async-main since it can't find the declaration. This patch changes it to conjure up the appropriate decl if it can't find it.
1 parent 5d24d97 commit 2133aa7

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

lib/SILGen/SILGenFunction.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,6 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
928928
FuncDecl *builtinDecl = cast<FuncDecl>(getBuiltinValueDecl(
929929
getASTContext(),
930930
ctx.getIdentifier(getBuiltinName(BuiltinValueKind::CreateAsyncTask))));
931-
932931
auto subs = SubstitutionMap::get(builtinDecl->getGenericSignature(),
933932
{TupleType::getEmpty(ctx)},
934933
ArrayRef<ProtocolConformanceRef>{});
@@ -953,6 +952,7 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
953952

954953
// Get swiftJobRun
955954
FuncDecl *swiftJobRunFuncDecl = SGM.getSwiftJobRun();
955+
assert(swiftJobRunFuncDecl && "Failed to find swift_job_run function decl");
956956
SILFunction *swiftJobRunSILFunc =
957957
SGM.getFunction(SILDeclRef(swiftJobRunFuncDecl, SILDeclRef::Kind::Func),
958958
NotForDefinition);
@@ -970,6 +970,28 @@ void SILGenFunction::emitAsyncMainThreadStart(SILDeclRef entryPoint) {
970970

971971
// Get main executor
972972
FuncDecl *getMainExecutorFuncDecl = SGM.getGetMainExecutor();
973+
if (!getMainExecutorFuncDecl) {
974+
// If it doesn't exist due to an SDK-compiler mismatch, we can conjure one
975+
// up instead of crashing:
976+
// @available(SwiftStdlib 5.5, *)
977+
// @_silgen_name("swift_task_getMainExecutor")
978+
// internal func _getMainExecutor() -> Builtin.Executor
979+
980+
ParameterList *emptyParams = ParameterList::createEmpty(getASTContext());
981+
getMainExecutorFuncDecl = FuncDecl::createImplicit(
982+
getASTContext(), StaticSpellingKind::None,
983+
DeclName(
984+
getASTContext(),
985+
DeclBaseName(getASTContext().getIdentifier("_getMainExecutor")),
986+
/*Arguments*/ emptyParams),
987+
{}, /*async*/ false, /*throws*/ false, {}, emptyParams,
988+
getASTContext().TheExecutorType,
989+
entryPoint.getDecl()->getModuleContext());
990+
getMainExecutorFuncDecl->getAttrs().add(
991+
new (getASTContext())
992+
SILGenNameAttr("swift_task_getMainExecutor", /*implicit*/ true));
993+
}
994+
973995
SILFunction *getMainExeutorSILFunc = SGM.getFunction(
974996
SILDeclRef(getMainExecutorFuncDecl, SILDeclRef::Kind::Func),
975997
NotForDefinition);

0 commit comments

Comments
 (0)