swift icon indicating copy to clipboard operation
swift copied to clipboard

[SR-2348] Compiler crash or error when conforming Objective-C generic type to `Sequence`

Open jadengeller opened this issue 10 years ago • 8 comments

Previous ID SR-2348
Radar None
Original Reporter @jadengeller
Type Bug

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

md5: c38dcabc918890090a9425541ab5131a

Issue Description:

Currently, conforming an Objective-C generic type to `Sequence` is not possible in Swift. I think that the problem may be more general in that you cannot conform to a protocol with associated types, but I'm unsure.

Here's a recreation of the problem: https://gist.github.com/JadenGeller/6672c8a55600f2ce415bcf60da6dd24a

jadengeller avatar Aug 16 '16 03:08 jadengeller

@slavapestov, have we seen this before?

belkadan avatar Aug 16 '16 03:08 belkadan

It's almost entirely impossible to write extensions on Objective-C generic types in Swift. For example, I can't write `addObjectsFromSequence` as an extension on `NSArray` without accessing the generic type. And even some methods that seem like they wouldn't require runtime knowledge of the generic type are not possible to write.

jadengeller avatar Aug 16 '16 03:08 jadengeller

Might be a duplicate of https://bugs.swift.org/browse/SR-1779

jadengeller avatar Aug 16 '16 03:08 jadengeller

cc @jckarter, although really I think those (valid) complaints deserve a separate bug from this crash.

belkadan avatar Aug 16 '16 03:08 belkadan

Yeah, I think it's a dupe of SR-1779. We need to store the type parameters in the conformance for Objective-C generic types.

slavapestov avatar Aug 16 '16 05:08 slavapestov

This is a more specific problem than SR-1779. Returning IteratorOfOne<Any> makes the method @nonobjc, since IteratorOfOne isn't representable in ObjC, and we conservatively assume that all @nonobjc declarations require real type metadata.

jckarter avatar Aug 16 '16 18:08 jckarter

However, trying to work around that does reveal a different compiler crash:

@import Foundation;

@interface Box<T> : NSObject
@property (nonatomic) T _Nonnull value;
@end

struct BoxIterator {
  id __unsafe_unretained _Nullable value;
};
extension Box: Sequence {
    public func makeIterator() -> BoxIterator {
        let unmanagedSelf = Unmanaged.passRetained(self as AnyObject)
        return BoxIterator(value: unmanagedSelf.autorelease())
    }
}

extension BoxIterator: IteratorProtocol {
  public mutating func next() -> Any? {
    let result = self.value
    self.value = nil
    return result
  }
}

jckarter avatar Aug 16 '16 18:08 jckarter

The fixes in https://github.com/apple/swift/pull/4324 should at least allow you to conform to a protocol using @objc methods.

jckarter avatar Aug 16 '16 21:08 jckarter