Skip to content

[pigeon] Remove most use of safe casting in Swift and Kotlin generators #116999

@stuartmorgan-g

Description

@stuartmorgan-g

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.

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work listp: pigeonrelated to pigeon messaging codegen toolpackageflutter/packages repository. See also p: labels.platform-androidAndroid applications specificallyplatform-iosiOS applications specifically

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions