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

Forbid reinitialization of variable, resource-kinded fields #1527

Merged
merged 1 commit into from
Mar 23, 2022

Conversation

turbolent
Copy link
Member

Description

In addition to constant fields, also variable resource-kinded fields may not be re-initialized.


  • 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

@github-actions
Copy link

Cadence Benchstat comparison

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

Results

old.txtnew.txt
time/opdelta
RuntimeFungibleTokenTransfer-21.58ms ±25%1.92ms ± 2%+21.49%(p=0.035 n=7+6)
ParseArray-216.8ms ± 1%16.6ms ± 4%~(p=0.101 n=6+7)
ParseFungibleToken-2257µs ± 3%258µs ± 5%~(p=0.805 n=7+7)
ParseInfix-211.0µs ± 2%11.0µs ± 3%~(p=0.929 n=7+7)
ParseDeploy/byte_array-225.5ms ± 2%25.6ms ± 2%~(p=0.620 n=7+7)
ParseDeploy/decode_hex-21.58ms ± 2%1.60ms ± 6%~(p=0.318 n=7+7)
QualifiedIdentifierCreation/One_level-23.74ns ± 4%3.71ns ± 2%~(p=0.364 n=7+7)
QualifiedIdentifierCreation/Three_levels-2191ns ± 2%188ns ± 3%~(p=0.128 n=7+7)
ContractInterfaceFungibleToken-253.6µs ± 2%53.5µs ± 3%~(p=0.710 n=7+7)
NewInterpreter/new_interpreter-21.50µs ± 1%1.50µs ± 5%~(p=1.000 n=7+7)
NewInterpreter/new_sub-interpreter-22.92µs ± 2%2.91µs ± 4%~(p=0.423 n=7+6)
InterpretRecursionFib-23.63ms ± 2%3.57ms ± 2%~(p=0.073 n=6+7)
CheckContractInterfaceFungibleTokenConformance-2192µs ± 2%189µs ± 1%−1.91%(p=0.008 n=7+6)
RuntimeResourceDictionaryValues-218.8ms ± 1%18.1ms ± 2%−3.59%(p=0.002 n=6+6)
 
alloc/opdelta
RuntimeResourceDictionaryValues-24.05MB ± 0%4.05MB ± 0%~(p=0.318 n=7+7)
RuntimeFungibleTokenTransfer-2273kB ± 0%273kB ± 0%~(p=0.620 n=7+7)
QualifiedIdentifierCreation/One_level-20.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-264.0B ± 0%64.0B ± 0%~(all equal)
CheckContractInterfaceFungibleTokenConformance-266.2kB ± 0%66.2kB ± 0%~(all equal)
ContractInterfaceFungibleToken-226.6kB ± 0%26.6kB ± 0%~(p=0.592 n=7+7)
NewInterpreter/new_interpreter-2848B ± 0%848B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-21.34kB ± 0%1.34kB ± 0%~(all equal)
InterpretRecursionFib-21.26MB ± 0%1.26MB ± 0%~(p=0.497 n=7+7)
 
allocs/opdelta
RuntimeResourceDictionaryValues-2102k ± 0%102k ± 0%~(p=0.194 n=7+7)
RuntimeFungibleTokenTransfer-24.54k ± 0%4.53k ± 0%~(p=0.149 n=7+6)
QualifiedIdentifierCreation/One_level-20.00 0.00 ~(all equal)
QualifiedIdentifierCreation/Three_levels-22.00 ± 0%2.00 ± 0%~(all equal)
CheckContractInterfaceFungibleTokenConformance-21.07k ± 0%1.07k ± 0%~(all equal)
ContractInterfaceFungibleToken-2458 ± 0%458 ± 0%~(all equal)
NewInterpreter/new_interpreter-213.0 ± 0%13.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-240.0 ± 0%40.0 ± 0%~(all equal)
InterpretRecursionFib-226.2k ± 0%26.2k ± 0%~(all equal)
 

Comment on lines +299 to +305
// If the field is constant,
// or it is variable and resource-kinded,
// and it has already previously been initialized,
// report an error for the repeated assignment / initialization
//
// Assigning to a variable, resource-kinded field is invalid,
// because the initial value would get lost.
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be clearer to use more well-named variables than to have a large comment. You started this with targetIsConstant.

Copy link
Member Author

@turbolent turbolent Mar 23, 2022

Choose a reason for hiding this comment

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

What is your concrete suggestion? Do you have an example?

Copy link
Contributor

Choose a reason for hiding this comment

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

On second thought, it wouldn't be good to run initializedFieldMembers.Contains(accessedSelfMember) if unnecessary. It could be put into a function but that's probably less clear overall.

Anyway, here's what I was thinking:

...

targetIsConstant := member.VariableKind == ast.VariableKindConstant
targetIsResource := member.TypeAnnotation.Type.IsResourceType()
targetIsInitialized := functionActivation.InitializationInfo.InitializedFieldMembers.Contains(accessedSelfMember)

...

// Report error for repeated assignment/initialization of constants or resource variables.
// Cannot assign to resource-kinded fields because the initial value would be lost.

if (targetIsConstant || targetIsResource) && targetIsInitialized {

...

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.

Thanks for fixing!

@turbolent turbolent merged commit 6d5d688 into master Mar 23, 2022
@turbolent turbolent deleted the bastian/fix-resource-field-initialization branch March 23, 2022 22:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants