Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make CompositeType.Members field an ordered map #581

Merged
merged 15 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions runtime/common/orderedmap/orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (om *KeyTypeValueTypeOrderedMap) Newest() *KeyTypeValueTypePair {
// Foreach iterates over the entries of the map in the insertion order, and invokes
// the provided function for each key-value pair.
func (om *KeyTypeValueTypeOrderedMap) Foreach(f func(key KeyType, value ValueType)) {
for pair := om.Oldest(); pair != nil; pair = pair.next() {
for pair := om.Oldest(); pair != nil; pair = pair.Next() {
f(pair.Key, pair.Value)
}
}
Expand All @@ -124,13 +124,13 @@ type KeyTypeValueTypePair struct {
element *list.Element
}

// next returns a pointer to the next pair.
func (p *KeyTypeValueTypePair) next() *KeyTypeValueTypePair {
// Next returns a pointer to the next pair.
func (p *KeyTypeValueTypePair) Next() *KeyTypeValueTypePair {
return listElementToKeyTypeValueTypePair(p.element.Next())
}

// prev returns a pointer to the previous pair.
func (p *KeyTypeValueTypePair) prev() *KeyTypeValueTypePair {
// Prev returns a pointer to the previous pair.
func (p *KeyTypeValueTypePair) Prev() *KeyTypeValueTypePair {
return listElementToKeyTypeValueTypePair(p.element.Prev())
}

Expand Down
10 changes: 5 additions & 5 deletions runtime/common/orderedmap/string_fruit_orderedmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (om *StringFruitOrderedMap) Newest() *StringFruitPair {
// Foreach iterates over the entries of the map in the insertion order, and invokes
// the provided function for each key-value pair.
func (om *StringFruitOrderedMap) Foreach(f func(key string, value *Fruit)) {
for pair := om.Oldest(); pair != nil; pair = pair.next() {
for pair := om.Oldest(); pair != nil; pair = pair.Next() {
f(pair.Key, pair.Value)
}
}
Expand All @@ -121,13 +121,13 @@ type StringFruitPair struct {
element *list.Element
}

// next returns a pointer to the next pair.
func (p *StringFruitPair) next() *StringFruitPair {
// Next returns a pointer to the next pair.
func (p *StringFruitPair) Next() *StringFruitPair {
return listElementToStringFruitPair(p.element.Next())
}

// prev returns a pointer to the previous pair.
func (p *StringFruitPair) prev() *StringFruitPair {
// Prev returns a pointer to the previous pair.
func (p *StringFruitPair) Prev() *StringFruitPair {
return listElementToStringFruitPair(p.element.Prev())
}

Expand Down
8 changes: 4 additions & 4 deletions runtime/convertTypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,9 @@ func exportCompositeType(t *sema.CompositeType, results map[sema.TypeID]cadence.
fieldMembers := make([]*sema.Member, 0, len(t.Fields))

for _, identifier := range t.Fields {
member := t.Members[identifier]
member, ok := t.Members.Get(identifier)

if member.IgnoreInSerialization {
if !ok || member.IgnoreInSerialization {
Copy link
Member

@turbolent turbolent Feb 8, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there ever a case where the lookup would fail? Is it rather an implementation error when the lookup fails?

Same question below.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIU Fields are a subset of Members. So the lookup should always succeed.

Maybe a panic would be a good idea.

continue
}

Expand Down Expand Up @@ -256,9 +256,9 @@ func exportInterfaceType(t *sema.InterfaceType, results map[sema.TypeID]cadence.
fieldMembers := make([]*sema.Member, 0, len(t.Fields))

for _, identifier := range t.Fields {
member := t.Members[identifier]
member, ok := t.Members.Get(identifier)

if member.IgnoreInSerialization {
if !ok || member.IgnoreInSerialization {
continue
}

Expand Down
6 changes: 3 additions & 3 deletions runtime/convertTypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ func TestExportRecursiveType(t *testing.T) {
Location: utils.TestLocation,
Identifier: "Foo",
Kind: common.CompositeKindResource,
Members: map[string]*sema.Member{},
Members: sema.NewStringMemberOrderedMap(),
Fields: []string{"foo"},
}

ty.Members["foo"] = &sema.Member{
ty.Members.Set("foo", &sema.Member{
ContainerType: ty,
Access: ast.AccessNotSpecified,
Identifier: ast.Identifier{Identifier: "foo"},
// NOTE: recursive type
TypeAnnotation: sema.NewTypeAnnotation(ty),
DeclarationKind: common.DeclarationKindField,
VariableKind: ast.VariableKindVariable,
}
})

expected := &cadence.ResourceType{
Location: utils.TestLocation,
Expand Down
1 change: 1 addition & 0 deletions runtime/interpreter/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,7 @@ func TestBlockValue(t *testing.T) {
ID: NewArrayValueUnownedNonCopying(),
Timestamp: 5.0,
}

// static type test
var actualTs = block.Timestamp
const expectedTs UFix64Value = 5.0
Expand Down
6 changes: 4 additions & 2 deletions runtime/sema/check_assignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (checker *Checker) accessedSelfMember(expression ast.Expression) *Member {
return nil
}

var members map[string]*Member
var members *StringMemberOrderedMap
switch containerType := variable.Type.(type) {
case *CompositeType:
members = containerType.Members
Expand All @@ -139,7 +139,9 @@ func (checker *Checker) accessedSelfMember(expression ast.Expression) *Member {

fieldName := memberExpression.Identifier.Identifier

return members[fieldName]
// caller handles the non-existing fieldNames
member, _ := members.Get(fieldName)
return member
}

func (checker *Checker) visitAssignmentValueType(
Expand Down
Loading