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

Conversion to entitled type #2693

Closed
wants to merge 6 commits into from
Closed
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
44 changes: 43 additions & 1 deletion runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4933,7 +4933,9 @@
})
}
})
// TODO: include inherited entitlements
t.EffectiveInterfaceConformanceSet().ForEach(func(it *InterfaceType) {
set.SetAll(it.SupportedEntitlements())
})

t.supportedEntitlements = set
return set
Expand Down Expand Up @@ -7656,6 +7658,46 @@
return t
}

// Converts its input to an entitled type according to the following rules:
// * `ConvertToEntitledType(&T) ---> auth(Entitlements(T)) &T`
// * `ConvertToEntitledType(Capability<T>) ---> Capability<ConvertToEntitledType(T)>`
// * `ConvertToEntitledType(T?) ---> ConvertToEntitledType(T)?
// * `ConvertToEntitledType(T) ---> T`
// where Entitlements(I) is defined as the result of T.SupportedEntitlements()
func ConvertToEntitledType(memoryGauge common.MemoryGauge, t Type) Type {
switch t := t.(type) {
case *ReferenceType:
switch t.Authorization {
case UnauthorizedAccess:
innerType := ConvertToEntitledType(memoryGauge, t.Type)
auth := UnauthorizedAccess
if entitlementSupportingType, ok := innerType.(EntitlementSupportingType); ok {
supportedEntitlements := entitlementSupportingType.SupportedEntitlements()
if supportedEntitlements.Len() > 0 {
auth = EntitlementSetAccess{
SetKind: Conjunction,
Entitlements: supportedEntitlements,
}
}
}
return NewReferenceType(
memoryGauge,
innerType,
auth,
)
// type is already entitled
default:
return t

Check warning on line 7690 in runtime/sema/type.go

View check run for this annotation

Codecov / codecov/patch

runtime/sema/type.go#L7689-L7690

Added lines #L7689 - L7690 were not covered by tests
}
case *OptionalType:
return NewOptionalType(memoryGauge, ConvertToEntitledType(memoryGauge, t.Type))
case *CapabilityType:
return NewCapabilityType(memoryGauge, ConvertToEntitledType(memoryGauge, t.BorrowType))
default:
return t
}
}

var NativeCompositeTypes = map[string]*CompositeType{}

func init() {
Expand Down
333 changes: 333 additions & 0 deletions runtime/sema/type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2021,3 +2021,336 @@ func TestMapType(t *testing.T) {
require.True(t, outputFunction.Parameters[0].TypeAnnotation.Type.(*GenericType).TypeParameter == outputFunction.TypeParameters[0])
})
}

