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

Improve reference expressions #1661

Merged
merged 6 commits into from Jun 30, 2022
Merged

Improve reference expressions #1661

merged 6 commits into from Jun 30, 2022

Conversation

turbolent
Copy link
Member

@turbolent turbolent commented May 18, 2022

Description

This PR implements the FLIP proposing to improve reference expressions:

  • Remove the type from the syntax: reference expressions are now just &v. This turns existing cases of &v as &T into a casting expression, a static type annotation
  • Remove type inference for reference expressions. This enforces a contextual type to be provided, e.g. through a static type annotation with as, a variable declaration type annotation

These changes essentially make references behave exactly like empty container literals (empty array [] and empty dictionary {}), unifying the concepts and allowing the intuition that developers gained there to be applied for references, too.

In addition it keeps all current code working as before.


  • 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

@turbolent turbolent self-assigned this May 18, 2022
@codecov
Copy link

codecov bot commented May 18, 2022

Codecov Report

Merging #1661 (cee1fc4) into master (5a06fec) will increase coverage by 0.00%.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1661   +/-   ##
=======================================
  Coverage   77.43%   77.43%           
=======================================
  Files         305      305           
  Lines       62552    62546    -6     
=======================================
- Hits        48437    48434    -3     
+ Misses      12494    12492    -2     
+ Partials     1621     1620    -1     
Flag Coverage Δ
unittests 77.43% <100.00%> (+<0.01%) ⬆️

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

Impacted Files Coverage Δ
runtime/ast/expression.go 88.85% <100.00%> (+0.02%) ⬆️
runtime/parser/expression.go 93.31% <100.00%> (+0.12%) ⬆️
runtime/sema/check_array_expression.go 100.00% <100.00%> (ø)
runtime/sema/check_dictionary_expression.go 100.00% <100.00%> (ø)
runtime/sema/check_reference_expression.go 100.00% <100.00%> (ø)

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 5a06fec...cee1fc4. Read the comment docs.

@github-actions
Copy link

github-actions bot commented May 18, 2022

Cadence Benchstat comparison

This branch with compared with the base branch onflow:master commit 5a06fec
The command for i in {1..N}; do go test ./... -run=XXX -bench=. -benchmem -shuffle=on; done was used.
Bench tests were run a total of 7 times on each branch.

Results

old.txtnew.txt
time/opdelta
CheckContractInterfaceFungibleTokenConformance-2164µs ± 2%162µs ± 4%~(p=0.456 n=7+7)
ContractInterfaceFungibleToken-249.1µs ± 3%48.5µs ± 0%~(p=0.530 n=7+5)
InterpretRecursionFib-24.16ms ± 2%3.98ms ± 3%−4.33%(p=0.002 n=6+7)
NewInterpreter/new_interpreter-21.39µs ± 7%1.33µs ± 3%~(p=0.079 n=7+7)
NewInterpreter/new_sub-interpreter-22.80µs ± 3%2.78µs ± 2%~(p=0.383 n=7+7)
ParseArray-210.2ms ± 3%9.9ms ± 2%−2.87%(p=0.011 n=7+7)
ParseDeploy/byte_array-215.7ms ±10%15.4ms ± 6%~(p=0.535 n=7+7)
ParseDeploy/decode_hex-21.33ms ± 2%1.30ms ± 4%~(p=0.053 n=7+7)
ParseFungibleToken/With_memory_metering-2249µs ± 3%240µs ± 5%~(p=0.053 n=7+7)
ParseFungibleToken/Without_memory_metering-2189µs ± 4%183µs ± 6%~(p=0.209 n=7+7)
ParseInfix-28.93µs ± 3%8.83µs ± 5%~(p=0.535 n=7+7)
QualifiedIdentifierCreation/One_level-22.71ns ± 2%3.11ns ± 1%+14.49%(p=0.002 n=6+6)
QualifiedIdentifierCreation/Three_levels-2168ns ± 5%161ns ± 4%−3.96%(p=0.011 n=7+7)
RuntimeFungibleTokenTransfer-21.47ms ±20%1.31ms ±23%~(p=0.128 n=7+7)
RuntimeResourceDictionaryValues-27.31ms ± 2%7.35ms ± 1%~(p=0.295 n=7+6)
ValueIsSubtypeOfSemaType-2105ns ± 2%105ns ± 6%~(p=0.314 n=6+7)
 
