Skip to content
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

Use common super-type for expression type inferring #1027

Merged
merged 20 commits into from
Nov 18, 2021

Conversation

SupunS
Copy link
Member

@SupunS SupunS commented Jun 21, 2021

Closes #61
Closes #1239

Description

This PR adds support for inferring the common supertype for:

  • Array literals
  • Dictionary literals
  • Conditional expression

Breaking Changes

With these changes, array literals and dictionary literals no longer use the first element to infer the type for the literal. Thus, if it requires a literal to have a certain specific type, then explicit type annotation or casting must be used.
e.g:
Old way

var x = [1 as UInt8, 6, 35]

New Way

var x = [1, 6, 35] as [UInt8]

  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@SupunS SupunS self-assigned this Jun 21, 2021
@SupunS SupunS force-pushed the supun/common-supertype-3 branch 2 times, most recently from b0d42b2 to 5ca55cb Compare June 21, 2021 11:33
@SupunS SupunS added the Feature label Jun 21, 2021
@SupunS SupunS force-pushed the supun/common-supertype-usages branch from 6ce5a41 to ea32ec5 Compare June 28, 2021 05:08
@SupunS SupunS force-pushed the supun/common-supertype-usages branch from ca1a55e to d362440 Compare June 28, 2021 06:40
@SupunS SupunS added the Language Breaking Change Breaks Cadence contracts deployed on Mainnet label Jun 28, 2021
@SupunS SupunS force-pushed the supun/common-supertype-usages branch from d39e167 to cc3c169 Compare June 28, 2021 10:34
@SupunS SupunS force-pushed the supun/common-supertype-usages branch from cc3c169 to 8a5de64 Compare June 28, 2021 10:34
@SupunS SupunS marked this pull request as ready for review June 28, 2021 10:59
@SupunS SupunS requested a review from turbolent as a code owner June 28, 2021 10:59
@SupunS SupunS mentioned this pull request Aug 13, 2021
6 tasks
@@ -233,41 +231,6 @@ func (checker *Checker) recordVariableDeclarationRange(
)
}

