-
Notifications
You must be signed in to change notification settings - Fork 137
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
Add support for resource-kinded fields in transaction roles #2262
Merged
turbolent
merged 13 commits into
feature/extended-transaction-format
from
bastian/extended-transaction-format-4
Jan 31, 2023
Merged
Changes from 10 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
7abf054
simplify resource invalidation recording
turbolent 48247ab
refactor resource field invalidation check to function
turbolent 4cea08d
add support for resource-kinded fields in transaction roles: allow/re…
turbolent 2d73b12
improve identifier naming
turbolent c566da8
check role resource field after invalidation
turbolent 8a95517
improve field alignment
turbolent 1a90380
improve message of invalid nested resource move error: include identi…
turbolent 7fb3e0a
refactor tests
turbolent a91d406
test nested resource invalidation for transaction role resource-kinde…
turbolent 9fa982e
move checker config setup out of checking function
turbolent 21ee569
Merge branch 'feature/extended-transaction-format' into bastian/exten…
turbolent 27de6f2
fix merge
turbolent 7d32929
roles are not stored in transaction type anymore, iterate over members
turbolent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,28 +91,28 @@ type ContractValueHandlerFunc func( | |
|
||
type Checker struct { | ||
// memoryGauge is used for metering memory usage | ||
memoryGauge common.MemoryGauge | ||
Location common.Location | ||
expectedType Type | ||
resources *Resources | ||
valueActivations *VariableActivations | ||
currentMemberExpression *ast.MemberExpression | ||
typeActivations *VariableActivations | ||
containerTypes map[Type]struct{} | ||
Program *ast.Program | ||
PositionInfo *PositionInfo | ||
Config *Config | ||
Elaboration *Elaboration | ||
memoryGauge common.MemoryGauge | ||
Location common.Location | ||
expectedType Type | ||
Config *Config | ||
// initialized lazily. use beforeExtractor() | ||
_beforeExtractor *BeforeExtractor | ||
errors []error | ||
functionActivations *FunctionActivations | ||
inCondition bool | ||
allowSelfResourceFieldInvalidation bool | ||
inAssignment bool | ||
inInvocation bool | ||
inCreate bool | ||
isChecked bool | ||
_beforeExtractor *BeforeExtractor | ||
currentMemberExpression *ast.MemberExpression | ||
typeActivations *VariableActivations | ||
containerTypes map[Type]struct{} | ||
Program *ast.Program | ||
PositionInfo *PositionInfo | ||
resources *Resources | ||
Elaboration *Elaboration | ||
valueActivations *VariableActivations | ||
resourceFieldInvalidationAllowed func(*ast.MemberExpression) *Member | ||
functionActivations *FunctionActivations | ||
errors []error | ||
inCondition bool | ||
inAssignment bool | ||
inInvocation bool | ||
inCreate bool | ||
isChecked bool | ||
} | ||
|
||
var _ ast.DeclarationVisitor[struct{}] = &Checker{} | ||
|
@@ -1355,9 +1355,10 @@ func (checker *Checker) recordResourceInvalidation( | |
return nil | ||
} | ||
|
||
reportInvalidNestedMove := func() { | ||
reportInvalidNestedMove := func(identifier *ast.Identifier) { | ||
checker.report( | ||
&InvalidNestedResourceMoveError{ | ||
Identifier: identifier, | ||
Range: ast.NewRangeFromPositioned( | ||
checker.memoryGauge, | ||
expression, | ||
|
@@ -1366,32 +1367,13 @@ func (checker *Checker) recordResourceInvalidation( | |
) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
accessedSelfMember := checker.accessedSelfMember(expression) | ||
|
||
switch expression.(type) { | ||
case *ast.MemberExpression: | ||
|
||
if accessedSelfMember == nil || | ||
!checker.allowSelfResourceFieldInvalidation { | ||
|
||
reportInvalidNestedMove() | ||
return nil | ||
getRecordedResourceInvalidation := func(res Resource) *recordedResourceInvalidation { | ||
invalidation := ResourceInvalidation{ | ||
Kind: invalidationKind, | ||
StartPos: expression.StartPosition(), | ||
EndPos: expression.EndPosition(checker.memoryGauge), | ||
} | ||
|
||
case *ast.IndexExpression: | ||
reportInvalidNestedMove() | ||
return nil | ||
} | ||
|
||
invalidation := ResourceInvalidation{ | ||
Kind: invalidationKind, | ||
StartPos: expression.StartPosition(), | ||
EndPos: expression.EndPosition(checker.memoryGauge), | ||
} | ||
|
||
if checker.allowSelfResourceFieldInvalidation && accessedSelfMember != nil { | ||
res := Resource{Member: accessedSelfMember} | ||
|
||
checker.maybeAddResourceInvalidation(res, invalidation) | ||
|
||
return &recordedResourceInvalidation{ | ||
|
@@ -1400,37 +1382,51 @@ func (checker *Checker) recordResourceInvalidation( | |
} | ||
} | ||
|
||
identifierExpression, ok := expression.(*ast.IdentifierExpression) | ||
if !ok { | ||
return nil | ||
} | ||
switch expression := expression.(type) { | ||
case *ast.IdentifierExpression: | ||
variable := checker.findAndCheckValueVariable(expression, false) | ||
if variable == nil { | ||
return nil | ||
} | ||
|
||
variable := checker.findAndCheckValueVariable(identifierExpression, false) | ||
if variable == nil { | ||
return nil | ||
} | ||
if invalidationKind != ResourceInvalidationKindMoveTemporary && | ||
variable.DeclarationKind == common.DeclarationKindSelf { | ||
|
||
if invalidationKind != ResourceInvalidationKindMoveTemporary && | ||
variable.DeclarationKind == common.DeclarationKindSelf { | ||
checker.report( | ||
&InvalidSelfInvalidationError{ | ||
InvalidationKind: invalidationKind, | ||
Range: ast.NewRangeFromPositioned( | ||
checker.memoryGauge, | ||
expression, | ||
), | ||
}, | ||
) | ||
} | ||
|
||
checker.report( | ||
&InvalidSelfInvalidationError{ | ||
InvalidationKind: invalidationKind, | ||
Range: ast.NewRangeFromPositioned( | ||
checker.memoryGauge, | ||
expression, | ||
), | ||
}, | ||
) | ||
} | ||
return getRecordedResourceInvalidation(Resource{ | ||
Variable: variable, | ||
}) | ||
|
||
case *ast.MemberExpression: | ||
resourceFieldInvalidationAllowed := checker.resourceFieldInvalidationAllowed | ||
if resourceFieldInvalidationAllowed != nil { | ||
member := resourceFieldInvalidationAllowed(expression) | ||
if member != nil { | ||
return getRecordedResourceInvalidation(Resource{ | ||
Member: member, | ||
}) | ||
} | ||
} | ||
|
||
res := Resource{Variable: variable} | ||
reportInvalidNestedMove(&expression.Identifier) | ||
return nil | ||
|
||
checker.maybeAddResourceInvalidation(res, invalidation) | ||
case *ast.IndexExpression: | ||
reportInvalidNestedMove(nil) | ||
return nil | ||
|
||
return &recordedResourceInvalidation{ | ||
resource: res, | ||
invalidation: invalidation, | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
|
@@ -1798,11 +1794,14 @@ func (checker *Checker) isWriteableAccess(access ast.Access) bool { | |
} | ||
} | ||
|
||
func (checker *Checker) withSelfResourceInvalidationAllowed(f func()) { | ||
allowSelfResourceFieldInvalidation := checker.allowSelfResourceFieldInvalidation | ||
checker.allowSelfResourceFieldInvalidation = true | ||
func (checker *Checker) withResourceFieldInvalidationAllowed( | ||
invalidationAllowed func(*ast.MemberExpression) *Member, | ||
f func(), | ||
) { | ||
oldInvalidationAllowed := checker.resourceFieldInvalidationAllowed | ||
checker.resourceFieldInvalidationAllowed = invalidationAllowed | ||
defer func() { | ||
checker.allowSelfResourceFieldInvalidation = allowSelfResourceFieldInvalidation | ||
checker.resourceFieldInvalidationAllowed = oldInvalidationAllowed | ||
}() | ||
|
||
f() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The default, like in https://github.com/onflow/cadence/pull/2262/files#diff-c34bee2125d8f3673160cc7b3c912540ad811b4622a66c3a2ed6c25fc94e1cb0R176 (jump is broken, need to scroll up a bit)