SR-15038 Incorrect "conditional downcast from ... does nothing" diagnostic
Issue Description:
As discussed in SR-15038 when using an array with an optional element of type X forced checked cast to an array of element of unwrapped X causes a runtime crash. Also as discussed there is not clear if that is something that shouldn't be allowed by the type checker and fail to compile or is really the runtime that should handle this.
protocolExperimentDeserializable {
staticfuncdeserializeExperiment(_value: Any) ->Self?
}
extensionString: ExperimentDeserializable {
staticfuncdeserializeExperiment(_value: Any) ->String? { value as?String }
}
extensionInt: ExperimentDeserializable {
staticfuncdeserializeExperiment(_value: Any) ->Int? { value as?Int }
}
classConstant<T> {
privateinit(getUnderlyingValue: @escaping () -> T) {
print(getUnderlyingValue())
}
}
extensionConstantwhere T:Sequence, T.Element:ExperimentDeserializable {
staticfuncfoo<U>(thing: Thing, defaultValue: T) -> T where T == [U] {
guardlet array = thing.storage["foo"] as? [Any] else {
fatalError()
}
let value = array.map(T.Element.deserializeExperiment) as! [T.Element] // runtime crashreturn value
}
}
structThing {
let storage: [String: Any]
}
let thing =Thing(storage: ["foo": ["x", 1]])
let result = Constant.foo(thing: thing, defaultValue: ["a", "b"])
assert(result == ["a", "b"])
Because the second element of array2 is nil, it cannot be cast to a non-optional string. Since you requested a force-cast `as!`, the program crashes when the cast fails.
So I think this is being handled correctly. The program is crashing because the `as!` cast has failed, and the message is correctly explaining why it failed:
Environment
Xcode 13 Beta 4
Additional Detail from JIRA
md5: 42e65820f8dec73893bd2ccaf5ad7ce4
relates to:
Issue Description:
As discussed in SR-15038 when using an array with an optional element of type X forced checked cast to an array of element of unwrapped X causes a runtime crash. Also as discussed there is not clear if that is something that shouldn't be allowed by the type checker and fail to compile or is really the runtime that should handle this.
Runtime crash logs reported by @jpsim
The text was updated successfully, but these errors were encountered: