Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion gnovm/pkg/gnolang/op_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -510,8 +510,8 @@ func isEql(m *Machine, lv, rv *TypedValue) bool {
case StructKind:
ls := lv.V.(*StructValue)
rs := rv.V.(*StructValue)
lt := baseOf(lv.T).(*StructType)
if debug {
lt := baseOf(lv.T).(*StructType)
rt := baseOf(rv.T).(*StructType)
if lt.TypeID() != rt.TypeID() {
panic("comparison on structs of unequal types")
Expand All @@ -521,6 +521,9 @@ func isEql(m *Machine, lv, rv *TypedValue) bool {
}
}
for i := range ls.Fields {
if lt.Fields[i].Name == blankIdentifier {
continue
}
m.incrCPU(OpCPUEql)
lf := ls.GetPointerToInt(m.Store, i).Deref()
rf := rs.GetPointerToInt(m.Store, i).Deref()
Expand Down
11 changes: 8 additions & 3 deletions gnovm/pkg/gnolang/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -1664,18 +1664,23 @@ func (tv *TypedValue) ComputeMapKey(m *Machine, store Store, omitType bool) (key
sv := tv.V.(*StructValue)
sl := len(sv.Fields)
bz = append(bz, '{')
count := 0
for i := range sl {
if bt.Fields[i].Name == blankIdentifier {
continue
}
fv := fillValueTV(store, &sv.Fields[i])
omitTypes := bt.Fields[i].Type.Kind() != InterfaceKind
mk, isNaN := fv.ComputeMapKey(m, store, omitTypes)
if isNaN {
return "", true
}
bz = binary.AppendUvarint(bz, uint64(len(mk)))
bz = append(bz, mk...)
if i != sl-1 {
if count > 0 {
bz = append(bz, ',')
}
bz = binary.AppendUvarint(bz, uint64(len(mk)))
bz = append(bz, mk...)
count++
}
bz = append(bz, '}')
case *ChanType:
Expand Down
37 changes: 37 additions & 0 deletions gnovm/tests/files/map49.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package main

import "math"

type T struct {
_ int
X int
}

type F struct {
_ float64
X int
}

func main() {
m := map[T]string{T{1, 2}: "a"}
println(m[T{3, 2}])

m[T{3, 2}] = "b"
println(len(m))
println(m[T{1, 2}])

delete(m, T{4, 2})
println(len(m))

f := map[F]string{F{math.NaN(), 2}: "nan"}
println(f[F{math.NaN(), 2}])
println(len(f))
}

// Output:
// a
// 1
// b
// 0
// nan
// 1
21 changes: 21 additions & 0 deletions gnovm/tests/files/types/cmp_struct_h.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

type T struct {
_ int
X int
}

type Outer struct {
Inner T
}

func main() {
println(T{1, 2} == T{3, 2})
println(T{1, 2} == T{1, 3})
println(Outer{T{1, 2}} == Outer{T{3, 2}})
}

// Output:
// true
// false
// true
18 changes: 18 additions & 0 deletions gnovm/tests/files/types/cmp_struct_i.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

type foo struct {
_ []int
}

func main() {
fa := foo{}
fb := foo{}

println(fa == fb)
}

// Error:
// main/cmp_struct_i.gno:11:10-18: main.foo is not comparable

// TypeCheckError:
// main/cmp_struct_i.gno:11:10: invalid operation: fa == fb (struct containing []int cannot be compared)
Loading