@@ -631,16 +631,22 @@ void SILGenFunction::emitArtificialTopLevel(Decl *mainDecl) {
631
631
->loadModule (SourceLoc (),
632
632
ImportPath::Module (llvm::makeArrayRef (UIKitName)));
633
633
assert (UIKit && " couldn't find UIKit objc module?!" );
634
- SmallVector<ValueDecl *, 1 > results;
634
+ SmallVector<ValueDecl *, 2 > results;
635
635
UIKit->lookupQualified (UIKit,
636
636
DeclNameRef (ctx.getIdentifier (" UIApplicationMain" )),
637
637
NL_QualifiedDefault,
638
638
results);
639
- assert (results.size () == 1
640
- && " couldn't find a unique UIApplicationMain in the UIKit ObjC "
641
- " module?!" );
642
639
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;
644
650
645
651
auto mainRef = SILDeclRef (UIApplicationMainDecl).asForeign ();
646
652
SILGenFunctionBuilder builder (SGM);
0 commit comments