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

Require values passed to load and store to be storable #251

Merged
merged 2 commits into from
Jul 29, 2020
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/check_composite_declaration.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,11 +644,11 @@ func (checker *Checker) declareCompositeMembersAndValue(
//
func (checker *Checker) checkMemberStorability(members map[string]*Member) {

seenMembers := map[*Member]bool{}
storableResults := map[*Member]bool{}

for _, member := range members {

if member.IsStorable(seenMembers) {
if member.IsStorable(storableResults) {
continue
}

Expand Down
81 changes: 79 additions & 2 deletions runtime/sema/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -4617,7 +4617,10 @@ var authAccountRemovePublicKeyFunctionType = &FunctionType{

var authAccountSaveFunctionType = func() *FunctionType {

typeParameter := &TypeParameter{Name: "T"}
typeParameter := &TypeParameter{
Name: "T",
TypeBound: &StorableType{},
}

return &FunctionType{
TypeParameters: []*TypeParameter{
Expand Down Expand Up @@ -4645,7 +4648,10 @@ var authAccountSaveFunctionType = func() *FunctionType {

var authAccountLoadFunctionType = func() *FunctionType {

typeParameter := &TypeParameter{Name: "T"}
typeParameter := &TypeParameter{
Name: "T",
TypeBound: &StorableType{},
}

return &FunctionType{
TypeParameters: []*TypeParameter{
Expand Down Expand Up @@ -6145,6 +6151,10 @@ func IsSubType(subType Type, superType Type) bool {
}
}
}

case *StorableType:
storableResults := map[*Member]bool{}
return subType.IsStorable(storableResults)
}

// TODO: enforce type arguments, remove this rule
Expand Down Expand Up @@ -6789,3 +6799,70 @@ func (t *CapabilityType) GetMember(identifier string, _ ast.Range, _ func(error)
return nil
}
}

// StorableType is the supertype of all types which are storable.
//
// It is only used as e.g. a type bound, but is not accessible
// to user programs, i.e. can't be used in type annotations
// for e.g. parameters, return types, fields, etc.
//
type StorableType struct{}

func (*StorableType) IsType() {}

func (*StorableType) String() string {
return "Storable"
}

func (*StorableType) QualifiedString() string {
return "Storable"
}

func (*StorableType) ID() TypeID {
return "Storable"
}

func (*StorableType) Equal(other Type) bool {
_, ok := other.(*StorableType)
return ok
}

func (*StorableType) IsResourceType() bool {

// NOTE: Subtypes may be either resource types or not.
//
// Returning false here is safe, because this type is
// only used as e.g. a type bound, but is not accessible
// to user programs, i.e. can't be used in type annotations
// for e.g. parameters, return types, fields, etc.

return false
}

func (*StorableType) IsInvalidType() bool {
return false
}

func (*StorableType) IsStorable(_ map[*Member]bool) bool {
return true
}

func (*StorableType) IsEquatable() bool {
return false
}

func (*StorableType) TypeAnnotationState() TypeAnnotationState {
return TypeAnnotationStateValid
}

func (*StorableType) ContainsFirstLevelInterfaceType() bool {
return false
}

func (*StorableType) Unify(_ Type, _ map[*TypeParameter]Type, _ func(err error), _ ast.Range) bool {
return false
}

func (t *StorableType) Resolve(_ map[*TypeParameter]Type) Type {
return t
}
Loading