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

Remove support for custom destructors #2789

Merged
merged 9 commits into from
Oct 30, 2023
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
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
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 @@ -1688,10 +1688,6 @@ func TestRuntimeExportNestedResourceValueFromScript(t *testing.T) {
init(bar: @Bar) {
self.bar <- bar
}

destroy() {
destroy self.bar
}
}

access(all) fun main(): @Foo {
Expand Down
4 changes: 0 additions & 4 deletions runtime/ft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,6 @@ access(all) contract FlowToken: FungibleToken {
vault.balance = 0.0
destroy vault
}

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

// createEmptyVault
Expand Down
81 changes: 0 additions & 81 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ type CompositeTypeHandlerFunc func(location common.Location, typeID TypeID) *sem
// these are the "leaf" nodes in the call chain, and are functions.
type CompositeTypeCode struct {
CompositeFunctions map[string]FunctionValue
DestructorFunction FunctionValue
}

type FunctionWrapper = func(inner FunctionValue) FunctionValue
Expand All @@ -188,7 +187,6 @@ type FunctionWrapper = func(inner FunctionValue) FunctionValue
// i.e. they wrap the functions / function wrappers that inherit them.
type WrapperCode struct {
InitializerFunctionWrapper FunctionWrapper
DestructorFunctionWrapper FunctionWrapper
FunctionWrappers map[string]FunctionWrapper
Functions map[string]FunctionValue
}
Expand Down Expand Up @@ -1130,12 +1128,6 @@ func (interpreter *Interpreter) declareNonEnumCompositeValue(
}
}

var destructorFunction FunctionValue
compositeDestructorFunction := interpreter.compositeDestructorFunction(declaration, lexicalScope)
if compositeDestructorFunction != nil {
destructorFunction = compositeDestructorFunction
}

functions := interpreter.compositeFunctions(declaration, lexicalScope)

wrapFunctions := func(code WrapperCode) {
Expand All @@ -1149,15 +1141,6 @@ func (interpreter *Interpreter) declareNonEnumCompositeValue(
initializerFunction = initializerFunctionWrapper(initializerFunction)
}

// Wrap destructor

destructorFunctionWrapper :=
code.DestructorFunctionWrapper

if destructorFunctionWrapper != nil {
destructorFunction = destructorFunctionWrapper(destructorFunction)
}

// Apply default functions, if conforming type does not provide the function

// Iterating over the map in a non-deterministic way is OK,
Expand Down Expand Up @@ -1192,7 +1175,6 @@ func (interpreter *Interpreter) declareNonEnumCompositeValue(
}

interpreter.SharedState.typeCodes.CompositeCodes[compositeType.ID()] = CompositeTypeCode{
DestructorFunction: destructorFunction,
CompositeFunctions: functions,
}

Expand Down Expand Up @@ -1280,7 +1262,6 @@ func (interpreter *Interpreter) declareNonEnumCompositeValue(

value.InjectedFields = injectedFields
value.Functions = functions
value.Destructor = destructorFunction

var self MemberAccessibleValue = value
if declaration.Kind() == common.CompositeKindAttachment {
Expand Down Expand Up @@ -1536,49 +1517,6 @@ func (interpreter *Interpreter) compositeInitializerFunction(
)
}

func (interpreter *Interpreter) compositeDestructorFunction(
compositeDeclaration ast.CompositeLikeDeclaration,
lexicalScope *VariableActivation,
) *InterpretedFunctionValue {

destructor := compositeDeclaration.DeclarationMembers().Destructor()
if destructor == nil {
return nil
}

statements := destructor.FunctionDeclaration.FunctionBlock.Block.Statements

var preConditions ast.Conditions

conditions := destructor.FunctionDeclaration.FunctionBlock.PreConditions
if conditions != nil {
preConditions = *conditions
}

var beforeStatements []ast.Statement
var rewrittenPostConditions ast.Conditions

postConditions := destructor.FunctionDeclaration.FunctionBlock.PostConditions
if postConditions != nil {
postConditionsRewrite :=
interpreter.Program.Elaboration.PostConditionsRewrite(postConditions)

beforeStatements = postConditionsRewrite.BeforeStatements
rewrittenPostConditions = postConditionsRewrite.RewrittenPostConditions
}

return NewInterpretedFunctionValue(
interpreter,
nil,
emptyImpureFunctionType,
lexicalScope,
beforeStatements,
preConditions,
statements,
rewrittenPostConditions,
)
}

func (interpreter *Interpreter) defaultFunctions(
members *ast.Members,
lexicalScope *VariableActivation,
Expand Down Expand Up @@ -2234,13 +2172,11 @@ func (interpreter *Interpreter) declareInterface(
interfaceType.InitializerParameters,
lexicalScope,
)
destructorFunctionWrapper := interpreter.destructorFunctionWrapper(declaration.Members, lexicalScope)
functionWrappers := interpreter.functionWrappers(declaration.Members, lexicalScope)
defaultFunctions := interpreter.defaultFunctions(declaration.Members, lexicalScope)

interpreter.SharedState.typeCodes.InterfaceCodes[typeID] = WrapperCode{
InitializerFunctionWrapper: initializerFunctionWrapper,
DestructorFunctionWrapper: destructorFunctionWrapper,
FunctionWrappers: functionWrappers,
Functions: defaultFunctions,
}
Expand Down Expand Up @@ -2278,23 +2214,6 @@ var voidFunctionType = &sema.FunctionType{
ReturnTypeAnnotation: sema.VoidTypeAnnotation,
}

func (interpreter *Interpreter) destructorFunctionWrapper(
members *ast.Members,
lexicalScope *VariableActivation,
) FunctionWrapper {

destructor := members.Destructor()
if destructor == nil {
return nil
}

return interpreter.functionConditionsWrapper(
destructor.FunctionDeclaration,
voidFunctionType,
lexicalScope,
)
}

func (interpreter *Interpreter) functionConditionsWrapper(
declaration *ast.FunctionDeclaration,
functionType *sema.FunctionType,
Expand Down
2 changes: 0 additions & 2 deletions runtime/interpreter/interpreter_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -1207,8 +1207,6 @@ func (interpreter *Interpreter) VisitCreateExpression(expression *ast.CreateExpr
func (interpreter *Interpreter) VisitDestroyExpression(expression *ast.DestroyExpression) Value {
value := interpreter.evalExpression(expression.Expression)

interpreter.invalidateResource(value)
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved

locationRange := LocationRange{
Location: interpreter.Location,
HasPosition: expression,
Expand Down
Loading