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

Fix interpretation of optional binding with second value #1082

Merged
merged 4 commits into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 16 additions & 1 deletion runtime/interpreter/interpreter_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,26 @@ func (interpreter *Interpreter) visitIfStatementWithVariableDeclaration(
) controlReturn {

value := interpreter.evalExpression(declaration.Value)

valueType := interpreter.Program.Elaboration.VariableDeclarationValueTypes[declaration]

if declaration.SecondValue != nil {
secondValueType := interpreter.Program.Elaboration.VariableDeclarationSecondValueTypes[declaration]

interpreter.visitAssignment(
declaration.Transfer.Operation,
declaration.Value,
valueType,
declaration.SecondValue,
secondValueType,
declaration,
)
}

var result interface{}
if someValue, ok := value.(*SomeValue); ok {

targetType := interpreter.Program.Elaboration.VariableDeclarationTargetTypes[declaration]
valueType := interpreter.Program.Elaboration.VariableDeclarationValueTypes[declaration]
getLocationRange := locationRangeGetter(interpreter.Location, declaration.Value)
unwrappedValueCopy := interpreter.copyAndConvert(someValue.Value, valueType, targetType, getLocationRange)

Expand Down
47 changes: 47 additions & 0 deletions runtime/tests/checker/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4955,3 +4955,50 @@ func TestCheckFunctionDefinitelyHaltedNoResourceLoss(t *testing.T) {
require.NoError(t, err)
})
}

func TestCheckOptionalResourceBindingWithSecondValue(t *testing.T) {

t.Parallel()

_, err := ParseAndCheck(t, `
resource R {
let field: Int

init() {
self.field = 1
}
}

resource Test {

var r: @R?

init() {
self.r <- create R()
}

destroy () {
destroy self.r
}

fun duplicate(): @R? {
if let r <- self.r <- nil {
let r2 <- self.r <- nil
self.r <-! r2
return <-r
} else {
return nil
}
}
}

fun test() {
let test <- create Test()
let copy <- test.duplicate()

destroy copy
destroy test
}
`)
require.NoError(t, err)
}
83 changes: 83 additions & 0 deletions runtime/tests/interpreter/resources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright 2019-2020 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package interpreter_test

import (
"testing"

"github.com/onflow/cadence/runtime/interpreter"
"github.com/stretchr/testify/require"
)

func TestInterpetOptionalResourceBindingWithSecondValue(t *testing.T) {

t.Parallel()

inter := parseCheckAndInterpret(t, `
resource R {
let field: Int

init() {
self.field = 1
}
}

resource Test {

var r: @R?

init() {
self.r <- create R()
}

destroy () {
destroy self.r
}

fun duplicate(): @R? {
if let r <- self.r <- nil {
let r2 <- self.r <- nil
self.r <-! r2
return <-r
} else {
return nil
}
}
}

fun test(): Bool {
let test <- create Test()

let copy <- test.duplicate()

// "copy" here is actually expected to hold resource,
// the important test is that the field was properly set to nil
let res = copy != nil && test.r == nil

destroy copy
destroy test

return res
}
`)

result, err := inter.Invoke("test")
require.NoError(t, err)
require.Equal(t, interpreter.BoolValue(true), result)
}