Skip to content

[Typechecker] Allow @objc functions to return dynamic self #22006

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
merged 3 commits into from
Jan 23, 2019
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
1 change: 1 addition & 0 deletions lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ bool swift::isRepresentableInObjC(
if (!ResultType->hasError() &&
!ResultType->isVoid() &&
!ResultType->isUninhabited() &&
!ResultType->hasDynamicSelfType() &&
!ResultType->isRepresentableIn(ForeignLanguage::ObjectiveC,
const_cast<FuncDecl *>(FD))) {
if (Diagnose) {
Expand Down
3 changes: 1 addition & 2 deletions test/ClangImporter/objc_parse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ func classAnyObject(_ obj: NSObject) {
class Wobbler : NSWobbling {
@objc func wobble() { }

func returnMyself() -> Self { return self } // expected-error{{non-'@objc' method 'returnMyself()' does not satisfy requirement of '@objc' protocol 'NSWobbling'}}{{none}}
// expected-error@-1{{method cannot be an implementation of an @objc requirement because its result type cannot be represented in Objective-C}}
func returnMyself() -> Self { return self }
}

extension Wobbler : NSMaybeInitWobble { // expected-error{{type 'Wobbler' does not conform to protocol 'NSMaybeInitWobble'}}
Expand Down
37 changes: 37 additions & 0 deletions test/PrintAsObjC/dynamicself.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// REQUIRES: objc_interop

// RUN: %empty-directory(%t)

// FIXME: BEGIN -enable-source-import hackaround
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %S/../Inputs/clang-importer-sdk/swift-modules/ObjectiveC.swift -disable-objc-attr-requires-foundation-module
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %S/../Inputs/clang-importer-sdk/swift-modules/CoreGraphics.swift
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %S/../Inputs/clang-importer-sdk/swift-modules/Foundation.swift
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -o %t %S/../Inputs/clang-importer-sdk/swift-modules/AppKit.swift
// FIXME: END -enable-source-import hackaround


// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -emit-module -I %S/Inputs/custom-modules -o %t %s -disable-objc-attr-requires-foundation-module
// RUN: %target-swift-frontend(mock-sdk: -sdk %S/../Inputs/clang-importer-sdk -I %t) -parse-as-library %t/dynamicself.swiftmodule -typecheck -I %S/Inputs/custom-modules -emit-objc-header-path %t/dynamicself.h -import-objc-header %S/../Inputs/empty.h -disable-objc-attr-requires-foundation-module
// RUN: %FileCheck %s < %t/dynamicself.h
// RUN: %check-in-clang -I %S/Inputs/custom-modules/ %t/dynamicself.h

import Foundation

// CHECK-LABEL: @protocol FooProtocol
@objc protocol FooProtocol {
// CHECK: - (nonnull instancetype)fooFunc SWIFT_WARN_UNUSED_RESULT;
func fooFunc() -> Self
// CHECK: - (nullable instancetype)optionalFooFunc SWIFT_WARN_UNUSED_RESULT;
func optionalFooFunc() -> Self?
}
// CHECK: @end

// CHECK-LABEL: @interface BarClass : NSObject <FooProtocol>
@objc @objcMembers class BarClass: NSObject, FooProtocol {
// CHECK: - (nonnull instancetype)fooFunc SWIFT_WARN_UNUSED_RESULT;
func fooFunc() -> Self { return self }
// CHECK: - (nullable instancetype)optionalFooFunc SWIFT_WARN_UNUSED_RESULT;
func optionalFooFunc() -> Self? { return self }
// CHECK: - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
}
// CHECK: @end
1 change: 0 additions & 1 deletion test/attr/attr_objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,6 @@ class ConcreteContext3 {
func dynamicSelf1() -> Self { return self }

@objc func dynamicSelf1_() -> Self { return self }
// expected-error@-1{{method cannot be marked @objc because its result type cannot be represented in Objective-C}}

@objc func genericParams<T: NSObject>() -> [T] { return [] }
// expected-error@-1{{method cannot be marked @objc because it has generic parameters}}
Expand Down