Skip to content

Commit

Permalink
Move is-container check to the type itself
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Jun 23, 2023
1 parent 651f4b5 commit 9a64a7f
Show file tree
Hide file tree
Showing 18 changed files with 254 additions and 72 deletions.
4 changes: 1 addition & 3 deletions runtime/interpreter/interpreter_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,8 @@ func (interpreter *Interpreter) memberExpressionGetterSetter(memberExpression *a
// e.g.2: Given T?, this returns (&T)?
func (interpreter *Interpreter) getReferenceValue(value Value, semaType sema.Type) Value {
switch value.(type) {
case NilValue:
case NilValue, ReferenceValue:
// Reference to a nil, should return a nil.
return value
case ReferenceValue:
// If the value is already a reference then return the same reference.
return value
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/sema/account_capability_controller.cdc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
access(all) struct AccountCapabilityController {
access(all) struct AccountCapabilityController: MemberAccessible {

/// An arbitrary "tag" for the controller.
/// For example, it could be used to describe the purpose of the capability.
Expand Down
21 changes: 11 additions & 10 deletions runtime/sema/account_capability_controller.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions runtime/sema/anyresource_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var AnyResourceType = &SimpleType{
Equatable: false,
Comparable: false,
// The actual returnability of a value is checked at run-time
Exportable: true,
Importable: false,
Exportable: true,
Importable: false,
MemberAccessible: true,
}
3 changes: 2 additions & 1 deletion runtime/sema/anystruct_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ var AnyStructType = &SimpleType{
Comparable: false,
Exportable: true,
// The actual importability is checked at runtime
Importable: true,
Importable: true,
MemberAccessible: true,
}

var AnyStructTypeAnnotation = NewTypeAnnotation(AnyStructType)
2 changes: 1 addition & 1 deletion runtime/sema/authaccount.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

access(all) struct AuthAccount {
access(all) struct AuthAccount: MemberAccessible {

/// The address of the account.
access(all) let address: Address
Expand Down
2 changes: 1 addition & 1 deletion runtime/sema/block.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

access(all) struct Block {
access(all) struct Block: MemberAccessible {

/// The height of the block.
///
Expand Down
21 changes: 11 additions & 10 deletions runtime/sema/block.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 1 addition & 18 deletions runtime/sema/check_member_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func shouldReturnReference(parentType, memberType Type) bool {
return false
}

return isContainerType(memberType)
return memberType.IsMemberAccessible()
}

func isReferenceType(typ Type) bool {
Expand All @@ -121,23 +121,6 @@ func isReferenceType(typ Type) bool {
return isReference
}

func isContainerType(typ Type) bool {
switch typ := typ.(type) {
case *CompositeType,
*DictionaryType,
ArrayType:
return true
case *OptionalType:
return isContainerType(typ.Type)
default:
switch typ {
case AnyStructType, AnyResourceType:
return true
}
return false
}
}

func (checker *Checker) visitMember(expression *ast.MemberExpression) (accessedType Type, resultingType Type, member *Member, isOptional bool) {
memberInfo, ok := checker.Elaboration.MemberExpressionMemberInfo(expression)
if ok {
Expand Down
2 changes: 1 addition & 1 deletion runtime/sema/deployedcontract.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

access(all) struct DeployedContract {
access(all) struct DeployedContract: MemberAccessible {
/// The address of the account where the contract is deployed at.
access(all) let address: Address

Expand Down
21 changes: 11 additions & 10 deletions runtime/sema/deployedcontract.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions runtime/sema/gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ type typeDecl struct {
exportable bool
comparable bool
importable bool
memberAccessible bool
memberDeclarations []ast.Declaration
nestedTypes []*typeDecl
}
Expand Down Expand Up @@ -423,6 +424,15 @@ func (g *generator) VisitCompositeDeclaration(decl *ast.CompositeDeclaration) (_

case "Importable":
typeDecl.importable = true

case "MemberAccessible":
if !canGenerateSimpleType {
panic(fmt.Errorf(
"composite types cannot be explicitly marked as member accessible: %s",
g.currentTypeID(),
))
}
typeDecl.memberAccessible = true
}
}

Expand Down Expand Up @@ -1158,6 +1168,7 @@ func simpleTypeLiteral(ty *typeDecl) dst.Expr {
goKeyValue("Comparable", goBoolLit(ty.comparable)),
goKeyValue("Exportable", goBoolLit(ty.exportable)),
goKeyValue("Importable", goBoolLit(ty.importable)),
goKeyValue("MemberAccessible", goBoolLit(ty.memberAccessible)),
}

return &dst.UnaryExpr{
Expand Down
5 changes: 5 additions & 0 deletions runtime/sema/simple_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type SimpleType struct {
Comparable bool
Storable bool
IsResource bool
MemberAccessible bool
}

var _ Type = &SimpleType{}
Expand Down Expand Up @@ -106,6 +107,10 @@ func (t *SimpleType) IsImportable(_ map[*Member]bool) bool {
return t.Importable
}

func (t *SimpleType) IsMemberAccessible() bool {
return t.MemberAccessible
}

func (*SimpleType) TypeAnnotationState() TypeAnnotationState {
return TypeAnnotationStateValid
}
Expand Down
2 changes: 1 addition & 1 deletion runtime/sema/storage_capability_controller.cdc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
access(all) struct StorageCapabilityController {
access(all) struct StorageCapabilityController: MemberAccessible {

/// An arbitrary "tag" for the controller.
/// For example, it could be used to describe the purpose of the capability.
Expand Down
21 changes: 11 additions & 10 deletions runtime/sema/storage_capability_controller.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9a64a7f

Please sign in to comment.