diff --git a/runtime/sema/type.go b/runtime/sema/type.go index 7577ebf30e..33b31d602d 100644 --- a/runtime/sema/type.go +++ b/runtime/sema/type.go @@ -6644,18 +6644,21 @@ func (t *CapabilityType) Unify( report func(err error), outerRange ast.Range, ) bool { - otherOptional, ok := other.(*CapabilityType) + otherCap, ok := other.(*CapabilityType) if !ok { return false } - return t.BorrowType.Unify(otherOptional.BorrowType, typeParameters, report, outerRange) + if t.BorrowType == nil { + return false + } + + return t.BorrowType.Unify(otherCap.BorrowType, typeParameters, report, outerRange) } func (t *CapabilityType) Resolve(typeParameters map[*TypeParameter]Type) Type { var resolvedBorrowType Type if t.BorrowType != nil { - resolvedBorrowType = t.BorrowType.Resolve(typeParameters) } @@ -6694,7 +6697,9 @@ func (t *CapabilityType) BaseType() Type { func (t *CapabilityType) TypeArguments() []Type { borrowType := t.BorrowType if borrowType == nil { - borrowType = &AnyType{} + borrowType = &ReferenceType{ + Type: &AnyType{}, + } } return []Type{ borrowType, diff --git a/runtime/tests/checker/genericfunction_test.go b/runtime/tests/checker/genericfunction_test.go index 6753d3377d..5734b0371a 100644 --- a/runtime/tests/checker/genericfunction_test.go +++ b/runtime/tests/checker/genericfunction_test.go @@ -886,3 +886,16 @@ func TestCheckGenericFunctionIsInvalid(t *testing.T) { assert.False(t, genericFunctionType.IsInvalidType()) } + +// https://github.com/onflow/cadence/issues/225 +func TestCheckBorrowOfCapabilityWithoutTypeArgument(t *testing.T) { + + t.Parallel() + + _, err := ParseAndCheckWithPanic(t, ` + let cap: Capability = panic("") + let ref = cap.borrow<&Int>()! + `) + + require.NoError(t, err) +}