From 251f6e3854539b6056d17d2b4987edba1c84e274 Mon Sep 17 00:00:00 2001 From: Daniel Sainati Date: Wed, 4 Oct 2023 10:25:13 -0400 Subject: [PATCH 1/3] don't add relations to mappings if they fail --- runtime/sema/check_interface_declaration.go | 2 ++ runtime/tests/checker/entitlements_test.go | 28 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/runtime/sema/check_interface_declaration.go b/runtime/sema/check_interface_declaration.go index 51d798896b..45934ed77a 100644 --- a/runtime/sema/check_interface_declaration.go +++ b/runtime/sema/check_interface_declaration.go @@ -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) @@ -538,6 +539,7 @@ func (checker *Checker) declareEntitlementMappingType(declaration *ast.Entitleme checker.report(&InvalidNonEntitlementTypeInMapError{ Pos: association.Output.Identifier.Pos, }) + continue } entitlementRelations = append( diff --git a/runtime/tests/checker/entitlements_test.go b/runtime/tests/checker/entitlements_test.go index c43a02c0fc..3432237c3f 100644 --- a/runtime/tests/checker/entitlements_test.go +++ b/runtime/tests/checker/entitlements_test.go @@ -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, ` @@ -7312,3 +7315,28 @@ func TestCheckEntitlementOptionalChaining(t *testing.T) { require.NoError(t, err) }) } + +func TestCheckEntitlementMissingInMap(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; + selfRef.foo; + } + } + `) + + errors := RequireCheckerErrors(t, err, 2) + require.IsType(t, errors[0], &sema.NotDeclaredError{}) + require.IsType(t, errors[1], &sema.InvalidNonEntitlementTypeInMapError{}) +} From 1be5126ee5c345ef317de432597f6e3c72c2a3bc Mon Sep 17 00:00:00 2001 From: Daniel Sainati Date: Wed, 4 Oct 2023 10:26:34 -0400 Subject: [PATCH 2/3] other test --- runtime/tests/checker/entitlements_test.go | 37 +++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/runtime/tests/checker/entitlements_test.go b/runtime/tests/checker/entitlements_test.go index 3432237c3f..dffec20bb2 100644 --- a/runtime/tests/checker/entitlements_test.go +++ b/runtime/tests/checker/entitlements_test.go @@ -7320,7 +7320,11 @@ func TestCheckEntitlementMissingInMap(t *testing.T) { t.Parallel() - _, err := ParseAndCheck(t, ` + t.Run("missing type", func(t *testing.T) { + + t.Parallel() + + _, err := ParseAndCheck(t, ` access(all) entitlement X access(all) entitlement mapping M { X -> X @@ -7336,7 +7340,32 @@ func TestCheckEntitlementMissingInMap(t *testing.T) { } `) - errors := RequireCheckerErrors(t, err, 2) - require.IsType(t, errors[0], &sema.NotDeclaredError{}) - require.IsType(t, errors[1], &sema.InvalidNonEntitlementTypeInMapError{}) + errors := RequireCheckerErrors(t, err, 2) + require.IsType(t, errors[0], &sema.NotDeclaredError{}) + require.IsType(t, errors[1], &sema.InvalidNonEntitlementTypeInMapError{}) + }) + + 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{}) + }) } From 16885b67a7c23193b6e549ed4eacd271b19b0f7c Mon Sep 17 00:00:00 2001 From: Daniel Sainati Date: Wed, 4 Oct 2023 13:58:45 -0400 Subject: [PATCH 3/3] fix tests --- runtime/tests/checker/entitlements_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/runtime/tests/checker/entitlements_test.go b/runtime/tests/checker/entitlements_test.go index dffec20bb2..3a997c54d5 100644 --- a/runtime/tests/checker/entitlements_test.go +++ b/runtime/tests/checker/entitlements_test.go @@ -7334,15 +7334,15 @@ func TestCheckEntitlementMissingInMap(t *testing.T) { access(M) var foo: auth(M) &Int init() { self.foo = &3 as auth(X) &Int - var selfRef = &self as auth(X) &S; - selfRef.foo; + var selfRef = &self as auth(X) &S + selfRef.foo } } `) errors := RequireCheckerErrors(t, err, 2) - require.IsType(t, errors[0], &sema.NotDeclaredError{}) - require.IsType(t, errors[1], &sema.InvalidNonEntitlementTypeInMapError{}) + require.IsType(t, &sema.NotDeclaredError{}, errors[0]) + require.IsType(t, &sema.InvalidNonEntitlementTypeInMapError{}, errors[1]) }) t.Run("non entitlement type", func(t *testing.T) { @@ -7359,13 +7359,13 @@ func TestCheckEntitlementMissingInMap(t *testing.T) { access(M) var foo: auth(M) &Int init() { self.foo = &3 as auth(X) &Int - var selfRef = &self as auth(X) &S; - selfRef.foo; + var selfRef = &self as auth(X) &S + selfRef.foo } } `) errors := RequireCheckerErrors(t, err, 1) - require.IsType(t, errors[0], &sema.InvalidNonEntitlementTypeInMapError{}) + require.IsType(t, &sema.InvalidNonEntitlementTypeInMapError{}, errors[0]) }) }