Skip to content
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

[SR-7067] Imported Obj-C class is incorrectly assumed to implement all optional protocol methods #49615

Closed
lilyball mannequin opened this issue Feb 23, 2018 · 2 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@lilyball
Copy link
Mannequin

lilyball mannequin commented Feb 23, 2018

Previous ID SR-7067
Radar None
Original Reporter @lilyball
Type Bug
Status Resolved
Resolution Duplicate

Attachment: Download

Environment

Xcode 9.2 (9C40b)
Apple Swift version 4.0.3 (swiftlang-900.0.74.1 clang-900.0.39.2)
Target: x86_64-apple-macosx10.9

Also occurs with the latest 4.1 snapshot:
Apple Swift version 4.1-dev (LLVM c4ec2ab808, Clang d8b11579e8, Swift c8e3724)
Target: x86_64-apple-darwin16.7.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 09409a9d09b0830edfcee49a1007453c

duplicates:

  • SR-6993 Swift imports @optional ObjC methods from superclass protocol as if implemented

Issue Description:

When an Obj-C class conforms to a protocol with optional methods, and you use that Obj-C class from Swift, the compiler incorrectly assumes the class implements all of the optional methods. You can see this in the autocomplete menu where it lists a non-optional version of the method right next to the IUO-version of the method. And you can confirm this by trying to use optional-chaining on the method and the seeing a compiler error telling you the method isn't optional.

// ObjCClass.h
@protocol Foo <NSObject>
@optional
- (nonnull NSString *)foobar;
@end

@interface ObjCClass : NSObject <Foo>
@end
// SomeSwiftFile.swift
let foo = ObjCClass()
print(foo.foobar?() ?? "unimplemented")
// error: Cannot use optional chaining on non-optional value of type '() -> String'

And here's what autocomplete is showing:
![](Screen Shot 2018-02-23 at 2.12.06 PM.png)

@lilyball
Copy link
Mannequin Author

lilyball mannequin commented Feb 23, 2018

Thankfully invoking the method from Swift can be accomplished by casting the object to the protocol first, where the method is still optional (well, IUO). But it's awkward and really easy to miss and attempt to invoke a method that doesn't exist.

@belkadan
Copy link
Contributor

(Consolidating.)

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

1 participant