From 736227f3a751392c3a616aff0caf2843e35ac1ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20M=C3=BCller?= Date: Tue, 2 Jul 2024 15:52:34 -0700 Subject: [PATCH] simplify subtyping --- runtime/interpreter/interpreter.go | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/runtime/interpreter/interpreter.go b/runtime/interpreter/interpreter.go index 46edcb3d48..453861fc31 100644 --- a/runtime/interpreter/interpreter.go +++ b/runtime/interpreter/interpreter.go @@ -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 {