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

Merge destructor removal into stable cadence #2928

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
8743fae
remove parsing support for destructors
dsainati1 Sep 13, 2023
b6476df
remove typechecking support for destructors
dsainati1 Sep 13, 2023
ac178d1
automatically delete nested resources on destroy
dsainati1 Sep 15, 2023
070e278
remove unused variables
dsainati1 Sep 15, 2023
85eacf5
add support for default arguments to AST
dsainati1 Sep 20, 2023
ef45306
parsing default event
dsainati1 Sep 20, 2023
34f4344
re-add removed test
dsainati1 Sep 20, 2023
d09b578
partial checking
dsainati1 Sep 21, 2023
6846737
support for declarations
dsainati1 Sep 21, 2023
60c1d1e
more progress on checking
dsainati1 Sep 22, 2023
2b1ef90
fix checking order so variables are properly in scope
dsainati1 Sep 22, 2023
26b5ade
check only certain expressions are allowed
dsainati1 Sep 22, 2023
ff166ec
amend error message
dsainati1 Sep 22, 2023
a986a76
permit paths
dsainati1 Sep 22, 2023
b365401
Merge branch 'feature/stable-cadence' of github.com:onflow/cadence in…
dsainati1 Sep 22, 2023
415bef4
Merge branch 'sainati/ban-custom-destructors' of github.com:onflow/ca…
dsainati1 Sep 22, 2023
01f5e1c
update error message on unexpected default arg
dsainati1 Sep 22, 2023
c4786ca
Merge branch 'sainati/default-event-parsing' of github.com:onflow/cad…
dsainati1 Sep 22, 2023
3295bbb
emit default event
dsainati1 Sep 26, 2023
59210c1
emit events on destroy
dsainati1 Sep 26, 2023
b8954d4
fix attachment base values
dsainati1 Sep 26, 2023
e40c490
fix scoping across interpreters
dsainati1 Sep 26, 2023
24e6164
lint
dsainati1 Sep 26, 2023
8d2169f
fix lint
dsainati1 Sep 26, 2023
85c12ac
Merge branch 'sainati/default-event-checking' of github.com:onflow/ca…
dsainati1 Sep 27, 2023
0d9bf8c
emit interface events as well
dsainati1 Sep 27, 2023
339767b
Update runtime/ast/composite.go
dsainati1 Sep 27, 2023
1e83b48
add test for same name interfaces
dsainati1 Sep 27, 2023
6e42dee
Merge branch 'feature/stable-cadence' of github.com:onflow/cadence in…
dsainati1 Sep 27, 2023
a3b43ad
Merge branch 'sainati/ban-custom-destructors' of github.com:onflow/ca…
dsainati1 Sep 27, 2023
9f23589
fix test
dsainati1 Sep 27, 2023
dbf0770
Merge branch 'sainati/default-event-parsing' of github.com:onflow/cad…
dsainati1 Sep 27, 2023
15543eb
respond to review
dsainati1 Sep 28, 2023
f35d5db
Merge branch 'sainati/default-event-parsing' of github.com:onflow/cad…
dsainati1 Sep 28, 2023
936f2ba
correct scoping
dsainati1 Sep 28, 2023
5e92c18
Merge branch 'feature/stable-cadence' of github.com:onflow/cadence in…
dsainati1 Oct 24, 2023
c034b5a
respond to review
dsainati1 Oct 26, 2023
33c238d
Merge branch 'feature/remove-destructors' of github.com:onflow/cadenc…
dsainati1 Oct 26, 2023
bad0e79
Merge branch 'sainati/ban-custom-destructors' of github.com:onflow/ca…
dsainati1 Oct 26, 2023
1910211
Merge branch 'sainati/default-event-parsing' of github.com:onflow/cad…
dsainati1 Oct 26, 2023
8fb0ad2
respond to review
dsainati1 Oct 26, 2023
1d6cc42
Merge branch 'sainati/default-event-checking' of github.com:onflow/ca…
dsainati1 Oct 26, 2023
48fb4b2
prevent manually emitting default destroy events
dsainati1 Oct 27, 2023
8f044b2
respond to review
dsainati1 Oct 27, 2023
3899fdb
add tests
dsainati1 Oct 27, 2023
b1551c3
Merge pull request #2789 from onflow/sainati/ban-custom-destructors
dsainati1 Oct 30, 2023
18e4c0d
respond to review
dsainati1 Oct 30, 2023
1be5186
fix lint
dsainati1 Oct 30, 2023
e63c9e7
Merge pull request #2799 from onflow/sainati/default-event-parsing
dsainati1 Oct 30, 2023
7931d90
respond to review
dsainati1 Oct 30, 2023
1e79d89
Merge pull request #2812 from onflow/sainati/default-event-checking
dsainati1 Oct 30, 2023
3ba46df
respond to review
dsainati1 Oct 31, 2023
ceebf15
Merge branch 'feature/remove-destructors' of github.com:onflow/cadenc…
dsainati1 Oct 31, 2023
366366d
use deterministic ordered function map
dsainati1 Oct 31, 2023
229a189
Update runtime/interpreter/interpreter.go
dsainati1 Nov 1, 2023
e0b9d03
respond to review
dsainati1 Nov 1, 2023
3cd0f5d
add comment
dsainati1 Nov 1, 2023
e5af208
rename variables
dsainati1 Nov 1, 2023
d8abdd4
proper lexical scoping and testing
dsainati1 Nov 1, 2023
920ea94
use new activation for default event param evaluation
dsainati1 Nov 2, 2023
89fbe55
style
dsainati1 Nov 7, 2023
05f6aaf
Merge branch 'feature/stable-cadence' of github.com:onflow/cadence in…
dsainati1 Nov 7, 2023
4045c17
Update runtime/parser/declaration_test.go
dsainati1 Nov 7, 2023
13786b0
review comments
dsainati1 Nov 7, 2023
d1fc770
Merge branch 'sainati/merge-stable-cadence-destructors' of github.com…
dsainati1 Nov 7, 2023
3dd41d5
review comments
dsainati1 Nov 7, 2023
481826f
more argument orders
dsainati1 Nov 7, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions encoding/ccf/ccf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5218,10 +5218,6 @@ func TestEncodeResource(t *testing.T) {
init(bar: @Bar) {
self.bar <- bar
}

destroy() {
destroy self.bar
}
}

