Skip to content

Commit

Permalink
fix the export of type values with restricted static types
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent committed Jan 22, 2021
1 parent ec4293e commit 1e0e4e6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
45 changes: 45 additions & 0 deletions runtime/convertValues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,51 @@ func TestExportTypeValue(t *testing.T) {

assert.Equal(t, expected, actual)
})

t.Run("with restricted static type", func(t *testing.T) {

t.Parallel()

program, err := parser2.ParseProgram(`
pub struct interface SI {}
pub struct S: SI {}
`)
require.NoError(t, err)

checker, err := sema.NewChecker(program, utils.TestLocation)
require.NoError(t, err)

err = checker.Check()
require.NoError(t, err)

inter, err := interpreter.NewInterpreter(checker)
require.NoError(t, err)

ty := interpreter.TypeValue{
Type: &interpreter.RestrictedStaticType{
Type: interpreter.CompositeStaticType{
Location: utils.TestLocation,
QualifiedIdentifier: "S",
},
Restrictions: []interpreter.InterfaceStaticType{
{
Location: utils.TestLocation,
QualifiedIdentifier: "SI",
},
},
},
}

assert.Equal(t,
cadence.TypeValue{
StaticType: "S.test.S{S.test.SI}",
},
ExportValue(ty, inter),
)
})

}

func TestExportCapabilityValue(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/interpreter/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -1429,7 +1429,7 @@ func (d *Decoder) decodeRestrictedStaticType(v interface{}) (StaticType, error)
restrictions[i] = restriction
}

return RestrictedStaticType{
return &RestrictedStaticType{
Type: restrictedType,
Restrictions: restrictions,
}, nil
Expand Down
4 changes: 2 additions & 2 deletions runtime/interpreter/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ func (e *Encoder) prepareStaticType(t StaticType) (interface{}, error) {
case DictionaryStaticType:
return e.prepareDictionaryStaticType(v)

case RestrictedStaticType:
case *RestrictedStaticType:
return e.prepareRestrictedStaticType(v)

case CapabilityStaticType:
Expand Down Expand Up @@ -1091,7 +1091,7 @@ const (
encodedRestrictedStaticTypeRestrictionsFieldKey uint64 = 1
)

func (e *Encoder) prepareRestrictedStaticType(v RestrictedStaticType) (interface{}, error) {
func (e *Encoder) prepareRestrictedStaticType(v *RestrictedStaticType) (interface{}, error) {
restrictedType, err := e.prepareStaticType(v.Type)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion runtime/interpreter/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3867,7 +3867,7 @@ func TestEncodeDecodeLinkValue(t *testing.T) {
encodeDecodeTest{
value: LinkValue{
TargetPath: publicPathValue,
Type: RestrictedStaticType{
Type: &RestrictedStaticType{
Type: CompositeStaticType{
Location: utils.TestLocation,
QualifiedIdentifier: "S",
Expand Down
13 changes: 9 additions & 4 deletions runtime/interpreter/statictype.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,14 @@ type RestrictedStaticType struct {
Restrictions []InterfaceStaticType
}

func (RestrictedStaticType) IsStaticType() {}
// NOTE: must be pointer receiver, as static types get used in type values,
// which are used as keys in maps when exporting.
// Key types in Go maps must be (transitively) hashable types,
// and slices are not, but `Restrictions` is one.
//
func (*RestrictedStaticType) IsStaticType() {}

func (t RestrictedStaticType) String() string {
func (t *RestrictedStaticType) String() string {
restrictions := make([]string, len(t.Restrictions))

for i, restriction := range t.Restrictions {
Expand Down Expand Up @@ -217,7 +222,7 @@ func ConvertSemaToStaticType(t sema.Type) StaticType {
restrictions[i] = convertToInterfaceStaticType(restriction)
}

return RestrictedStaticType{
return &RestrictedStaticType{
Type: ConvertSemaToStaticType(t.Type),
Restrictions: restrictions,
}
Expand Down Expand Up @@ -288,7 +293,7 @@ func ConvertStaticToSemaType(
Type: ConvertStaticToSemaType(t.Type, getInterface, getComposite),
}

case RestrictedStaticType:
case *RestrictedStaticType:
restrictions := make([]*sema.InterfaceType, len(t.Restrictions))

for i, restriction := range t.Restrictions {
Expand Down

0 comments on commit 1e0e4e6

Please sign in to comment.