swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.32k stars 10.34k forks source link

[SR-2800] Declaring conformance of generic class to NSFetchedResultsControllerDelegate in extension crashes the compiler #45404

Open swift-ci opened 7 years ago

swift-ci commented 7 years ago
Previous ID SR-2800
Radar rdar://problem/21216149
Original Reporter ac (JIRA User)
Type Bug

Attachment: Download

Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, CompilerCrash | |Assignee | @slavapestov | |Priority | Medium | md5: 8ebb4ebdb785ef8c6369a8f2de8d11d3

is duplicated by:

Issue Description:

The following code causes the compiler to crash (crash log is in the attachment):

import CoreData

class A<M>: NSObject where M: NSManagedObject {
}

extension A: NSFetchedResultsControllerDelegate {

  func controller(_ controller: NSFetchedResultsController<M>, didChange anObject: M, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
  }
}

The following code compiles:

class A<M>: NSObject, NSFetchedResultsControllerDelegate where M: NSManagedObject {

  func controller(_ controller: NSFetchedResultsController<M>, didChange anObject: M, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) {
  }
}
belkadan commented 7 years ago
   Assertion failed: (metadata && "extended objc class doesn't have constant metadata?"), function emitCategory, file /Volumes/Data/swift-public/swift/lib/IRGen/GenClass.cpp, line 1115.
Stack dump:
0.  Program arguments: /Volumes/Data/swift-public/build/ninja/swift-macosx-x86_64/bin/swift -frontend -interpret - -target x86_64-apple-macosx10.12 -enable-objc-interop -sdk /Volumes/Data/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -color-diagnostics -module-name main 
1.  While emitting IR for source file <stdin>

@slavapestov, @jckarter, one of you had another like this, right?

slavapestov commented 7 years ago

Yeah, we don't support extensions of generic subclasses of @objc classes. We have to emit the category in the same way we do in JIT mode, where we add it dynamically to runtime instead of emitting a static metadata record for it.

belkadan commented 7 years ago

Smaller example:

import Foundation
protocol SomeProtocol {}
@objc protocol SomeObjCProto {}
class Foo<Bar: SomeProtocol>: NSObject { }
extension Foo: SomeObjCProto { }

We should consider rejecting this in Sema for now.