Skip to content

Commit

Permalink
Merge pull request #3203 from onflow/sainati/capability-get-unreachable
Browse files Browse the repository at this point in the history
Properly handle `Never` type argument to `Capabilities.get`
  • Loading branch information
dsainati1 authored Apr 1, 2024
2 parents 468b11b + cb8242d commit 536612f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
12 changes: 12 additions & 0 deletions runtime/capabilities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,13 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
assert(!self.account.capabilities.exists(path))
}
access(all)
fun testNever() {
let path = /public/r
assert(self.account.capabilities.get<Never>(path) == nil)
assert(self.account.capabilities.exists(path))
}
access(all)
fun testSwap(): Int {
let ref = self.account.capabilities.get<&R>(/public/r)!.borrow()!
Expand Down Expand Up @@ -305,6 +312,11 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
require.NoError(t, err)
})

t.Run("testNever", func(t *testing.T) {
_, err := invoke("testNever")
require.NoError(t, err)
})

t.Run("testSwap", func(t *testing.T) {

_, err := invoke("testSwap")
Expand Down
9 changes: 7 additions & 2 deletions runtime/stdlib/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -3595,8 +3595,13 @@ func newAccountCapabilitiesGetFunction(

// Get borrow type type argument

typeParameterPair := invocation.TypeParameterTypes.Oldest()
wantedBorrowType, ok := typeParameterPair.Value.(*sema.ReferenceType)
typeParameterPairValue := invocation.TypeParameterTypes.Oldest().Value
// `Never` is never a supertype of any stored value
if typeParameterPairValue.Equal(sema.NeverType) {
return interpreter.Nil
}

wantedBorrowType, ok := typeParameterPairValue.(*sema.ReferenceType)
if !ok {
panic(errors.NewUnreachableError())
}
Expand Down

0 comments on commit 536612f

Please sign in to comment.