diff --git a/runtime/convertTypes.go b/runtime/convertTypes.go index 08f5c69a45..4cc4b82030 100644 --- a/runtime/convertTypes.go +++ b/runtime/convertTypes.go @@ -527,8 +527,8 @@ func ImportType(t cadence.Type) interpreter.StaticType { return importInterfaceType(t.(cadence.InterfaceType)) case cadence.ReferenceType: return interpreter.ReferenceStaticType{ - Authorized: t.Authorized, - Type: ImportType(t.Type), + Authorized: t.Authorized, + BorrowedType: ImportType(t.Type), } case cadence.RestrictedType: restrictions := make([]interpreter.InterfaceStaticType, 0, len(t.Restrictions)) diff --git a/runtime/convertValues_test.go b/runtime/convertValues_test.go index 9bd0755556..2d7016e71d 100644 --- a/runtime/convertValues_test.go +++ b/runtime/convertValues_test.go @@ -1069,8 +1069,8 @@ func TestImportRuntimeType(t *testing.T) { Type: cadence.IntType{}, }, expected: interpreter.ReferenceStaticType{ - Authorized: false, - Type: interpreter.PrimitiveStaticTypeInt, + Authorized: false, + BorrowedType: interpreter.PrimitiveStaticTypeInt, }, }, { diff --git a/runtime/interpreter/decode.go b/runtime/interpreter/decode.go index e8cdc425ec..08c2cab0b4 100644 --- a/runtime/interpreter/decode.go +++ b/runtime/interpreter/decode.go @@ -1337,8 +1337,8 @@ func decodeReferenceStaticType(dec *cbor.StreamDecoder) (StaticType, error) { } return ReferenceStaticType{ - Authorized: authorized, - Type: staticType, + Authorized: authorized, + BorrowedType: staticType, }, nil } diff --git a/runtime/interpreter/encode.go b/runtime/interpreter/encode.go index ffd1c287ce..a34d1159e5 100644 --- a/runtime/interpreter/encode.go +++ b/runtime/interpreter/encode.go @@ -1187,7 +1187,7 @@ func (t ReferenceStaticType) Encode(e *cbor.StreamEncoder) error { return err } // Encode type at array index encodedReferenceStaticTypeTypeFieldKey - return EncodeStaticType(e, t.Type) + return EncodeStaticType(e, t.BorrowedType) } // NOTE: NEVER change, only add/increment; ensure uint64 diff --git a/runtime/interpreter/encoding_test.go b/runtime/interpreter/encoding_test.go index 10c31c1ad0..15fa02fb2a 100644 --- a/runtime/interpreter/encoding_test.go +++ b/runtime/interpreter/encoding_test.go @@ -3405,8 +3405,8 @@ func TestEncodeDecodeLinkValue(t *testing.T) { value := LinkValue{ TargetPath: publicPathValue, Type: ReferenceStaticType{ - Authorized: true, - Type: PrimitiveStaticTypeBool, + Authorized: true, + BorrowedType: PrimitiveStaticTypeBool, }, } @@ -3439,8 +3439,8 @@ func TestEncodeDecodeLinkValue(t *testing.T) { value := LinkValue{ TargetPath: publicPathValue, Type: ReferenceStaticType{ - Authorized: false, - Type: PrimitiveStaticTypeBool, + Authorized: false, + BorrowedType: PrimitiveStaticTypeBool, }, } diff --git a/runtime/interpreter/interpreter.go b/runtime/interpreter/interpreter.go index 6a90095495..2681220f30 100644 --- a/runtime/interpreter/interpreter.go +++ b/runtime/interpreter/interpreter.go @@ -3293,8 +3293,8 @@ var runtimeTypeConstructors = []runtimeTypeConstructor{ return TypeValue{ Type: ReferenceStaticType{ - Authorized: bool(authorizedValue), - Type: typeValue.Type, + Authorized: bool(authorizedValue), + BorrowedType: typeValue.Type, }, } }, @@ -3678,30 +3678,32 @@ func (interpreter *Interpreter) IsSubTypeOfSemaType(subType StaticType, superTyp return true } - switch staticType := subType.(type) { + switch subType := subType.(type) { case OptionalStaticType: - if typedSuperType, ok := superType.(*sema.OptionalType); ok { - return interpreter.IsSubTypeOfSemaType(staticType.Type, typedSuperType.Type) + if superType, ok := superType.(*sema.OptionalType); ok { + return interpreter.IsSubTypeOfSemaType(subType.Type, superType.Type) } switch superType { case sema.AnyStructType, sema.AnyResourceType: - return interpreter.IsSubTypeOfSemaType(staticType.Type, superType) + return interpreter.IsSubTypeOfSemaType(subType.Type, superType) } case ReferenceStaticType: - if typedSuperType, ok := superType.(*sema.ReferenceType); ok { + if superType, ok := superType.(*sema.ReferenceType); ok { // First, check that the static type of the referenced value // is a subtype of the super type - if staticType.InnerType != nil && !interpreter.IsSubTypeOfSemaType(staticType.InnerType, typedSuperType.Type) { + if subType.ReferencedType == nil || + !interpreter.IsSubTypeOfSemaType(subType.ReferencedType, superType.Type) { + return false } // If the reference value is authorized it may be downcasted - authorized := staticType.Authorized + authorized := subType.Authorized if authorized { return true @@ -3710,17 +3712,14 @@ func (interpreter *Interpreter) IsSubTypeOfSemaType(subType StaticType, superTyp // If the reference value is not authorized, // it may not be down-casted - var borrowType sema.Type - if staticType.Type != nil { - borrowType = interpreter.MustConvertStaticToSemaType(staticType.Type) - } + borrowType := interpreter.MustConvertStaticToSemaType(subType.BorrowedType) return sema.IsSubType( &sema.ReferenceType{ Authorized: authorized, Type: borrowType, }, - typedSuperType, + superType, ) } diff --git a/runtime/interpreter/statictype.go b/runtime/interpreter/statictype.go index aa79ee68cf..d7d0936140 100644 --- a/runtime/interpreter/statictype.go +++ b/runtime/interpreter/statictype.go @@ -279,9 +279,9 @@ outer: // ReferenceStaticType type ReferenceStaticType struct { - Authorized bool - Type StaticType - InnerType StaticType + Authorized bool + BorrowedType StaticType + ReferencedType StaticType } var _ StaticType = ReferenceStaticType{} @@ -294,7 +294,7 @@ func (t ReferenceStaticType) String() string { auth = "auth " } - return fmt.Sprintf("%s&%s", auth, t.Type) + return fmt.Sprintf("%s&%s", auth, t.BorrowedType) } func (t ReferenceStaticType) Equal(other StaticType) bool { @@ -304,7 +304,7 @@ func (t ReferenceStaticType) Equal(other StaticType) bool { } return t.Authorized == otherReferenceType.Authorized && - t.Type.Equal(otherReferenceType.Type) + t.BorrowedType.Equal(otherReferenceType.BorrowedType) } // CapabilityStaticType @@ -378,7 +378,7 @@ func ConvertSemaToStaticType(t sema.Type) StaticType { } case *sema.ReferenceType: - return ConvertSemaReferenceTyoeToStaticReferenceType(t) + return ConvertSemaReferenceTypeToStaticReferenceType(t) case *sema.CapabilityType: result := CapabilityStaticType{} @@ -425,10 +425,10 @@ func ConvertSemaDictionaryTypeToStaticDictionaryType(t *sema.DictionaryType) Dic } } -func ConvertSemaReferenceTyoeToStaticReferenceType(t *sema.ReferenceType) ReferenceStaticType { +func ConvertSemaReferenceTypeToStaticReferenceType(t *sema.ReferenceType) ReferenceStaticType { return ReferenceStaticType{ - Authorized: t.Authorized, - Type: ConvertSemaToStaticType(t.Type), + Authorized: t.Authorized, + BorrowedType: ConvertSemaToStaticType(t.Type), } } @@ -498,7 +498,7 @@ func ConvertStaticToSemaType( }, err case ReferenceStaticType: - ty, err := ConvertStaticToSemaType(t.Type, getInterface, getComposite) + ty, err := ConvertStaticToSemaType(t.BorrowedType, getInterface, getComposite) return &sema.ReferenceType{ Authorized: t.Authorized, Type: ty, diff --git a/runtime/interpreter/statictype_test.go b/runtime/interpreter/statictype_test.go index 47cfe06cf0..ec6b35d1a3 100644 --- a/runtime/interpreter/statictype_test.go +++ b/runtime/interpreter/statictype_test.go @@ -89,7 +89,7 @@ func TestCapabilityStaticType_Equal(t *testing.T) { BorrowType: PrimitiveStaticTypeString, }.Equal( ReferenceStaticType{ - Type: PrimitiveStaticTypeString, + BorrowedType: PrimitiveStaticTypeString, }, ), ) @@ -106,12 +106,12 @@ func TestReferenceStaticType_Equal(t *testing.T) { require.True(t, ReferenceStaticType{ - Authorized: false, - Type: PrimitiveStaticTypeString, + Authorized: false, + BorrowedType: PrimitiveStaticTypeString, }.Equal( ReferenceStaticType{ - Authorized: false, - Type: PrimitiveStaticTypeString, + Authorized: false, + BorrowedType: PrimitiveStaticTypeString, }, ), ) @@ -123,12 +123,12 @@ func TestReferenceStaticType_Equal(t *testing.T) { require.False(t, ReferenceStaticType{ - Authorized: false, - Type: PrimitiveStaticTypeInt, + Authorized: false, + BorrowedType: PrimitiveStaticTypeInt, }.Equal( ReferenceStaticType{ - Authorized: false, - Type: PrimitiveStaticTypeString, + Authorized: false, + BorrowedType: PrimitiveStaticTypeString, }, ), ) @@ -140,12 +140,12 @@ func TestReferenceStaticType_Equal(t *testing.T) { require.False(t, ReferenceStaticType{ - Authorized: false, - Type: PrimitiveStaticTypeInt, + Authorized: false, + BorrowedType: PrimitiveStaticTypeInt, }.Equal( ReferenceStaticType{ - Authorized: true, - Type: PrimitiveStaticTypeInt, + Authorized: true, + BorrowedType: PrimitiveStaticTypeInt, }, ), ) @@ -157,7 +157,7 @@ func TestReferenceStaticType_Equal(t *testing.T) { require.False(t, ReferenceStaticType{ - Type: PrimitiveStaticTypeString, + BorrowedType: PrimitiveStaticTypeString, }.Equal( CapabilityStaticType{ BorrowType: PrimitiveStaticTypeString, @@ -936,7 +936,7 @@ func TestRestrictedStaticType_Equal(t *testing.T) { }, }).Equal( ReferenceStaticType{ - Type: PrimitiveStaticTypeInt, + BorrowedType: PrimitiveStaticTypeInt, }, ), ) diff --git a/runtime/interpreter/value.go b/runtime/interpreter/value.go index e2824a0674..75ad246f87 100644 --- a/runtime/interpreter/value.go +++ b/runtime/interpreter/value.go @@ -41,8 +41,8 @@ import ( type TypeConformanceResults map[typeConformanceResultEntry]bool type typeConformanceResultEntry struct { - EphemeralReferenceValue *EphemeralReferenceValue - EphemeralReferenceDynamicType ReferenceStaticType + EphemeralReferenceValue *EphemeralReferenceValue + EphemeralReferenceType ReferenceStaticType } // SeenReferences is a set of seen references. @@ -14236,20 +14236,15 @@ func (v *StorageReferenceValue) DynamicType(interpreter *Interpreter, seenRefere } func (v *StorageReferenceValue) StaticType(inter *Interpreter) StaticType { - var borrowedType StaticType - if v.BorrowedType != nil { - borrowedType = ConvertSemaToStaticType(v.BorrowedType) - } - referencedValue, err := v.dereference(inter, ReturnEmptyLocationRange) if err != nil { panic(err) } return ReferenceStaticType{ - Authorized: v.Authorized, - Type: borrowedType, - InnerType: (*referencedValue).StaticType(inter), + Authorized: v.Authorized, + BorrowedType: ConvertSemaToStaticType(v.BorrowedType), + ReferencedType: (*referencedValue).StaticType(inter), } } @@ -14455,11 +14450,11 @@ func (v *StorageReferenceValue) ConformsToStaticType( return false } - if refType.Type == nil { + if refType.BorrowedType == nil { if v.BorrowedType != nil { return false } - } else if !refType.Type.Equal(ConvertSemaToStaticType(v.BorrowedType)) { + } else if !refType.BorrowedType.Equal(ConvertSemaToStaticType(v.BorrowedType)) { return false } @@ -14471,7 +14466,7 @@ func (v *StorageReferenceValue) ConformsToStaticType( return (*referencedValue).ConformsToStaticType( interpreter, getLocationRange, - refType.InnerType, + refType.ReferencedType, results, ) } @@ -14580,20 +14575,15 @@ func (v *EphemeralReferenceValue) DynamicType(interpreter *Interpreter, seenRefe } func (v *EphemeralReferenceValue) StaticType(inter *Interpreter) StaticType { - var borrowedType StaticType - if v.BorrowedType != nil { - borrowedType = ConvertSemaToStaticType(v.BorrowedType) - } - referencedValue := v.ReferencedValue(inter, ReturnEmptyLocationRange) if referencedValue == nil { panic(DereferenceError{}) } return ReferenceStaticType{ - Authorized: v.Authorized, - Type: borrowedType, - InnerType: (*referencedValue).StaticType(inter), + Authorized: v.Authorized, + BorrowedType: ConvertSemaToStaticType(v.BorrowedType), + ReferencedType: (*referencedValue).StaticType(inter), } } @@ -14789,11 +14779,11 @@ func (v *EphemeralReferenceValue) ConformsToStaticType( return false } - if refType.Type == nil { + if refType.BorrowedType == nil { if v.BorrowedType != nil { return false } - } else if !refType.Type.Equal(ConvertSemaToStaticType(v.BorrowedType)) { + } else if !refType.BorrowedType.Equal(ConvertSemaToStaticType(v.BorrowedType)) { return false } @@ -14803,8 +14793,8 @@ func (v *EphemeralReferenceValue) ConformsToStaticType( } entry := typeConformanceResultEntry{ - EphemeralReferenceValue: v, - EphemeralReferenceDynamicType: refType, + EphemeralReferenceValue: v, + EphemeralReferenceType: refType, } if result, contains := results[entry]; contains { @@ -14818,7 +14808,7 @@ func (v *EphemeralReferenceValue) ConformsToStaticType( result := (*referencedValue).ConformsToStaticType( interpreter, getLocationRange, - refType.InnerType, + refType.ReferencedType, results, ) diff --git a/runtime/tests/interpreter/metatype_test.go b/runtime/tests/interpreter/metatype_test.go index ad7b7c3a41..4f19e8eca4 100644 --- a/runtime/tests/interpreter/metatype_test.go +++ b/runtime/tests/interpreter/metatype_test.go @@ -631,8 +631,8 @@ func TestInterpretGetType(t *testing.T) { result: interpreter.TypeValue{ Type: interpreter.OptionalStaticType{ Type: interpreter.ReferenceStaticType{ - Authorized: true, - Type: interpreter.PrimitiveStaticTypeInt, + Authorized: true, + BorrowedType: interpreter.PrimitiveStaticTypeInt, }, }, }, @@ -652,8 +652,8 @@ func TestInterpretGetType(t *testing.T) { result: interpreter.TypeValue{ Type: interpreter.OptionalStaticType{ Type: interpreter.ReferenceStaticType{ - Authorized: true, - Type: interpreter.PrimitiveStaticTypeInt, + Authorized: true, + BorrowedType: interpreter.PrimitiveStaticTypeInt, }, }, }, diff --git a/runtime/tests/interpreter/runtimetype_test.go b/runtime/tests/interpreter/runtimetype_test.go index 16fdb89bb8..a0cc573206 100644 --- a/runtime/tests/interpreter/runtimetype_test.go +++ b/runtime/tests/interpreter/runtimetype_test.go @@ -509,7 +509,7 @@ func TestInterpretReferenceType(t *testing.T) { assert.Equal(t, interpreter.TypeValue{ Type: interpreter.ReferenceStaticType{ - Type: interpreter.CompositeStaticType{ + BorrowedType: interpreter.CompositeStaticType{ QualifiedIdentifier: "R", Location: utils.TestLocation, TypeID: "S.test.R", @@ -523,8 +523,8 @@ func TestInterpretReferenceType(t *testing.T) { assert.Equal(t, interpreter.TypeValue{ Type: interpreter.ReferenceStaticType{ - Type: interpreter.PrimitiveStaticTypeString, - Authorized: false, + BorrowedType: interpreter.PrimitiveStaticTypeString, + Authorized: false, }, }, inter.Globals["b"].GetValue(), @@ -533,7 +533,7 @@ func TestInterpretReferenceType(t *testing.T) { assert.Equal(t, interpreter.TypeValue{ Type: interpreter.ReferenceStaticType{ - Type: interpreter.CompositeStaticType{ + BorrowedType: interpreter.CompositeStaticType{ QualifiedIdentifier: "S", Location: utils.TestLocation, TypeID: "S.test.S", @@ -704,8 +704,8 @@ func TestInterpretCapabilityType(t *testing.T) { interpreter.TypeValue{ Type: interpreter.CapabilityStaticType{ BorrowType: interpreter.ReferenceStaticType{ - Type: interpreter.PrimitiveStaticTypeString, - Authorized: false, + BorrowedType: interpreter.PrimitiveStaticTypeString, + Authorized: false, }, }, }, @@ -716,8 +716,8 @@ func TestInterpretCapabilityType(t *testing.T) { interpreter.TypeValue{ Type: interpreter.CapabilityStaticType{ BorrowType: interpreter.ReferenceStaticType{ - Type: interpreter.PrimitiveStaticTypeInt, - Authorized: false, + BorrowedType: interpreter.PrimitiveStaticTypeInt, + Authorized: false, }, }, }, @@ -728,7 +728,7 @@ func TestInterpretCapabilityType(t *testing.T) { interpreter.TypeValue{ Type: interpreter.CapabilityStaticType{ BorrowType: interpreter.ReferenceStaticType{ - Type: interpreter.CompositeStaticType{ + BorrowedType: interpreter.CompositeStaticType{ QualifiedIdentifier: "R", Location: utils.TestLocation, TypeID: "S.test.R", diff --git a/runtime/tests/interpreter/values_test.go b/runtime/tests/interpreter/values_test.go index 6f97b7942e..6ba4597ec6 100644 --- a/runtime/tests/interpreter/values_test.go +++ b/runtime/tests/interpreter/values_test.go @@ -1252,8 +1252,8 @@ func randomStorableValue(inter *interpreter.Interpreter, currentDepth int) inter Address: randomAddressValue(), Path: randomPathValue(), BorrowType: interpreter.ReferenceStaticType{ - Authorized: false, - Type: interpreter.PrimitiveStaticTypeAnyStruct, + Authorized: false, + BorrowedType: interpreter.PrimitiveStaticTypeAnyStruct, }, } case Some: