-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[pigeon] fix swift nsnull casting crash #3545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
574ee0b
crash fix
tarrinneal 46ca75e
remove need for function
tarrinneal e350040
cleaner method method
tarrinneal 3d31aff
better name
tarrinneal 3be7eb1
fix enums and tests
tarrinneal ef111ee
enum fixes, and Any casting
tarrinneal 6d5271f
gen test
tarrinneal a4625f8
Merge branch 'main' of github.com:flutter/packages into swift-nil
tarrinneal 29ac0ec
simplify casting enum
tarrinneal 4a7b408
Figured it out
tarrinneal d1d9c6f
Better name
tarrinneal 241f0e3
gen tests
tarrinneal 8d356cc
some nits
tarrinneal 04dfdbd
writedecodecasting init
tarrinneal ed57271
This works, but does it explode on incorect type?
tarrinneal 1bd3afa
cleaner with as Any
tarrinneal d0f9b86
gen test
tarrinneal fab7ca3
Merge branch 'main' of github.com:flutter/packages into swift-nil
tarrinneal 2e2d337
comment
tarrinneal 5b39dbe
fix NSNull issue in EchoBinaryMessenger
tarrinneal cbb8e0b
makes things better
tarrinneal 7fa0f7d
gen tests
tarrinneal b0c7653
Improve _writeDecodeCasting
tarrinneal 740abd8
Merge branch 'main' of github.com:flutter/packages into swift-nil
tarrinneal 748a0bb
assert message
tarrinneal d8f5fe3
nits
tarrinneal 06ab71e
comment
tarrinneal 547ed02
nested ternary
tarrinneal 367009e
gen test
tarrinneal cdf35ff
nits
tarrinneal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Improve _writeDecodeCasting
- Loading branch information
commit b0c7653c5ee2d9c158fa514cca54c39d838bfef9
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It occurs to me that we should reverse the conditional based on what I found about
NSNumber; as long as the underlying value actually is anNSNumber, "casting" toInt64directly will work, so checking forInt32first (which will also work) will result in two conversions (NSNumber->Int32>Int64) instead of one.So:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That doesn't work for nullables though
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And I'm fairly certain it doesn't lower the conversion amount, since casting checking the cast type doesn't perform an actual casting
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But the cast does. In the version you have in the PR now:
The flow for a NSNumber (what the codec currently produces) with a value that fits in 32 bits (which is going to be ~all of them in most usage) is, according to the evolution doc and observed behavior:
is Int32-> true, because the value can be safely converted toInt32(despite appearances, this is not actually just doing a type check;NSNumberis not anInt32or anInt64, butiswill return true for it if it can convert).as! Int32-> does a conversion, not a cast (again, despite appearances), creating anInt32from theNSNumber's value.Int64(theAnonymousNewInt32)-> does a conversion from Int32 toInt64, creating anInt64and discarding theInt32.What we want is to convert the
NSNumberdirectly to anInt64, which always work.Ah, right. Maybe we should just make ints fully custom then:
non-nullable:
let foo : Int64 = $value is Int64 ? $value as! Int64 : Int64($value as! Int32)nullable:
let foo : Int64? = $value is NSNull ? nil : $value is Int64? ? $value as! Int64? : Int64($value as! Int32)(Usually I think nested ternaries are a crime against nature, but here it's short and in generated code, and we can put a comment in the Dart explaining the logic, so I think it's okay, and it avoids the mess of generating extra variables.)