Skip to content

Commit 6158b3a

Browse files
Merge pull request #3915 from aschwaighofer/fix_implicit_dynamic_for_extension
TypeChecker: Fix implicit dynamic inference for extensions methods
2 parents 4c671e9 + 723e2f9 commit 6158b3a

File tree

6 files changed

+43
-9
lines changed

6 files changed

+43
-9
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,11 +2113,7 @@ static void inferDynamic(ASTContext &ctx, ValueDecl *D) {
21132113
return;
21142114

21152115
// Only introduce 'dynamic' on declarations...
2116-
if (isa<ExtensionDecl>(D->getDeclContext())) {
2117-
// ...in extensions that don't override other declarations.
2118-
if (D->getOverriddenDecl())
2119-
return;
2120-
} else {
2116+
if (!isa<ExtensionDecl>(D->getDeclContext())) {
21212117
// ...and in classes on decls marked @NSManaged.
21222118
if (!D->getAttrs().hasAttribute<NSManagedAttr>())
21232119
return;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import gizmo
2+
3+
public class SwiftBaseGizmo : Gizmo {
4+
}
5+
6+
extension SwiftBaseGizmo {
7+
public override func frob() {}
8+
}

test/IRGen/objc_extensions.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
// RUN: rm -rf %t && mkdir %t
22
// RUN: %build-irgen-test-overlays
3-
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/Inputs -I %t) %s -emit-ir | FileCheck %s
3+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/Inputs -I %t) -disable-objc-attr-requires-foundation-module -emit-module %S/Inputs/objc_extension_base.swift -o %t
4+
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/Inputs -I %t) -primary-file %s -emit-ir | FileCheck %s
45

56
// REQUIRES: CPU=x86_64
67
// REQUIRES: objc_interop
78

89
import Foundation
910
import gizmo
11+
import objc_extension_base
1012

1113
// Check that metadata for nested enums added in extensions to imported classes
1214
// gets emitted concretely.
@@ -157,3 +159,16 @@ class NSDogcow : NSObject {}
157159
extension NSDogcow {
158160
@NSManaged var woof: Int
159161
}
162+
163+
class SwiftSubGizmo : SwiftBaseGizmo {
164+
165+
// Don't crash on this call. Emit an objC method call to super.
166+
//
167+
// CHECK-LABEL: define {{.*}} @_TFC15objc_extensions13SwiftSubGizmo4frobfT_T_
168+
// CHECK: _TMaC15objc_extensions13SwiftSubGizmo
169+
// CHECK: objc_msgSendSuper2
170+
// CHECK: ret
171+
public override func frob() {
172+
super.frob()
173+
}
174+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
@import Foundation;
22

33
@interface Base : NSObject
4+
- (void)objCBaseMethod;
45
@property (nonatomic, strong) NSString *prop;
56
@end

test/SILGen/objc_extensions.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ extension Sub {
2020

2121
func foo() {
2222
}
23+
24+
override func objCBaseMethod() {}
2325
}
2426

2527
// CHECK-LABEL: sil hidden @_TF15objc_extensions20testOverridePropertyFCS_3SubT_
2628
func testOverrideProperty(_ obj: Sub) {
27-
// CHECK: = function_ref @_TFC15objc_extensions3Subs4propGSQSS_
29+
// CHECK: = class_method [volatile] %0 : $Sub, #Sub.prop!setter.1.foreign : (Sub) -> (String!) -> ()
2830
obj.prop = "abc"
2931
} // CHECK: }
3032

@@ -45,7 +47,13 @@ extension Sub {
4547
}
4648
}
4749

48-
class SubSub : Sub { }
50+
class SubSub : Sub {
51+
// CHECK-LABEL: sil hidden @_TFC15objc_extensions6SubSub14objCBaseMethodfT_T_
52+
// CHECK: super_method [volatile] %0 : $SubSub, #Sub.objCBaseMethod!1.foreign : (Sub) -> () -> () , $@convention(objc_method) (Sub) -> ()
53+
override func objCBaseMethod() {
54+
super.objCBaseMethod()
55+
}
56+
}
4957

5058
extension SubSub {
5159
// CHECK-LABEL: sil hidden @_TFC15objc_extensions6SubSubs9otherPropSS

test/attr/attr_dynamic_infer.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// RUN: %target-swift-ide-test -print-ast-typechecked -source-filename=%s -print-implicit-attrs -disable-objc-attr-requires-foundation-module | FileCheck %s
22

3-
@objc class Super {}
3+
@objc class Super {
4+
func baseFoo() {}
5+
}
46

57
// CHECK: extension Super {
68
extension Super {
@@ -47,4 +49,8 @@ extension Sub {
4749
// CHECK: @objc override dynamic set
4850
set { }
4951
}
52+
53+
// CHECK: @objc override dynamic func baseFoo
54+
override func baseFoo() {
55+
}
5056
}

0 commit comments

Comments
 (0)