Swift compiler crashes when using generic type constrained to both class and protocol
| Originator: | kristopherdjohnson | ||
| Number: | rdar://17929884 | Date Originated: | 06-Aug-2014 07:35 AM |
| Status: | Closed/Fixed | Resolved: | Fixed |
| Product: | Developer Tools | Product Version: | Xcode 6 beta 5 (6A279r) |
| Classification: | Crash/Hang/Data Loss | Reproducible: | Always |
Summary:
If a method is defined to use a generic type that is constrained to be a subclass of a specified class, and to also implement a specified protocol, the compiler crashes.
Steps to Reproduce:
Paste this code into a playground:
protocol CanBark {
func bark() -> String
}
class Animal {
func animalType() -> String {
return "Animal"
}
}
class Dog: Animal, CanBark {
override func animalType() -> String {
return "Dog"
}
func bark() -> String {
return "Woof"
}
}
func makeAnimalBark<T where T: CanBark, T: Animal>(barkingAnimal: T) {
let animalType = barkingAnimal.animalType()
let bark = barkingAnimal.bark()
println("\(animalType) says \"\(bark)\"")
}
let dog = Dog()
makeAnimalBark(dog) // Dog says "Woof"
Expected Results:
Should print 'Dog says "Woof"'
Actual Results:
Compiler crashes.
Version:
Xcode 6 beta 5 (6A279r)
Notes:
A workaround is to explicitly cast the argument to the protocol type before calling the function defined by the protocol, like this:
func makeAnimalBark<T where T: CanBark, T: Animal>(barkingAnimal: T) {
let animalType = barkingAnimal.animalType()
let canBark = barkingAnimal as CanBark
let bark = canBark.bark()
println("\(animalType) says \"\(bark)\"")
}
Configuration:
OS X 10.9.4
Attachments:
'com.apple.dt.Xcode.Playground_2014-08-06-072356_pluto.crash' was successfully uploaded.
----
Apple Developer Relations 29-Sep-2014 06:54 PM
We believe this issue has been addressed in the latest Xcode 6.1 beta.
This pre-release version of the Xcode 6.1 developer tools is required when developing for OS X Yosemite, and includes the Swift programming language. Xcode 6.1 beta runs on OS X Mavericks and OS X Yosemite Developer Preview.
Xcode 6.1 beta - Build 6A1042b
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!