Swift 2 runtime crash due to a subscript operator declared on an NSObject subclass
| Originator: | gwendal.roue | ||
| Number: | rdar://23122734 | Date Originated: | 2015/10/15 |
| Status: | Resolved: | ||
| Product: | Developer Tools | Product Version: | Xcode 7.0 (7A218) |
| Classification: | Crash | Reproducible: | Always |
Summary:
When an NSObject subclass defines a subscript operator, the Swift compiler accepts invalid code that crashes at runtime.
Steps to Reproduce:
Run the following code (in a Playground or in a project):
import Foundation
class Polluter : NSObject {
subscript (key: String) -> Polluter {
return self
}
func hello() { print("Hello") }
}
let dict: [String: AnyObject] = ["foo": "bar"]
let anyDict = dict as AnyObject
let v = anyDict["foo"]! // Inferred type: Polluter
print(String(reflecting: v)) // "bar"
v.hello() // Compiles, and crash at runtime
Expected Results:
1. The variable v is not inferred to be of type `Polluter`.
2. The `v.hello()` line should not compile.
3. There is no runtime crash.
Actual Results:
1. The variable v is inferred to be of type `Polluter`.
2. The `v.hello()` line compiles.
3. There is a runtime crash.
Version:
Xcode 7.0 (7A218)
OSX 10.10.5 (14F27)
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!