fun main(): @Foo {
Expand Down
4 changes: 0 additions & 4 deletions encoding/json/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1264,10 +1264,6 @@ func TestEncodeResource(t *testing.T) {
init(bar: @Bar) {
self.bar <- bar
}

destroy() {
destroy self.bar
}
}

fun main(): @Foo {
Expand Down
11 changes: 11 additions & 0 deletions runtime/ast/composite.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ type CompositeLikeDeclaration interface {
Kind() common.CompositeKind
}

const ResourceDestructionDefaultEventName = "ResourceDestroyed"

func IsResourceDestructionDefaultEvent(identifier string) bool {
return identifier == ResourceDestructionDefaultEventName
}

type CompositeDeclaration struct {
Members *Members
DocString string
Expand Down Expand Up @@ -280,6 +286,11 @@ func (d *CompositeDeclaration) ConformanceList() []*NominalType {
return d.Conformances
}

func (d *CompositeDeclaration) IsResourceDestructionDefaultEvent() bool {
return d.CompositeKind == common.CompositeKindEvent &&
IsResourceDestructionDefaultEvent(d.Identifier.Identifier)
}

// FieldDeclarationFlags

type FieldDeclarationFlags uint8
Expand Down
1 change: 1 addition & 0 deletions runtime/ast/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4414,6 +4414,7 @@ func TestFunctionExpression_MarshalJSON(t *testing.T) {
"StartPos": {"Offset": 7, "Line": 8, "Column": 9},
"EndPos": {"Offset": 5, "Line": 5, "Column": 7}
},
"DefaultArgument": null,
"StartPos": {"Offset": 10, "Line": 11, "Column": 12},
"EndPos": {"Offset": 5, "Line": 5, "Column": 7}
}
Expand Down
2 changes: 2 additions & 0 deletions runtime/ast/function_declaration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ func TestFunctionDeclaration_MarshalJSON(t *testing.T) {
"StartPos": {"Offset": 7, "Line": 8, "Column": 9},
"EndPos": {"Offset": 5, "Line": 5, "Column": 7}
},
"DefaultArgument": null,
"StartPos": {"Offset": 10, "Line": 11, "Column": 12},
"EndPos": {"Offset": 5, "Line": 5, "Column": 7}
}
Expand Down Expand Up @@ -715,6 +716,7 @@ func TestSpecialFunctionDeclaration_MarshalJSON(t *testing.T) {
"StartPos": {"Offset": 7, "Line": 8, "Column": 9},
"EndPos": {"Offset": 5, "Line": 5, "Column": 7}
},
"DefaultArgument": null,
"StartPos": {"Offset": 10, "Line": 11, "Column": 12},
"EndPos": {"Offset": 5, "Line": 5, "Column": 7}
}
Expand Down
12 changes: 0 additions & 12 deletions runtime/ast/memberindices.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ type memberIndices struct {
_specialFunctions []*SpecialFunctionDeclaration
// Use `Initializers()` instead
_initializers []*SpecialFunctionDeclaration
// Semantically only one destructor is allowed,
// but the program might illegally declare multiple.
// Use `Destructors()` instead
_destructors []*SpecialFunctionDeclaration
// Use `Functions()`
_functions []*FunctionDeclaration
// Use `FunctionsByIdentifier()` instead
Expand Down Expand Up @@ -109,11 +105,6 @@ func (i *memberIndices) Initializers(declarations []Declaration) []*SpecialFunct
return i._initializers
}

