Skip to content

Commit

Permalink
Merge pull request #2928 from onflow/sainati/merge-stable-cadence-des…
Browse files Browse the repository at this point in the history
…tructors

Merge destructor removal into stable cadence
  • Loading branch information
dsainati1 authored Nov 7, 2023
2 parents 2ac7503 + 481826f commit 7e955a3
Show file tree
Hide file tree
Showing 58 changed files with 3,330 additions and 3,732 deletions.
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 @@ package ast
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) StartPosition() Position {
}

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 @@ func (p *Parameter) MarshalJSON() ([]byte, error) {
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(),
)
}

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

0 comments on commit 7e955a3

Please sign in to comment.