Skip to content

Commit d6143a5

Browse files
authored
Merge pull request #61937 from CodaFi/mcmainerberry
[SILGen] Look for the C-Language Copy of UIApplicationMain
2 parents d74daea + fc52e7a commit d6143a5

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/SILGen/SILGenFunction.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -631,16 +631,22 @@ void SILGenFunction::emitArtificialTopLevel(Decl *mainDecl) {
631631
->loadModule(SourceLoc(),
632632
ImportPath::Module(llvm::makeArrayRef(UIKitName)));
633633
assert(UIKit && "couldn't find UIKit objc module?!");
634-
SmallVector<ValueDecl *, 1> results;
634+
SmallVector<ValueDecl *, 2> results;
635635
UIKit->lookupQualified(UIKit,
636636
DeclNameRef(ctx.getIdentifier("UIApplicationMain")),
637637
NL_QualifiedDefault,
638638
results);
639-
assert(results.size() == 1
640-
&& "couldn't find a unique UIApplicationMain in the UIKit ObjC "
641-
"module?!");
642639

643-
ValueDecl *UIApplicationMainDecl = results.front();
640+
// As the comment above alludes, using a qualified lookup into UIKit is
641+
// *not* sound. In particular, it's possible for the lookup to find the
642+
// (deprecated) Swift copy of UIApplicationMain in UIKit and try to call
643+
// that instead of the C entrypoint. Let's try to force this to happen.
644+
auto FoundUIApplicationMain = llvm::find_if(results, [](const ValueDecl *VD) {
645+
return !VD->getClangNode().isNull();
646+
});
647+
assert(FoundUIApplicationMain != results.end() &&
648+
"Could not find a UIApplicationMain to call!");
649+
ValueDecl *UIApplicationMainDecl = *FoundUIApplicationMain;
644650

645651
auto mainRef = SILDeclRef(UIApplicationMainDecl).asForeign();
646652
SILGenFunctionBuilder builder(SGM);

0 commit comments

Comments
 (0)