Skip to content

Commit 6d93097

Browse files
authored
Merge pull request #9317 from slavapestov/overrides-of-imported-methods-are-dynamic
Sema: Infer 'dynamic' for overrides of imported methods
2 parents cc707c7 + bcbd1d2 commit 6d93097

File tree

3 files changed

+21
-14
lines changed

3 files changed

+21
-14
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2522,7 +2522,8 @@ static Optional<ObjCReason> shouldMarkAsObjC(TypeChecker &TC,
25222522
///
25232523
/// This occurs when
25242524
/// - it is implied by an attribute like @NSManaged
2525-
/// - we need to dynamically dispatch to a method in an extension.
2525+
/// - when we have an override of an imported method
2526+
/// - we need to dynamically dispatch to a method in an extension
25262527
///
25272528
/// FIXME: The latter reason is a hack. We should figure out how to safely
25282529
/// put extension methods into the class vtable.
@@ -2535,11 +2536,15 @@ static void inferDynamic(ASTContext &ctx, ValueDecl *D) {
25352536
if (!D->isObjC() || D->hasClangNode())
25362537
return;
25372538

2539+
bool overridesImportedMethod =
2540+
(D->getOverriddenDecl() &&
2541+
D->getOverriddenDecl()->hasClangNode());
2542+
25382543
// Only introduce 'dynamic' on declarations...
25392544
bool isNSManaged = D->getAttrs().hasAttribute<NSManagedAttr>();
25402545
if (!isa<ExtensionDecl>(D->getDeclContext())) {
25412546
// ...and in classes on decls marked @NSManaged.
2542-
if (!isNSManaged)
2547+
if (!isNSManaged && !overridesImportedMethod)
25432548
return;
25442549
}
25452550

test/SILGen/objc_thunks.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,9 @@ extension Hoozit {
445445
// Calling objc methods of subclass should go through native entry points
446446
func useHoozit(_ h: Hoozit) {
447447
// sil @_T011objc_thunks9useHoozityAA0D0C1h_tF
448-
// In the class decl, gets dynamically dispatched
448+
// In the class decl, overrides importd method, 'dynamic' was inferred
449449
h.fork()
450-
// CHECK: class_method {{%.*}} : {{.*}}, #Hoozit.fork!1 :
450+
// CHECK: class_method [volatile] {{%.*}} : {{.*}}, #Hoozit.fork!1.foreign
451451

452452
// In an extension, 'dynamic' was inferred.
453453
h.foof()

test/SILGen/vtables.swift

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,23 +88,25 @@ class Hoozit : Gizmo {
8888
// Entries only exist for native Swift methods
8989

9090
// CHECK: sil_vtable Hoozit {
91-
// CHECK: #Hoozit.frob!1: {{.*}} : _T07vtables6HoozitC4frob{{[_0-9a-zA-Z]*}}F
92-
// CHECK: #Hoozit.funge!1: {{.*}} : _T07vtables6HoozitC5funge{{[_0-9a-zA-Z]*}}F
93-
// CHECK: #Hoozit.anse!1: {{.*}} : _T07vtables6HoozitC4anse{{[_0-9a-zA-Z]*}}F
94-
// CHECK: #Hoozit.incorrige!1: {{.*}} : _T07vtables6HoozitC9incorrige{{[_0-9a-zA-Z]*}}F
95-
// CHECK: }
91+
// CHECK-NEXT: #Hoozit.anse!1: {{.*}} : _T07vtables6HoozitC4anse{{[_0-9a-zA-Z]*}}F
92+
// CHECK-NEXT: #Hoozit.incorrige!1: {{.*}} : _T07vtables6HoozitC9incorrige{{[_0-9a-zA-Z]*}}F
93+
// CHECK-NEXT: #Hoozit.init!initializer.1: (Hoozit.Type) -> () -> Hoozit! : _T07vtables6HoozitCSQyACGycfc
94+
// CHECK-NEXT: #Hoozit.init!initializer.1: (Hoozit.Type) -> (Int) -> Hoozit! : _T07vtables6HoozitCSQyACGSi7bellsOn_tcfc
95+
// CHECK-NEXT: #Hoozit.deinit!deallocator: _T07vtables6HoozitCfD
96+
// CHECK-NEXT: }
9697

9798
class Wotsit : Hoozit {
9899
override func funge() {}
99100
override func incorrige() {}
100101
}
101102

102103
// CHECK: sil_vtable Wotsit {
103-
// CHECK: #Hoozit.frob!1: {{.*}} : _T07vtables6HoozitC4frob{{[_0-9a-zA-Z]*}}F
104-
// CHECK: #Hoozit.funge!1: {{.*}} : _T07vtables6WotsitC5funge{{[_0-9a-zA-Z]*}}F
105-
// CHECK: #Hoozit.anse!1: {{.*}} : _T07vtables6HoozitC4anse{{[_0-9a-zA-Z]*}}F
106-
// CHECK: #Hoozit.incorrige!1: {{.*}} : _T07vtables6WotsitC9incorrige{{[_0-9a-zA-Z]*}}F
107-
// CHECK: }
104+
// CHECK-NEXT: #Hoozit.anse!1: {{.*}} : _T07vtables6HoozitC4anse{{[_0-9a-zA-Z]*}}F
105+
// CHECK-NEXT: #Hoozit.incorrige!1: {{.*}} : _T07vtables6WotsitC9incorrige{{[_0-9a-zA-Z]*}}F
106+
// CHECK-NEXT: #Hoozit.init!initializer.1: (Hoozit.Type) -> () -> Hoozit! : _T07vtables6WotsitCSQyACGycfc
107+
// CHECK-NEXT: #Hoozit.init!initializer.1: (Hoozit.Type) -> (Int) -> Hoozit! : _T07vtables6WotsitCSQyACGSi7bellsOn_tcfc
108+
// CHECK-NEXT: #Wotsit.deinit!deallocator: _T07vtables6WotsitCfD
109+
// CHECK-NEXT: }
108110

109111
// <rdar://problem/15282548>
110112
// CHECK: sil_vtable Base {

0 commit comments

Comments
 (0)