Skip to content

Commit

Permalink
Merge pull request #3447 from onflow/bastian/simplify-subtyping
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Jul 8, 2024
2 parents 2a5e3c4 + 736227f commit 5979bf0
Showing 1 changed file with 9 additions and 17 deletions.
26 changes: 9 additions & 17 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3997,39 +3997,31 @@ func (interpreter *Interpreter) IsSubType(subType StaticType, superType StaticTy
return interpreter.IsSubTypeOfSemaType(subType, semaType)
}

func (interpreter *Interpreter) IsSubTypeOfSemaType(subType StaticType, superType sema.Type) bool {
func (interpreter *Interpreter) IsSubTypeOfSemaType(staticSubType StaticType, superType sema.Type) bool {
if superType == sema.AnyType {
return true
}

switch subType := subType.(type) {
// Optimization: Implement subtyping for common cases directly,
// without converting the subtype to a sema type.

switch staticSubType := staticSubType.(type) {
case *OptionalStaticType:
if superType, ok := superType.(*sema.OptionalType); ok {
return interpreter.IsSubTypeOfSemaType(subType.Type, superType.Type)
return interpreter.IsSubTypeOfSemaType(staticSubType.Type, superType.Type)
}

switch superType {
case sema.AnyStructType, sema.AnyResourceType:
return interpreter.IsSubTypeOfSemaType(subType.Type, superType)
}

case *ReferenceStaticType:
if superType, ok := superType.(*sema.ReferenceType); ok {

// First, check that the static type of the referenced value
// is a subtype of the super type

return subType.ReferencedType != nil &&
interpreter.IsSubTypeOfSemaType(subType.ReferencedType, superType.Type) &&
superType.Authorization.PermitsAccess(interpreter.MustConvertStaticAuthorizationToSemaAccess(subType.Authorization))
return interpreter.IsSubTypeOfSemaType(staticSubType.Type, superType)
}

return superType == sema.AnyStructType
}

semaType := interpreter.MustConvertStaticToSemaType(subType)
semaSubType := interpreter.MustConvertStaticToSemaType(staticSubType)

return sema.IsSubType(semaType, superType)
return sema.IsSubType(semaSubType, superType)
}

func (interpreter *Interpreter) domainPaths(address common.Address, domain common.PathDomain) []Value {
Expand Down

0 comments on commit 5979bf0

Please sign in to comment.