func TestConvertToEntitledType(t *testing.T) {

t.Parallel()

testLocation := common.StringLocation("test")

entitlementE := NewEntitlementType(nil, testLocation, "E")
entitlementF := NewEntitlementType(nil, testLocation, "F")
entitlementG := NewEntitlementType(nil, testLocation, "G")

eAccess := NewEntitlementSetAccess([]*EntitlementType{entitlementE}, Conjunction)
fAccess := NewEntitlementSetAccess([]*EntitlementType{entitlementF}, Conjunction)
eOrFAccess := NewEntitlementSetAccess([]*EntitlementType{entitlementE, entitlementF}, Disjunction)
eAndFAccess := NewEntitlementSetAccess([]*EntitlementType{entitlementE, entitlementF}, Conjunction)
eAndGAccess := NewEntitlementSetAccess([]*EntitlementType{entitlementE, entitlementG}, Conjunction)
eFAndGAccess := NewEntitlementSetAccess([]*EntitlementType{entitlementE, entitlementF, entitlementG}, Conjunction)

mapM := NewEntitlementMapType(nil, testLocation, "M")
mapM.Relations = []EntitlementRelation{
{
Input: entitlementE,
Output: entitlementF,
},
{
Input: entitlementF,
Output: entitlementG,
},
}
mapAccess := NewEntitlementMapAccess(mapM)

compositeStructWithOnlyE := &CompositeType{
Location: testLocation,
Identifier: "S",
Kind: common.CompositeKindStructure,
Members: &StringMemberOrderedMap{},
}
compositeStructWithOnlyE.Members.Set(
"foo",
NewFieldMember(nil, compositeStructWithOnlyE, eAccess, ast.VariableKindConstant, "foo", IntType, ""),
)

compositeResourceWithOnlyF := &CompositeType{
Location: testLocation,
Identifier: "R",
Kind: common.CompositeKindResource,
Members: &StringMemberOrderedMap{},
}
compositeResourceWithOnlyF.Members.Set(
"bar",
NewFieldMember(nil, compositeResourceWithOnlyF, fAccess, ast.VariableKindConstant, "bar", IntType, ""),
)
compositeResourceWithOnlyF.Members.Set(
"baz",
NewFieldMember(nil, compositeResourceWithOnlyF, fAccess, ast.VariableKindConstant, "baz", compositeStructWithOnlyE, ""),
)

compositeResourceWithEOrF := &CompositeType{
Location: testLocation,
Identifier: "R",
Kind: common.CompositeKindResource,
Members: &StringMemberOrderedMap{},
}
compositeResourceWithEOrF.Members.Set(
"qux",
NewFieldMember(nil, compositeResourceWithEOrF, eOrFAccess, ast.VariableKindConstant, "qux", IntType, ""),
)

compositeTwoFields := &CompositeType{
Location: testLocation,
Identifier: "S",
Kind: common.CompositeKindStructure,
Members: &StringMemberOrderedMap{},
}
compositeTwoFields.Members.Set(
"foo",
NewFieldMember(nil, compositeTwoFields, eAccess, ast.VariableKindConstant, "foo", IntType, ""),
)
compositeTwoFields.Members.Set(
"bar",
NewFieldMember(nil, compositeTwoFields, fAccess, ast.VariableKindConstant, "bar", IntType, ""),
)

interfaceTypeWithEAndG := &InterfaceType{
Location: testLocation,
Identifier: "I",
CompositeKind: common.CompositeKindResource,
Members: &StringMemberOrderedMap{},
}
interfaceTypeWithEAndG.Members.Set(
"foo",
NewFunctionMember(nil, interfaceTypeWithEAndG, eAndGAccess, "foo", &FunctionType{}, ""),
)

interfaceTypeInheriting := &InterfaceType{
Location: testLocation,
Identifier: "J",
CompositeKind: common.CompositeKindResource,
Members: &StringMemberOrderedMap{},
ExplicitInterfaceConformances: []*InterfaceType{interfaceTypeWithEAndG},
}

compositeTypeInheriting := &CompositeType{
Location: testLocation,
Identifier: "RI",
Kind: common.CompositeKindResource,
Members: &StringMemberOrderedMap{},
ExplicitInterfaceConformances: []*InterfaceType{interfaceTypeInheriting},
}

compositeTypeWithMap := &CompositeType{
Location: testLocation,
Identifier: "RI",
Kind: common.CompositeKindResource,
Members: &StringMemberOrderedMap{},
}
compositeTypeWithMap.Members.Set(
"foo",
NewFunctionMember(nil, compositeTypeWithMap, mapAccess, "foo", &FunctionType{}, ""),
)

interfaceTypeWithMap := &InterfaceType{
Location: testLocation,
Identifier: "RI",
CompositeKind: common.CompositeKindResource,
Members: &StringMemberOrderedMap{},
}
interfaceTypeWithMap.Members.Set(
"foo",
NewFunctionMember(nil, interfaceTypeWithMap, mapAccess, "foo", &FunctionType{}, ""),
)

compositeTypeWithCapField := &CompositeType{
Location: testLocation,
Identifier: "RI",
Kind: common.CompositeKindResource,
Members: &StringMemberOrderedMap{},
}
compositeTypeWithCapField.Members.Set(
"foo",
NewFieldMember(
nil, compositeTypeWithCapField, UnauthorizedAccess, ast.VariableKindConstant, "foo",
NewCapabilityType(nil,
NewReferenceType(nil, interfaceTypeInheriting, UnauthorizedAccess),
),
"",
),
)

interfaceTypeWithCapField := &InterfaceType{
Location: testLocation,
Identifier: "RI",
CompositeKind: common.CompositeKindResource,
Members: &StringMemberOrderedMap{},
}
interfaceTypeWithCapField.Members.Set(
"foo",
NewFieldMember(
nil, interfaceTypeWithCapField, UnauthorizedAccess, ast.VariableKindConstant, "foo",
NewCapabilityType(nil,
NewReferenceType(nil, interfaceTypeInheriting, UnauthorizedAccess),
),
"",
),
)

interfaceTypeInheritingCapField := &InterfaceType{
Location: testLocation,
Identifier: "J",
CompositeKind: common.CompositeKindResource,
Members: &StringMemberOrderedMap{},
ExplicitInterfaceConformances: []*InterfaceType{interfaceTypeWithCapField},
}

compositeTypeInheritingCapField := &CompositeType{
Location: testLocation,
Identifier: "RI",
Kind: common.CompositeKindResource,
Members: &StringMemberOrderedMap{},
ExplicitInterfaceConformances: []*InterfaceType{interfaceTypeInheritingCapField},
}

tests := []struct {
Input Type
Output Type
Name string
}{
{
Input: NewReferenceType(nil, IntType, UnauthorizedAccess),
Output: NewReferenceType(nil, IntType, UnauthorizedAccess),
Name: "int",
},
{
Input: NewReferenceType(nil, &FunctionType{}, UnauthorizedAccess),
Output: NewReferenceType(nil, &FunctionType{}, UnauthorizedAccess),
Name: "function",
},
{
Input: NewReferenceType(nil, compositeStructWithOnlyE, UnauthorizedAccess),
Output: NewReferenceType(nil, compositeStructWithOnlyE, eAccess),
Name: "composite E",
},
{
Input: NewReferenceType(nil, compositeResourceWithOnlyF, UnauthorizedAccess),
Output: NewReferenceType(nil, compositeResourceWithOnlyF, fAccess),
Name: "composite F",
},
{
Input: NewReferenceType(nil, compositeResourceWithEOrF, UnauthorizedAccess),
Output: NewReferenceType(nil, compositeResourceWithEOrF, eAndFAccess),
Name: "composite E or F",
},
{
Input: NewReferenceType(nil, compositeTwoFields, UnauthorizedAccess),
Output: NewReferenceType(nil, compositeTwoFields, eAndFAccess),
Name: "composite E and F",
},
{
Input: NewReferenceType(nil, interfaceTypeWithEAndG, UnauthorizedAccess),
Output: NewReferenceType(nil, interfaceTypeWithEAndG, eAndGAccess),
Name: "interface E and G",
},
{
Input: NewReferenceType(nil, interfaceTypeInheriting, UnauthorizedAccess),
Output: NewReferenceType(nil, interfaceTypeInheriting, eAndGAccess),
Name: "interface inheritance",
},
{
Input: NewReferenceType(nil, compositeTypeInheriting, UnauthorizedAccess),
Output: NewReferenceType(nil, compositeTypeInheriting, eAndGAccess),
Name: "composite inheritance",
},
{
Input: NewReferenceType(nil, compositeTypeWithMap, UnauthorizedAccess),
Output: NewReferenceType(nil, compositeTypeWithMap, eAndFAccess),
Name: "composite map",
},
{
Input: NewReferenceType(nil, interfaceTypeWithMap, UnauthorizedAccess),
Output: NewReferenceType(nil, interfaceTypeWithMap, eAndFAccess),
Name: "interface map",
},
{
Input: NewReferenceType(nil, NewCapabilityType(nil, NewReferenceType(nil, compositeTypeWithMap, UnauthorizedAccess)), UnauthorizedAccess),
Output: NewReferenceType(nil, NewCapabilityType(nil, NewReferenceType(nil, compositeTypeWithMap, eAndFAccess)), UnauthorizedAccess),
Name: "reference to capability",
},
{
Input: NewReferenceType(nil, NewIntersectionType(nil, []*InterfaceType{interfaceTypeInheriting, interfaceTypeWithMap}), UnauthorizedAccess),
Output: NewReferenceType(nil, NewIntersectionType(nil, []*InterfaceType{interfaceTypeInheriting, interfaceTypeWithMap}), eFAndGAccess),
Name: "intersection",
},
// no change
{
Input: NewReferenceType(nil, compositeTypeWithCapField, UnauthorizedAccess),
Output: NewReferenceType(nil, compositeTypeWithCapField, UnauthorizedAccess),
Name: "composite with capability field",
},
// no change
{
Input: NewReferenceType(nil, interfaceTypeWithCapField, UnauthorizedAccess),
Output: NewReferenceType(nil, interfaceTypeWithCapField, UnauthorizedAccess),
Name: "interface with capability field",
},
// no change
{
Input: NewReferenceType(nil, compositeTypeInheritingCapField, UnauthorizedAccess),
Output: NewReferenceType(nil, compositeTypeInheritingCapField, UnauthorizedAccess),
Name: "composite inheriting capability field",
},
// no change
{
Input: NewReferenceType(nil, interfaceTypeInheritingCapField, UnauthorizedAccess),
Output: NewReferenceType(nil, interfaceTypeInheritingCapField, UnauthorizedAccess),
Name: "interface inheriting capability field",
},
// TODO: add tests for array and dictionary entitlements once the mutability changes are merged
}

// create capability versions of all the existing tests
for _, test := range tests {
var capabilityTest struct {
Input Type
Output Type
Name string
}
capabilityTest.Input = NewCapabilityType(nil, test.Input)
capabilityTest.Output = NewCapabilityType(nil, test.Output)
capabilityTest.Name = "capability " + test.Name

tests = append(tests, capabilityTest)
}

// create optional versions of all the existing tests
for _, test := range tests {
var optionalTest struct {
Input Type
Output Type
Name string
}
optionalTest.Input = NewOptionalType(nil, test.Input)
optionalTest.Output = NewOptionalType(nil, test.Output)
optionalTest.Name = "optional " + test.Name

tests = append(tests, optionalTest)
}

var compareTypesRecursively func(t *testing.T, expected Type, actual Type)
compareTypesRecursively = func(t *testing.T, expected Type, actual Type) {
require.IsType(t, expected, actual)

switch expected := expected.(type) {
case *ReferenceType:
actual := actual.(*ReferenceType)
require.IsType(t, expected.Authorization, actual.Authorization)
require.True(t, expected.Authorization.Equal(actual.Authorization))
compareTypesRecursively(t, expected.Type, actual.Type)
case *OptionalType:
actual := actual.(*OptionalType)
compareTypesRecursively(t, expected.Type, actual.Type)
case *CapabilityType:
actual := actual.(*CapabilityType)
compareTypesRecursively(t, expected.BorrowType, actual.BorrowType)
}
}

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
compareTypesRecursively(t, ConvertToEntitledType(nil, test.Input), test.Output)
})
}

}
Loading
Loading