Skip to content

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

Closed

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.

Activity

  1. added
    p: pigeonrelated to pigeon messaging codegen tool
    P2Important issues not at the top of the work list
    on Dec 13, 2022
  2. self-assigned this
    on Feb 24, 2023
  3. github-actions commented on Mar 14, 2023

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  4. locked as resolved and limited conversation to collaborators on Mar 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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