func (checker *Checker) checkVariableDeclarationUsability(declaration *ast.VariableDeclaration) {
Copy link
Member Author

@SupunS SupunS Nov 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check now happens at the expression level for arrays/dictionaries. Because:

  • Need to infer the type even in non-variable-delectation scenarios. e.g: the expression [].getType() should have a type inferred for the empty array.
  • Even if the array is not empty, there could be non-inferrable scenarios
    e.g: [<some_struct>, <some_resource>]

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@SupunS SupunS requested a review from dsainati1 November 6, 2021 01:53
@SupunS SupunS mentioned this pull request Nov 10, 2021
6 tasks
Base automatically changed from supun/common-supertype-3 to master November 11, 2021 00:17
@github-actions
Copy link

github-actions bot commented Nov 11, 2021

Cadence Benchstat comparison

This branch with compared with the base branch onflow:master commit 81f00f2
The command go test ./... -run=XXX -bench=. -shuffle=on -count N was used.
Bench tests were run a total of 7 times on each branch.

Results

old.txtnew.txt
time/opdelta
ParseArray-221.6ms ± 0%22.0ms ± 2%+1.81%(p=0.003 n=5+7)
NewInterpreter/new_interpreter-2943ns ± 0%949ns ± 0%+0.67%(p=0.002 n=6+6)
RuntimeStorageWriteCached-2117µs ± 0%117µs ± 0%~(p=0.165 n=7+7)
RuntimeFungibleTokenTransfer-2890µs ± 1%884µs ± 1%~(p=0.128 n=7+7)
ParseInfix-222.9µs ± 1%23.1µs ± 1%~(p=0.073 n=7+7)
ParseFungibleToken-2427µs ± 1%429µs ± 1%~(p=0.259 n=7+7)
ParseDeploy/byte_array-232.7ms ± 1%32.8ms ± 1%~(p=0.128 n=7+7)
ParseDeploy/decode_hex-21.15ms ± 0%1.15ms ± 0%~(p=0.710 n=7+7)
QualifiedIdentifierCreation/One_level-22.66ns ±21%2.43ns ± 4%~(p=0.069 n=7+6)
QualifiedIdentifierCreation/Three_levels-2118ns ± 0%118ns ± 0%~(p=0.434 n=6+7)
NewInterpreter/new_sub-interpreter-21.84µs ± 1%1.84µs ± 0%~(p=0.114 n=7+7)
ContractInterfaceFungibleToken-239.5µs ± 1%39.3µs ± 0%−0.37%(p=0.030 n=6+6)
CheckContractInterfaceFungibleTokenConformance-2126µs ± 0%125µs ± 0%−0.44%(p=0.002 n=7+6)
InterpretRecursionFib-22.48ms ± 0%2.46ms ± 0%−0.91%(p=0.001 n=7+7)
RuntimeResourceDictionaryValues-213.2ms ± 2%13.0ms ± 2%−1.90%(p=0.022 n=6+7)
 
alloc/opdelta
RuntimeResourceDictionaryValues-24.34MB ± 0%4.34MB ± 0%~(p=0.165 n=7+7)
RuntimeStorageWriteCached-283.7kB ± 0%83.7kB ± 0%~(p=1.000 n=7+7)
RuntimeFungibleTokenTransfer-2233kB ± 0%233kB ± 0%~(p=0.977 n=7+7)
QualifiedIdentifierCreation/One_level-20.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-264.0B ± 0%64.0B ± 0%~(all equal)
ContractInterfaceFungibleToken-226.7kB ± 0%26.7kB ± 0%~(all equal)
CheckContractInterfaceFungibleTokenConformance-266.4kB ± 0%66.4kB ± 0%~(all equal)
NewInterpreter/new_interpreter-2680B ± 0%680B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-21.06kB ± 0%1.06kB ± 0%~(all equal)
InterpretRecursionFib-21.21MB ± 0%1.21MB ± 0%~(p=0.462 n=7+7)
 
allocs/opdelta
RuntimeResourceDictionaryValues-2108k ± 0%108k ± 0%~(p=0.087 n=7+7)
RuntimeStorageWriteCached-21.42k ± 0%1.42k ± 0%~(all equal)
RuntimeFungibleTokenTransfer-24.43k ± 0%4.43k ± 0%~(all equal)
QualifiedIdentifierCreation/One_level-20.00 0.00 ~(all equal)
QualifiedIdentifierCreation/Three_levels-22.00 ± 0%2.00 ± 0%~(all equal)
ContractInterfaceFungibleToken-2458 ± 0%458 ± 0%~(all equal)
CheckContractInterfaceFungibleTokenConformance-21.07k ± 0%1.07k ± 0%~(all equal)
NewInterpreter/new_interpreter-211.0 ± 0%11.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-231.0 ± 0%31.0 ± 0%~(all equal)
InterpretRecursionFib-225.0k ± 0%25.0k ± 0%~(all equal)
 

@codecov-commenter
Copy link

codecov-commenter commented Nov 12, 2021

Codecov Report

Merging #1027 (a961aae) into master (c4c1392) will increase coverage by 0.06%.
The diff coverage is 96.92%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1027      +/-   ##
==========================================
+ Coverage   77.53%   77.59%   +0.06%     
==========================================
  Files         274      274              
  Lines       35085    35241     +156     
==========================================
+ Hits        27204    27347     +143     
- Misses       6801     6809       +8     
- Partials     1080     1085       +5     
Flag Coverage Δ
unittests 77.59% <96.92%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
runtime/sema/check_unary_expression.go 88.37% <ø> (-0.27%) ⬇️
runtime/sema/check_variable_declaration.go 93.80% <ø> (-0.90%) ⬇️
runtime/sema/type_tags.go 95.51% <66.66%> (+0.09%) ⬆️
runtime/sema/check_array_expression.go 100.00% <100.00%> (ø)
runtime/sema/check_casting_expression.go 96.57% <100.00%> (+0.01%) ⬆️
runtime/sema/check_conditional.go 90.10% <100.00%> (-2.12%) ⬇️
runtime/sema/check_dictionary_expression.go 100.00% <100.00%> (ø)
runtime/sema/check_for.go 93.33% <100.00%> (+0.60%) ⬆️
runtime/sema/checker.go 89.33% <100.00%> (+0.07%) ⬆️
... and 13 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c4c1392...a961aae. Read the comment docs.

Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice work! 👏

runtime/sema/check_array_expression.go Show resolved Hide resolved
runtime/sema/check_array_expression.go Show resolved Hide resolved
runtime/sema/check_dictionary_expression.go Show resolved Hide resolved
runtime/sema/check_conditional.go Show resolved Hide resolved
runtime/sema/check_dictionary_expression.go Outdated Show resolved Hide resolved
runtime/tests/checker/string_test.go Outdated Show resolved Hide resolved
runtime/tests/checker/type_inference_test.go Show resolved Hide resolved
runtime/tests/interpreter/interpreter_test.go Outdated Show resolved Hide resolved
runtime/tests/interpreter/metatype_test.go Show resolved Hide resolved
runtime/tests/interpreter/string_test.go Outdated Show resolved Hide resolved
@SupunS SupunS force-pushed the supun/common-supertype-usages branch from da9d200 to a961aae Compare November 18, 2021 06:01
Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Language Breaking Change Breaks Cadence contracts deployed on Mainnet
Projects
None yet
3 participants