Skip to content

Commit

Permalink
Merge pull request #3357 from onflow/supun/improve-member-access
Browse files Browse the repository at this point in the history
Return a copy for enum-typed members, during member-access via references
  • Loading branch information
SupunS authored May 17, 2024
2 parents e837800 + 535c16a commit 96cf17a
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
4 changes: 2 additions & 2 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -5150,8 +5150,8 @@ func (*CompositeType) IsComparable() bool {
return false
}

func (*CompositeType) ContainFieldsOrElements() bool {
return true
func (t *CompositeType) ContainFieldsOrElements() bool {
return t.Kind != common.CompositeKindEnum
}

func (t *CompositeType) TypeAnnotationState() TypeAnnotationState {
Expand Down
23 changes: 23 additions & 0 deletions runtime/tests/checker/entitlements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1615,6 +1615,29 @@ func TestCheckBasicEntitlementMappingAccess(t *testing.T) {

assert.NoError(t, err)
})

t.Run("enum field", func(t *testing.T) {
t.Parallel()

_, err := ParseAndCheck(t, `
enum Status: Int {
case On
case Off
}
entitlement mapping M {}
struct interface S {
// enum cases are public.
// Hence, using entitlement mappings for enum-typed variables are pointless.
// So reject this statically.
access(mapping M) let status: Status
}
`)

errs := RequireCheckerErrors(t, err, 1)
require.IsType(t, &sema.InvalidMappedEntitlementMemberError{}, errs[0])
})
}

func TestCheckInvalidEntitlementAccess(t *testing.T) {
Expand Down
22 changes: 22 additions & 0 deletions runtime/tests/checker/for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,4 +507,26 @@ func TestCheckReferencesInForLoop(t *testing.T) {

require.NoError(t, err)
})

t.Run("Enum array", func(t *testing.T) {
t.Parallel()

_, err := ParseAndCheck(t, `
enum Status: Int {
case On
case Off
}
fun main() {
var array = [Status.On, Status.Off]
var arrayRef = &array as &[Status]
for element in arrayRef {
let e: Status = element
}
}
`)

require.NoError(t, err)
})
}
26 changes: 26 additions & 0 deletions runtime/tests/checker/member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -961,4 +961,30 @@ func TestCheckMemberAccess(t *testing.T) {
})
}
})

t.Run("composite reference, enum field", func(t *testing.T) {
t.Parallel()

_, err := ParseAndCheck(t, `
struct Test {
var status: Status
init() {
self.status = Status.Off
}
}
enum Status: Int {
case On
case Off
}
fun test() {
let test = Test()
let testRef = &test as &Test
var x: Status = testRef.status
}
`)

require.NoError(t, err)
})
}

0 comments on commit 96cf17a

Please sign in to comment.