From cb8242d1f8f1a974291a86937d04e47975be73bc Mon Sep 17 00:00:00 2001 From: Daniel Sainati Date: Thu, 28 Mar 2024 12:12:55 -0400 Subject: [PATCH] properly handle never type argument --- runtime/capabilities_test.go | 12 ++++++++++++ runtime/stdlib/account.go | 9 +++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/runtime/capabilities_test.go b/runtime/capabilities_test.go index ac73a29e55..3b172dde7a 100644 --- a/runtime/capabilities_test.go +++ b/runtime/capabilities_test.go @@ -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(path) == nil) + assert(self.account.capabilities.exists(path)) + } + access(all) fun testSwap(): Int { let ref = self.account.capabilities.get<&R>(/public/r)!.borrow()! @@ -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") diff --git a/runtime/stdlib/account.go b/runtime/stdlib/account.go index aec228ceb8..14a528da1a 100644 --- a/runtime/stdlib/account.go +++ b/runtime/stdlib/account.go @@ -3592,8 +3592,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()) }