alloc/opdelta
CheckContractInterfaceFungibleTokenConformance-267.0kB ± 0%67.0kB ± 0%−0.00%(p=0.042 n=7+6)
ContractInterfaceFungibleToken-226.8kB ± 0%26.8kB ± 0%~(all equal)
InterpretRecursionFib-21.51MB ± 0%1.51MB ± 0%−0.00%(p=0.008 n=7+5)
NewInterpreter/new_interpreter-2928B ± 0%928B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-21.46kB ± 0%1.46kB ± 0%~(all equal)
ParseArray-22.79MB ± 2%2.80MB ± 2%~(p=0.156 n=7+7)
ParseDeploy/byte_array-24.43MB ± 3%4.57MB ± 0%+3.07%(p=0.007 n=7+7)
ParseDeploy/decode_hex-2214kB ± 0%214kB ± 0%~(p=0.946 n=7+7)
ParseFungibleToken/With_memory_metering-236.3kB ± 0%36.3kB ± 0%~(p=0.796 n=7+7)
ParseFungibleToken/Without_memory_metering-236.3kB ± 0%36.3kB ± 0%~(p=0.851 n=7+7)
ParseInfix-22.17kB ± 0%2.17kB ± 0%~(p=0.188 n=7+7)
QualifiedIdentifierCreation/One_level-20.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-264.0B ± 0%64.0B ± 0%~(all equal)
RuntimeFungibleTokenTransfer-2232kB ± 1%232kB ± 0%~(p=0.628 n=7+6)
RuntimeResourceDictionaryValues-22.25MB ± 0%2.25MB ± 0%~(p=0.710 n=7+7)
ValueIsSubtypeOfSemaType-248.0B ± 0%48.0B ± 0%~(all equal)
 
allocs/opdelta
CheckContractInterfaceFungibleTokenConformance-21.08k ± 0%1.08k ± 0%~(all equal)
ContractInterfaceFungibleToken-2461 ± 0%461 ± 0%~(all equal)
InterpretRecursionFib-233.5k ± 0%33.5k ± 0%~(all equal)
NewInterpreter/new_interpreter-213.0 ± 0%13.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-242.0 ± 0%42.0 ± 0%~(all equal)
ParseArray-270.0k ± 0%70.0k ± 0%~(p=0.385 n=6+7)
ParseDeploy/byte_array-2105k ± 0%105k ± 0%~(p=0.538 n=6+7)
ParseDeploy/decode_hex-277.0 ± 0%77.0 ± 0%~(all equal)
ParseFungibleToken/With_memory_metering-21.06k ± 0%1.06k ± 0%~(all equal)
ParseFungibleToken/Without_memory_metering-21.06k ± 0%1.06k ± 0%~(all equal)
ParseInfix-266.0 ± 0%66.0 ± 0%~(all equal)
QualifiedIdentifierCreation/One_level-20.00 0.00 ~(all equal)
QualifiedIdentifierCreation/Three_levels-22.00 ± 0%2.00 ± 0%~(all equal)
RuntimeFungibleTokenTransfer-24.19k ± 0%4.19k ± 0%~(p=0.242 n=7+7)
RuntimeResourceDictionaryValues-237.5k ± 0%37.5k ± 0%~(p=0.417 n=7+7)
ValueIsSubtypeOfSemaType-21.00 ± 0%1.00 ± 0%~(all equal)
 

Comment on lines 6445 to 6446
require.IsType(t, &sema.UnnecessaryCastHint{}, hints[0])
castHint := hints[0].(*sema.UnnecessaryCastHint)
Copy link
Member

Choose a reason for hiding this comment

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

👌

Copy link
Member

@SupunS SupunS left a comment

Choose a reason for hiding this comment

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

We also need to update the docs

Copy link
Contributor

@dsainati1 dsainati1 left a comment

Choose a reason for hiding this comment

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

Awesome!

@turbolent
Copy link
Member Author

Turns out, this also fixes a bug in the parser: https://github.com/onflow/cadence/pull/1661/files#diff-62e34e916aed6bc25eed273b0edbb3c0fc078de38bec85ed1fb4d034fc65b6cdR2428-R2429

Kudos to themasterpiece.art for reporting this bug.

@turbolent
Copy link
Member Author

Merging, now that onflow/flow#941 has been accepted

@turbolent turbolent added the Language Breaking Change Breaks Cadence contracts deployed on Mainnet label Jun 30, 2022
@turbolent turbolent merged commit 7f8cd8a into master Jun 30, 2022
@turbolent turbolent deleted the bastian/improve-references branch June 30, 2022 21:32
@SupunS SupunS mentioned this pull request Jul 8, 2022
6 tasks
@turbolent turbolent mentioned this pull request Sep 27, 2022
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bugfix Improvement Language Breaking Change Breaks Cadence contracts deployed on Mainnet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants