From fa4cf1b77cf37d6b69b53bda67a29ced8d26e875 Mon Sep 17 00:00:00 2001 From: Supun Setunga Date: Fri, 3 Nov 2023 11:14:17 -0700 Subject: [PATCH] Add interpreter test --- runtime/runtime_test.go | 10 ++--- runtime/tests/interpreter/resources_test.go | 43 +++++++++++++++++++++ 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/runtime/runtime_test.go b/runtime/runtime_test.go index bded55499a..3286727df6 100644 --- a/runtime/runtime_test.go +++ b/runtime/runtime_test.go @@ -9509,7 +9509,7 @@ func TestNestedResourceMoveInDestructor(t *testing.T) { self.temp <- nil } - pub fun doubler(_ vault: @Bar.Vault): @Bar.Vault{ + pub fun doubler(_ vault: @Bar.Vault): @Bar.Vault { destroy <- create R(<-vault) var doubled <- self.temp <- nil return <- doubled! @@ -9519,16 +9519,16 @@ func TestNestedResourceMoveInDestructor(t *testing.T) { pub var bounty: @Bar.Vault pub var dummy: @Bar.Vault - init(_ v: @Bar.Vault){ + init(_ v: @Bar.Vault) { self.bounty <- v self.dummy <- Bar.createEmptyVault() } - pub fun swap(){ + pub fun swap() { self.bounty <-> self.dummy } - destroy(){ + destroy() { // Nested resource is moved here once var bounty <- self.bounty @@ -9623,7 +9623,7 @@ func TestNestedResourceMoveInDestructor(t *testing.T) { import Bar from %[1]s transaction { - prepare(acc: AuthAccount){ + prepare(acc: AuthAccount) { acc.save(<- Bar.createVault(balance: 100.0), to: /storage/vault)! var vault = acc.borrow<&Bar.Vault>(from: /storage/vault)! var flow <- vault.withdraw(amount: 42.0) diff --git a/runtime/tests/interpreter/resources_test.go b/runtime/tests/interpreter/resources_test.go index cd7f20e49d..4d810ef4e5 100644 --- a/runtime/tests/interpreter/resources_test.go +++ b/runtime/tests/interpreter/resources_test.go @@ -2938,3 +2938,46 @@ func TestInterpretInnerResourceDestruction(t *testing.T) { var destroyedResourceErr interpreter.DestroyedResourceError require.ErrorAs(t, err, &destroyedResourceErr) } + +func TestInterpretInnerResourceMove(t *testing.T) { + + t.Parallel() + + inter := parseCheckAndInterpret(t, ` + pub resource OuterResource { + pub var a: @InnerResource + pub var b: @InnerResource + + init() { + self.a <- create InnerResource() + self.b <- create InnerResource() + } + + pub fun swap() { + self.a <-> self.b + } + + destroy() { + // Nested resource is moved here once + var a <- self.a + + // Nested resource is again moved here. This one should fail. + self.swap() + + destroy a + destroy self.b + } + } + + pub resource InnerResource {} + + pub fun main() { + let a <- create OuterResource() + destroy a + }`, + ) + + _, err := inter.Invoke("main") + RequireError(t, err) + require.ErrorAs(t, err, &interpreter.UseBeforeInitializationError{}) +}