Skip to content

Commit 536612f

Browse files
authored
Merge pull request #3203 from onflow/sainati/capability-get-unreachable
Properly handle `Never` type argument to `Capabilities.get`
2 parents 468b11b + cb8242d commit 536612f

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

runtime/capabilities_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,13 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
235235
assert(!self.account.capabilities.exists(path))
236236
}
237237
238+
access(all)
239+
fun testNever() {
240+
let path = /public/r
241+
assert(self.account.capabilities.get<Never>(path) == nil)
242+
assert(self.account.capabilities.exists(path))
243+
}
244+
238245
access(all)
239246
fun testSwap(): Int {
240247
let ref = self.account.capabilities.get<&R>(/public/r)!.borrow()!
@@ -305,6 +312,11 @@ func TestRuntimeCapability_borrowAndCheck(t *testing.T) {
305312
require.NoError(t, err)
306313
})
307314

315+
t.Run("testNever", func(t *testing.T) {
316+
_, err := invoke("testNever")
317+
require.NoError(t, err)
318+
})
319+
308320
t.Run("testSwap", func(t *testing.T) {
309321

310322
_, err := invoke("testSwap")

runtime/stdlib/account.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,8 +3595,13 @@ func newAccountCapabilitiesGetFunction(
35953595

35963596
// Get borrow type type argument
35973597

3598-
typeParameterPair := invocation.TypeParameterTypes.Oldest()
3599-
wantedBorrowType, ok := typeParameterPair.Value.(*sema.ReferenceType)
3598+
typeParameterPairValue := invocation.TypeParameterTypes.Oldest().Value
3599+
// `Never` is never a supertype of any stored value
3600+
if typeParameterPairValue.Equal(sema.NeverType) {
3601+
return interpreter.Nil
3602+
}
3603+
3604+
wantedBorrowType, ok := typeParameterPairValue.(*sema.ReferenceType)
36003605
if !ok {
36013606
panic(errors.NewUnreachableError())
36023607
}

0 commit comments

Comments
 (0)