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

Skip adding malformed entitlement relations to maps #2838

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 2 additions & 0 deletions runtime/sema/check_interface_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,7 @@ func (checker *Checker) declareEntitlementMappingType(declaration *ast.Entitleme
checker.report(&InvalidNonEntitlementTypeInMapError{
Pos: association.Input.Identifier.Pos,
})
continue
}

output := checker.convertNominalType(association.Output)
Expand All @@ -538,6 +539,7 @@ func (checker *Checker) declareEntitlementMappingType(declaration *ast.Entitleme
checker.report(&InvalidNonEntitlementTypeInMapError{
Pos: association.Output.Identifier.Pos,
})
continue
}

entitlementRelations = append(
Expand Down
57 changes: 57 additions & 0 deletions runtime/tests/checker/entitlements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7224,6 +7224,9 @@ func TestCheckEntitlementErrorReporting(t *testing.T) {
}

func TestCheckEntitlementOptionalChaining(t *testing.T) {

t.Parallel()

t.Run("optional chain function call", func(t *testing.T) {
t.Parallel()
_, err := ParseAndCheck(t, `
Expand Down Expand Up @@ -7312,3 +7315,57 @@ func TestCheckEntitlementOptionalChaining(t *testing.T) {
require.NoError(t, err)
})
}

func TestCheckEntitlementMissingInMap(t *testing.T) {

t.Parallel()

t.Run("missing type", func(t *testing.T) {

t.Parallel()

_, err := ParseAndCheck(t, `
access(all) entitlement X
access(all) entitlement mapping M {
X -> X
NonExistingEntitlement -> X
}
access(all) struct S {
access(M) var foo: auth(M) &Int
init() {
self.foo = &3 as auth(X) &Int
var selfRef = &self as auth(X) &S;
turbolent marked this conversation as resolved.
Show resolved Hide resolved
selfRef.foo;
}
}
`)

errors := RequireCheckerErrors(t, err, 2)
require.IsType(t, errors[0], &sema.NotDeclaredError{})
require.IsType(t, errors[1], &sema.InvalidNonEntitlementTypeInMapError{})
dsainati1 marked this conversation as resolved.
Show resolved Hide resolved
})

t.Run("non entitlement type", func(t *testing.T) {

t.Parallel()

_, err := ParseAndCheck(t, `
access(all) entitlement X
access(all) entitlement mapping M {
X -> X
Int -> X
}
access(all) struct S {
access(M) var foo: auth(M) &Int
init() {
self.foo = &3 as auth(X) &Int
var selfRef = &self as auth(X) &S;
selfRef.foo;
}
}
`)

errors := RequireCheckerErrors(t, err, 1)
require.IsType(t, errors[0], &sema.InvalidNonEntitlementTypeInMapError{})
})
}
Loading