Skip to content

TypeChecker: Fix implicit dynamic inference for extensions methods #3915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2113,11 +2113,7 @@ static void inferDynamic(ASTContext &ctx, ValueDecl *D) {
return;

// Only introduce 'dynamic' on declarations...
if (isa<ExtensionDecl>(D->getDeclContext())) {
// ...in extensions that don't override other declarations.
if (D->getOverriddenDecl())
return;
} else {
if (!isa<ExtensionDecl>(D->getDeclContext())) {
// ...and in classes on decls marked @NSManaged.
if (!D->getAttrs().hasAttribute<NSManagedAttr>())
return;
Expand Down
8 changes: 8 additions & 0 deletions test/IRGen/Inputs/objc_extension_base.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import gizmo

public class SwiftBaseGizmo : Gizmo {
}

extension SwiftBaseGizmo {
public override func frob() {}
}
17 changes: 16 additions & 1 deletion test/IRGen/objc_extensions.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// RUN: rm -rf %t && mkdir %t
// RUN: %build-irgen-test-overlays
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/Inputs -I %t) %s -emit-ir | FileCheck %s
// 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
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/Inputs -I %t) -primary-file %s -emit-ir | FileCheck %s

// REQUIRES: CPU=x86_64
// REQUIRES: objc_interop

import Foundation
import gizmo
import objc_extension_base

// Check that metadata for nested enums added in extensions to imported classes
// gets emitted concretely.
Expand Down Expand Up @@ -157,3 +159,16 @@ class NSDogcow : NSObject {}
extension NSDogcow {
@NSManaged var woof: Int
}

class SwiftSubGizmo : SwiftBaseGizmo {

// Don't crash on this call. Emit an objC method call to super.
//
// CHECK-LABEL: define {{.*}} @_TFC15objc_extensions13SwiftSubGizmo4frobfT_T_
// CHECK: _TMaC15objc_extensions13SwiftSubGizmo
// CHECK: objc_msgSendSuper2
// CHECK: ret
public override func frob() {
super.frob()
}
}
1 change: 1 addition & 0 deletions test/SILGen/Inputs/usr/include/objc_extensions_helper.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@import Foundation;

@interface Base : NSObject
- (void)objCBaseMethod;
@property (nonatomic, strong) NSString *prop;
@end
12 changes: 10 additions & 2 deletions test/SILGen/objc_extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ extension Sub {

func foo() {
}

override func objCBaseMethod() {}
}

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

Expand All @@ -45,7 +47,13 @@ extension Sub {
}
}

class SubSub : Sub { }
class SubSub : Sub {
// CHECK-LABEL: sil hidden @_TFC15objc_extensions6SubSub14objCBaseMethodfT_T_
// CHECK: super_method [volatile] %0 : $SubSub, #Sub.objCBaseMethod!1.foreign : (Sub) -> () -> () , $@convention(objc_method) (Sub) -> ()
override func objCBaseMethod() {
super.objCBaseMethod()
}
}

extension SubSub {
// CHECK-LABEL: sil hidden @_TFC15objc_extensions6SubSubs9otherPropSS
Expand Down
8 changes: 7 additions & 1 deletion test/attr/attr_dynamic_infer.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// RUN: %target-swift-ide-test -print-ast-typechecked -source-filename=%s -print-implicit-attrs -disable-objc-attr-requires-foundation-module | FileCheck %s

@objc class Super {}
@objc class Super {
func baseFoo() {}
}

// CHECK: extension Super {
extension Super {
Expand Down Expand Up @@ -47,4 +49,8 @@ extension Sub {
// CHECK: @objc override dynamic set
set { }
}

// CHECK: @objc override dynamic func baseFoo
override func baseFoo() {
}
}