Skip to content

Commit aee0b4e

Browse files
authored
Merge pull request #22006 from theblixguy/fix/SR-7601
[Typechecker] Allow @objc functions to return dynamic self
2 parents 6d10e4e + c3e0747 commit aee0b4e

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

lib/Sema/TypeCheckDeclObjC.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ bool swift::isRepresentableInObjC(
523523
if (!ResultType->hasError() &&
524524
!ResultType->isVoid() &&
525525
!ResultType->isUninhabited() &&
526+
!ResultType->hasDynamicSelfType() &&
526527
!ResultType->isRepresentableIn(ForeignLanguage::ObjectiveC,
527528
const_cast<FuncDecl *>(FD))) {
528529
if (Diagnose) {

test/ClangImporter/objc_parse.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,7 @@ func classAnyObject(_ obj: NSObject) {
273273
class Wobbler : NSWobbling {
274274
@objc func wobble() { }
275275

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

280279
extension Wobbler : NSMaybeInitWobble { // expected-error{{type 'Wobbler' does not conform to protocol 'NSMaybeInitWobble'}}

test/PrintAsObjC/dynamicself.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// REQUIRES: objc_interop
2+
3+
// RUN: %empty-directory(%t)
4+
5+
// FIXME: BEGIN -enable-source-import hackaround
6+
// 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
7+
// 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
8+
// 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
9+
// 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
10+
// FIXME: END -enable-source-import hackaround
11+
12+
13+
// 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
14+
// 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
15+
// RUN: %FileCheck %s < %t/dynamicself.h
16+
// RUN: %check-in-clang -I %S/Inputs/custom-modules/ %t/dynamicself.h
17+
18+
import Foundation
19+
20+
// CHECK-LABEL: @protocol FooProtocol
21+
@objc protocol FooProtocol {
22+
// CHECK: - (nonnull instancetype)fooFunc SWIFT_WARN_UNUSED_RESULT;
23+
func fooFunc() -> Self
24+
// CHECK: - (nullable instancetype)optionalFooFunc SWIFT_WARN_UNUSED_RESULT;
25+
func optionalFooFunc() -> Self?
26+
}
27+
// CHECK: @end
28+
29+
// CHECK-LABEL: @interface BarClass : NSObject <FooProtocol>
30+
@objc @objcMembers class BarClass: NSObject, FooProtocol {
31+
// CHECK: - (nonnull instancetype)fooFunc SWIFT_WARN_UNUSED_RESULT;
32+
func fooFunc() -> Self { return self }
33+
// CHECK: - (nullable instancetype)optionalFooFunc SWIFT_WARN_UNUSED_RESULT;
34+
func optionalFooFunc() -> Self? { return self }
35+
// CHECK: - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
36+
}
37+
// CHECK: @end

test/attr/attr_objc.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,6 @@ class ConcreteContext3 {
323323
func dynamicSelf1() -> Self { return self }
324324

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

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

0 commit comments

Comments
 (0)