Skip to content

Commit

Permalink
Merge pull request #2789 from onflow/sainati/ban-custom-destructors
Browse files Browse the repository at this point in the history
Remove support for custom destructors
  • Loading branch information
dsainati1 authored Oct 30, 2023
2 parents b9edc95 + 33c238d commit b1551c3
Show file tree
Hide file tree
Showing 40 changed files with 164 additions and 3,063 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
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 @@ -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
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
93 changes: 0 additions & 93 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,6 @@ import (

//

var emptyImpureFunctionType = sema.NewSimpleFunctionType(
sema.FunctionPurityImpure,
nil,
sema.VoidTypeAnnotation,
)

//

type getterSetter struct {
target Value
// allowMissing may be true when the got value is nil.
Expand Down Expand Up @@ -183,7 +175,6 @@ type CompositeValueFunctionsHandlerFunc func(
// 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 @@ -195,7 +186,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 @@ -1136,12 +1126,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 @@ -1155,15 +1139,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 @@ -1198,7 +1173,6 @@ func (interpreter *Interpreter) declareNonEnumCompositeValue(
}

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

Expand Down Expand Up @@ -1288,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 @@ -1544,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 @@ -2250,13 +2180,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 @@ -2290,27 +2218,6 @@ func (interpreter *Interpreter) initializerFunctionWrapper(
)
}

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
Loading

0 comments on commit b1551c3

Please sign in to comment.