Unspecialized generic types silently converting to specialized generic types allows nonsensical code to compile
| Originator: | stephen.groom | ||
| Number: | rdar://22323191 | Date Originated: | 18-Aug-2015 12:13 PM |
| Status: | Closed | Resolved: | |
| Product: | Developer Tools | Product Version: | >Xcode 7 |
| Classification: | Serious Bug | Reproducible: | Always |
Consider the following Swift Class:
@objc class StringCreator: NSObject {
class func createStrings(strings: [NSString])
{
for string in strings {
let _ = NSString(string: string)
}
}
}
It accepts an array of NSStrings (NSArray<NSString *> * __nonnull) which seems really good, but it still takes an NSArray * without warning. I can call this from ObjC as follows:
NSArray *anyArray = @[@"string", [NSData data], @3];
[StringCreator createStrings:anyArray];
This crashes at runtime with the error "fatal error: NSArray element failed to match the Swift Array Element type" the second time it tries to iterate the array strings using a for loop.
The fact that this is a crash at runtime rather than a compiler error seriously impairs the usefulness of Objective-C generics. In any other language (Swift included) I would not expect this to compile.
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!
There are no plans to address this based on the following:
This is correct behavior. Eliminating the unspecialized-to-specialized implicit conversion breaks far too much existing code to be feasible.
We are now closing this report.
If you have questions about the resolution, or if this is still a critical issue for you, then please update your bug report with that information.
Please be sure to regularly check new Apple releases for any updates that might affect this issue.