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

Add regression tests for nil-coalescing bug #3553

Merged
merged 1 commit into from
Aug 27, 2024
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
33 changes: 33 additions & 0 deletions runtime/tests/checker/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10417,3 +10417,36 @@ func TestCheckInvalidOptionalResourceCoalescingRightSideNilLeftSide(t *testing.T

assert.IsType(t, &sema.InvalidNilCoalescingRightResourceOperandError{}, errs[0])
}

func TestInterpretInvalidNilCoalescingResourceDuplication(t *testing.T) {

t.Parallel()

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

let answer: Int

init() {
self.answer = 42
}
}

fun main(): Int {
let rs <- [<- create R(), nil]
rs[1] <-! (nil ?? rs[0])
let r1 <- rs.remove(at:0)!
let r2 <- rs.remove(at:0)!
let answer1 = r1.answer
let answer2 = r2.answer
destroy r1
destroy r2
destroy rs
return answer1 + answer2
}
`)
require.Error(t, err)

errs := RequireCheckerErrors(t, err, 1)
assert.IsType(t, &sema.InvalidNilCoalescingRightResourceOperandError{}, errs[0])
}
42 changes: 42 additions & 0 deletions runtime/tests/interpreter/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3543,3 +3543,45 @@ func TestInterpretResourceReferenceInvalidation(t *testing.T) {
require.ErrorAs(t, err, &interpreter.InvalidatedResourceReferenceError{})
})
}

func TestInterpretInvalidNilCoalescingResourceDuplication(t *testing.T) {

t.Parallel()

inter, err := parseCheckAndInterpretWithOptions(t,
`
access(all) resource R {
access(all) let answer: Int
init() {
self.answer = 42
}
}

access(all) fun main(): Int {
let rs <- [<- create R(), nil]
rs[1] <-! (nil ?? rs[0])
let r1 <- rs.remove(at:0)!
let r2 <- rs.remove(at:0)!
let answer1 = r1.answer
let answer2 = r2.answer
destroy r1
destroy r2
destroy rs
return answer1 + answer2
}
`,
ParseCheckAndInterpretOptions{
HandleCheckerError: func(err error) {
errs := checker.RequireCheckerErrors(t, err, 1)
assert.IsType(t, &sema.InvalidNilCoalescingRightResourceOperandError{}, errs[0])
},
},
)
require.NoError(t, err)

_, err = inter.Invoke("main")
require.Error(t, err)

var destroyedResourceErr interpreter.DestroyedResourceError
require.ErrorAs(t, err, &destroyedResourceErr)
}
Loading