-
Notifications
You must be signed in to change notification settings - Fork 140
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
Improve type inferrence for binary expressions #957
Improve type inferrence for binary expressions #957
Conversation
// If there is no contextually expected type, | ||
// then expect the right type to have the type of the left side. |
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.
This enforcement seems sensible 👍
// Also, if there is a contextually expected type, | ||
// but the left type is a subtype and more specific (i.e not the same), | ||
// then use it instead as the expected type for the right type. | ||
// For example, this allows declarations like the following to type-check: |
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.
👍
Codecov Report
@@ Coverage Diff @@
## master #957 +/- ##
==========================================
+ Coverage 75.36% 75.42% +0.06%
==========================================
Files 268 268
Lines 32666 32729 +63
==========================================
+ Hits 24618 24686 +68
+ Misses 6915 6911 -4
+ Partials 1133 1132 -1
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
Description
I wrote a Base64 encoding function for Cadence here:
https://forum.onflow.org/t/base64-encode-in-cadence/1915/7
This currently requires a lot of static type annotations and casts and could be much simplified through the following observations and changes.
Currently the contextually expected type (if any) is only used for arithmetic binary expressions.
However, bitwise binary expressions have the same property that the resulting type must be compatible with the operands.
Currently the left expression and the right expression are type checked individually, and each side is provided with the contextually expected type (if any).
However, the both sides of arithmetic, logical, etc. expressions must have the same type, so if no contextually expected type is present, use the left type as the expected type when checking the right side.
The contextually expected type might be less specific. For example, the indexing type for arrays and strings is
Integer
. This causes an expression likelet x = 1 as UInt8; "abc"[x + 1]
to fail to type check.With the following changes, this portion of the example program can be significantly simplified:
Before:
After:
Also, improve the tests by
master
branchFiles changed
in the Github PR explorer