SourceKit crashing when assigning result of static function that returns Optional<Self> to variable
| Originator: | addison.f.webb | ||
| Number: | rdar://28134633 | Date Originated: | 9/2/16 |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | |
| Classification: | Crash | Reproducible: | Always |
Summary:
When a protocol is defined with a static function that returns an optional self (`Optional<Self>`) and is implemented by a `final` class, SourceKit will crash when trying to call that method and assign it's return value to a variable.
Steps to Reproduce:
1. Open radar.playground
2. See SourceKit crash almost immediately
Expected Results:
SourceKit would not crash but be able to interpret code.
Actual Results:
SourceKit crashes.
Version:
- OS X 10.11.6 (15G1004)
- Xcode 7.3.1 (7D1014)
- Swift 2.2
Notes:
Configuration:
This will occur in both Swift Playgrounds as well as an Xcode project when implementing the same pattern as in the example playground.
----------------------------------------------------------------------
// radar.playground
import UIKit
protocol Foo {
var name: String { get }
static func fooObject() -> Optional<Self>
// static func fooObject() -> Self? // works
}
final class Kung: Foo {
var name = "Kung"
static func fooObject() -> Kung? {
return Kung()
}
}
final class Bar: Foo {
var name = "Bar"
static func fooObject() -> Bar? {
return Bar()
}
}
// a helper function for one of the work arounds
func getInstance(forType type: Foo.Type) -> Foo? {
return type.fooObject()
}
// works
//func printName(forType type: Foo.Type) {
// let instance = getInstance(forType: type)
// print(instance?.name)
//}
// works
//func printName(forType type: Foo.Type) {
// print(type.fooObject()?.name)
//}
// doesn't work
func printName(forType type: Foo.Type) {
let fooObject = type.fooObject() // crashes on this line
print(fooObject?.name)
}
printName(forType: Kung.self)
printName(forType: Bar.self)
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!