-
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
Forbid reinitialization of variable, resource-kinded fields #1527
Conversation
Cadence Benchstat comparisonThis branch with compared with the base branch onflow:master commit 668062c Results
|
// 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. |
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 might be clearer to use more well-named variables than to have a large comment. You started this with targetIsConstant
.
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.
What is your concrete suggestion? Do you have an example?
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.
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 {
...
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.
Thanks for fixing!
Description
In addition to constant fields, also variable resource-kinded fields may not be re-initialized.
master
branchFiles changed
in the Github PR explorer