Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return a copy for enum-typed members, during member-access via references #3357

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}
Loading