func (i *memberIndices) Destructors(declarations []Declaration) []*SpecialFunctionDeclaration {
i.once.Do(i.initializer(declarations))
return i._destructors
}

func (i *memberIndices) Fields(declarations []Declaration) []*FieldDeclaration {
i.once.Do(i.initializer(declarations))
return i._fields
Expand Down Expand Up @@ -175,7 +166,6 @@ func (i *memberIndices) init(declarations []Declaration) {
i._functionsByIdentifier = make(map[string]*FunctionDeclaration)

i._specialFunctions = make([]*SpecialFunctionDeclaration, 0)
i._destructors = make([]*SpecialFunctionDeclaration, 0)
i._initializers = make([]*SpecialFunctionDeclaration, 0)

i._composites = make([]*CompositeDeclaration, 0)
Expand Down Expand Up @@ -211,8 +201,6 @@ func (i *memberIndices) init(declarations []Declaration) {
switch declaration.Kind {
case common.DeclarationKindInitializer:
i._initializers = append(i._initializers, declaration)
case common.DeclarationKindDestructor:
i._destructors = append(i._destructors, declaration)
}

case *EntitlementDeclaration:
Expand Down
7 changes: 1 addition & 6 deletions runtime/ast/memberindices_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ func TestMemberIndices(t *testing.T) {
specialFunctionA := &SpecialFunctionDeclaration{
Kind: common.DeclarationKindInitializer,
}
specialFunctionB := &SpecialFunctionDeclaration{
Kind: common.DeclarationKindDestructor,
}
specialFunctionC := &SpecialFunctionDeclaration{}
specialFunctionB := &SpecialFunctionDeclaration{}

compositeA := &CompositeDeclaration{
Identifier: Identifier{Identifier: "A"},
Expand Down Expand Up @@ -98,7 +95,6 @@ func TestMemberIndices(t *testing.T) {
interfaceB,
compositeA,
functionB,
specialFunctionC,
compositeB,
specialFunctionA,
interfaceA,
Expand Down Expand Up @@ -141,7 +137,6 @@ func TestMemberIndices(t *testing.T) {
require.Equal(t,
[]*SpecialFunctionDeclaration{
specialFunctionB,
specialFunctionC,
specialFunctionA,
},
members.SpecialFunctions(),
Expand Down
13 changes: 0 additions & 13 deletions runtime/ast/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,19 +116,6 @@ func (m *Members) Initializers() []*SpecialFunctionDeclaration {
return m.indices.Initializers(m.declarations)
}

func (m *Members) Destructors() []*SpecialFunctionDeclaration {
return m.indices.Destructors(m.declarations)
}

// Destructor returns the first destructor, if any
func (m *Members) Destructor() *SpecialFunctionDeclaration {
destructors := m.Destructors()
if len(destructors) == 0 {
return nil
}
return destructors[0]
}

func (m *Members) FieldPosition(name string, compositeKind common.CompositeKind) Position {
if compositeKind == common.CompositeKindEvent {
parameters := m.Initializers()[0].FunctionDeclaration.ParameterList.ParametersByIdentifier()
Expand Down
60 changes: 52 additions & 8 deletions runtime/ast/parameter.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,34 @@
import (
"encoding/json"

"github.com/turbolent/prettier"

"github.com/onflow/cadence/runtime/common"
)

type Parameter struct {
TypeAnnotation *TypeAnnotation
Label string
Identifier Identifier
StartPos Position `json:"-"`
TypeAnnotation *TypeAnnotation
DefaultArgument Expression
Label string
Identifier Identifier
StartPos Position `json:"-"`
}

func NewParameter(
gauge common.MemoryGauge,
label string,
identifier Identifier,
typeAnnotation *TypeAnnotation,
defaultArgument Expression,
startPos Position,
) *Parameter {
common.UseMemory(gauge, common.ParameterMemoryUsage)
return &Parameter{
Label: label,
Identifier: identifier,
TypeAnnotation: typeAnnotation,
StartPos: startPos,
Label: label,
Identifier: identifier,
TypeAnnotation: typeAnnotation,
DefaultArgument: defaultArgument,
StartPos: startPos,
}
}

Expand All @@ -65,9 +70,16 @@
}

func (p *Parameter) EndPosition(memoryGauge common.MemoryGauge) Position {
if p.HasDefaultArgument() {
return p.DefaultArgument.EndPosition(memoryGauge)
}
return p.TypeAnnotation.EndPosition(memoryGauge)
}

func (p *Parameter) HasDefaultArgument() bool {
return p.DefaultArgument != nil
}

func (p *Parameter) MarshalJSON() ([]byte, error) {
type Alias Parameter
return json.Marshal(&struct {
Expand All @@ -78,3 +90,35 @@
Alias: (*Alias)(p),
})
}

const parameterDefaultArgumentSeparator = "="

func (p *Parameter) Doc() prettier.Doc {
var parameterDoc prettier.Concat

if p.Label != "" {
parameterDoc = append(
parameterDoc,
prettier.Text(p.Label),
prettier.Space,
)
}

parameterDoc = append(
parameterDoc,
prettier.Text(p.Identifier.Identifier),
typeSeparatorSpaceDoc,
p.TypeAnnotation.Doc(),
)

if p.DefaultArgument != nil {
parameterDoc = append(parameterDoc,
prettier.Space,
prettier.Text(parameterDefaultArgumentSeparator),
prettier.Space,
p.DefaultArgument.Doc(),
)
}

Check warning on line 121 in runtime/ast/parameter.go

View check run for this annotation

Codecov / codecov/patch

runtime/ast/parameter.go#L115-L121

Added lines #L115 - L121 were not covered by tests

return parameterDoc
}
19 changes: 1 addition & 18 deletions runtime/ast/parameterlist.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,7 @@ func (l *ParameterList) Doc() prettier.Doc {
parameterDocs := make([]prettier.Doc, 0, len(l.Parameters))

for _, parameter := range l.Parameters {
var parameterDoc prettier.Concat

if parameter.Label != "" {
parameterDoc = append(
parameterDoc,
prettier.Text(parameter.Label),
prettier.Space,
)
}

parameterDoc = append(
parameterDoc,
prettier.Text(parameter.Identifier.Identifier),
typeSeparatorSpaceDoc,
parameter.TypeAnnotation.Doc(),
)

parameterDocs = append(parameterDocs, parameterDoc)
parameterDocs = append(parameterDocs, parameter.Doc())
}

return prettier.WrapParentheses(
Expand Down
5 changes: 0 additions & 5 deletions runtime/common/declarationkind.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const (
DeclarationKindEvent
DeclarationKindField
DeclarationKindInitializer
DeclarationKindDestructor
DeclarationKindStructureInterface
DeclarationKindResourceInterface
DeclarationKindContractInterface
Expand Down Expand Up @@ -117,8 +116,6 @@ func (k DeclarationKind) Name() string {
return "field"
case DeclarationKindInitializer:
return "initializer"
case DeclarationKindDestructor:
return "destructor"
case DeclarationKindAttachment:
return "attachment"
case DeclarationKindStructureInterface:
Expand Down Expand Up @@ -176,8 +173,6 @@ func (k DeclarationKind) Keywords() string {
return "event"
case DeclarationKindInitializer:
return "init"
case DeclarationKindDestructor:
return "destroy"
case DeclarationKindAttachment:
return "attachment"
case DeclarationKindStructureInterface:
Expand Down
37 changes: 18 additions & 19 deletions runtime/common/declarationkind_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions runtime/convertValues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1687,10 +1687,6 @@ func TestRuntimeExportNestedResourceValueFromScript(t *testing.T) {
init(bar: @Bar) {
self.bar <- bar
}

destroy() {
destroy self.bar
}
}

access(all) fun main(): @Foo {
Expand Down
2 changes: 1 addition & 1 deletion runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ func (e *interpreterEnvironment) newCompositeValueFunctionsHandler() interpreter
inter *interpreter.Interpreter,
locationRange interpreter.LocationRange,
compositeValue *interpreter.CompositeValue,
) map[string]interpreter.FunctionValue {
) *interpreter.FunctionOrderedMap {

handler := e.compositeValueFunctionsHandlers[compositeValue.TypeID()]
if handler == nil {
Expand Down
4 changes: 0 additions & 4 deletions runtime/ft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,6 @@ access(all) contract FlowToken: FungibleToken {
vault.balance = 0.0
destroy vault
}

destroy() {
FlowToken.totalSupply = FlowToken.totalSupply - self.balance
}
}

// createEmptyVault
Expand Down
Loading
Loading