Skip to content

Commit

Permalink
detect other unsupported recursive types. requires making all types h…
Browse files Browse the repository at this point in the history
…ashable
  • Loading branch information
turbolent committed Jun 7, 2022
1 parent fbf3df5 commit a36cd96
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 25 deletions.
22 changes: 16 additions & 6 deletions encoding/json/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ func prepareComposite(kind, id string, fieldTypes []cadence.Field, fields []cade
nonFunctionFieldTypes := make([]cadence.Field, 0)

for _, field := range fieldTypes {
if _, ok := field.Type.(cadence.FunctionType); !ok {
if _, ok := field.Type.(*cadence.FunctionType); !ok {
nonFunctionFieldTypes = append(nonFunctionFieldTypes, field)
}
}
Expand Down Expand Up @@ -661,11 +661,21 @@ func prepareInitializers(initializerTypes [][]cadence.Parameter, results typeRes

func prepareType(typ cadence.Type, results typeResults) jsonValue {

switch typ := typ.(type) {
var supportedRecursiveType bool
switch typ.(type) {
case cadence.CompositeType, cadence.InterfaceType:
if _, ok := results[typ]; ok {
return typ.ID()
supportedRecursiveType = true
}

if _, ok := results[typ]; ok {
if !supportedRecursiveType {
panic(fmt.Errorf("failed to prepare type: unsupported recursive type: %T", typ))
}

return typ.ID()
}

if supportedRecursiveType {
results[typ] = struct{}{}
}

Expand Down Expand Up @@ -801,7 +811,7 @@ func prepareType(typ cadence.Type, results typeResults) jsonValue {
Fields: prepareFields(typ.Fields, results),
Initializers: prepareInitializers(typ.Initializers, results),
}
case cadence.FunctionType:
case *cadence.FunctionType:
return jsonFunctionType{
Kind: "Function",
TypeID: typ.ID(),
Expand All @@ -814,7 +824,7 @@ func prepareType(typ cadence.Type, results typeResults) jsonValue {
Authorized: typ.Authorized,
Type: prepareType(typ.Type, results),
}
case cadence.RestrictedType:
case *cadence.RestrictedType:
restrictions := make([]jsonValue, 0)
for _, restriction := range typ.Restrictions {
restrictions = append(restrictions, prepareType(restriction, results))
Expand Down
8 changes: 4 additions & 4 deletions encoding/json/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1481,12 +1481,12 @@ func TestEncodeType(t *testing.T) {
testEncodeAndDecode(
t,
cadence.TypeValue{
StaticType: cadence.FunctionType{
StaticType: (&cadence.FunctionType{
Parameters: []cadence.Parameter{
{Label: "qux", Identifier: "baz", Type: cadence.StringType{}},
},
ReturnType: cadence.IntType{},
}.WithID("Foo"),
}).WithID("Foo"),
},
`{"type":"Type","value":{"staticType":
{
Expand Down Expand Up @@ -1521,12 +1521,12 @@ func TestEncodeType(t *testing.T) {
testEncodeAndDecode(
t,
cadence.TypeValue{
StaticType: cadence.RestrictedType{
StaticType: (&cadence.RestrictedType{
Restrictions: []cadence.Type{
cadence.StringType{},
},
Type: cadence.IntType{},
}.WithID("Int{String}"),
}).WithID("Int{String}"),
},
`{"type":"Type","value":{"staticType":
{
Expand Down
4 changes: 2 additions & 2 deletions runtime/convertTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ func exportRestrictedType(
gauge common.MemoryGauge,
t *sema.RestrictedType,
results map[sema.TypeID]cadence.Type,
) cadence.RestrictedType {
) *cadence.RestrictedType {

convertedType := ExportMeteredType(gauge, t.Type, results)

Expand Down Expand Up @@ -745,7 +745,7 @@ func ImportType(memoryGauge common.MemoryGauge, t cadence.Type) interpreter.Stat
ImportType(memoryGauge, t.Type),
nil,
)
case cadence.RestrictedType:
case *cadence.RestrictedType:
restrictions := make([]interpreter.InterfaceStaticType, 0, len(t.Restrictions))
for _, restriction := range t.Restrictions {
intf, ok := restriction.(cadence.InterfaceType)
Expand Down
6 changes: 3 additions & 3 deletions runtime/convertValues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,7 @@ func TestImportRuntimeType(t *testing.T) {
},
{
label: "RestrictedType",
actual: cadence.RestrictedType{
actual: &cadence.RestrictedType{
Type: &cadence.StructType{
Location: TestLocation,
QualifiedIdentifier: "S",
Expand Down Expand Up @@ -1769,7 +1769,7 @@ func TestExportTypeValue(t *testing.T) {

assert.Equal(t,
cadence.TypeValue{
StaticType: cadence.RestrictedType{
StaticType: (&cadence.RestrictedType{
Type: &cadence.StructType{
QualifiedIdentifier: "S",
Location: TestLocation,
Expand All @@ -1782,7 +1782,7 @@ func TestExportTypeValue(t *testing.T) {
Fields: []cadence.Field{},
},
},
}.WithID("S.test.S{S.test.SI}"),
}).WithID("S.test.S{S.test.SI}"),
},
actual,
)
Expand Down
18 changes: 9 additions & 9 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1364,8 +1364,8 @@ func NewFunctionType(
typeID string,
parameters []Parameter,
returnType Type,
) FunctionType {
return FunctionType{
) *FunctionType {
return &FunctionType{
typeID: typeID,
Parameters: parameters,
ReturnType: returnType,
Expand All @@ -1377,18 +1377,18 @@ func NewMeteredFunctionType(
typeID string,
parameters []Parameter,
returnType Type,
) FunctionType {
) *FunctionType {
common.UseMemory(gauge, common.CadenceFunctionTypeMemoryUsage)
return NewFunctionType(typeID, parameters, returnType)
}

func (FunctionType) isType() {}
func (*FunctionType) isType() {}

func (t FunctionType) ID() string {
func (t *FunctionType) ID() string {
return t.typeID
}

func (t FunctionType) WithID(id string) FunctionType {
func (t *FunctionType) WithID(id string) *FunctionType {
t.typeID = id
return t
}
Expand Down Expand Up @@ -1459,13 +1459,13 @@ func NewMeteredRestrictedType(
return NewRestrictedType(typeID, typ, restrictions)
}

func (RestrictedType) isType() {}
func (*RestrictedType) isType() {}

func (t RestrictedType) ID() string {
func (t *RestrictedType) ID() string {
return t.typeID
}

func (t RestrictedType) WithID(id string) RestrictedType {
func (t *RestrictedType) WithID(id string) *RestrictedType {
t.typeID = id
return t
}
Expand Down
2 changes: 1 addition & 1 deletion types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestType_ID(t *testing.T) {
"S.test.FooI",
},
{
RestrictedType{}.WithID("S.test.Foo{S.test.FooI}"),
(&RestrictedType{}).WithID("S.test.Foo{S.test.FooI}"),
"S.test.Foo{S.test.FooI}",
},
}
Expand Down

0 comments on commit a36cd96

Please sign in to comment.