Skip to content

Commit

Permalink
fix borrowing of capability without type argument
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Jul 10, 2020
1 parent 4d10cd0 commit 37a5460
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
13 changes: 9 additions & 4 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions runtime/tests/checker/genericfunction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 37a5460

Please sign in to comment.