-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
Currently we have a lot of deserialization casts that are using as?, such as (from the AllTypes Swift code):
let aBool = list[0] as? Bool
let anInt = list[1] as? Int32
let aDouble = list[2] as? Double
...
Because that wouldn't compile for non-nullable fields (since as? returns nil/null on failure), the non-nullable version looks like:
let aBool = list[0] as! Bool
let anInt = list[1] as! Int32
let aDouble = list[2] as! Double
(Similar for Kotlin, but with as.)
The former isn't right though; if a field is supposed to be a Bool?, but we somehow send, say, a Double, we shouldn't treat it as a null Bool?, we should explode because something has gone terribly wrong.
This should let us eliminate a lot of branching in the generators for this part of the code, since we should always do as! (or as) with the full type. I.e., the nullable AllTypes code in Swift should be:
let aBool = list[0] as! Bool?
let anInt = list[1] as! Int32?
let aDouble = list[2] as! Double?
...
See also #116972, but this is distinct because we are actually actually silently masking any error that happens here, rather than just doing extra work.