Skip to content

Commit

Permalink
fix(gnovm): in op_binary, return typed booleans where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
thehowl committed Dec 8, 2024
1 parent 918c9ab commit 1fb863e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
35 changes: 17 additions & 18 deletions gnovm/pkg/gnolang/op_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ func (m *Machine) doOpLand() {
lv.SetBool(lv.GetBool() && rv.GetBool())
}

// setMaybeUntypedBool sets lv to b; it will be untyped if both lv and rv are
// untyped; otherwise, it will be a typed bool.
func setMaybeUntypedBool(lv, rv *TypedValue, b bool) {
tp := BoolType
if isUntyped(lv.T) && isUntyped(rv.T) {
tp = UntypedBoolType
}
lv.T, lv.V = tp, nil
lv.SetBool(b)
}

func (m *Machine) doOpEql() {
m.PopExpr()

Expand All @@ -82,9 +93,7 @@ func (m *Machine) doOpEql() {
}
// set result in lv.
res := isEql(m.Store, lv, rv)
lv.T = UntypedBoolType
lv.V = nil
lv.SetBool(res)
setMaybeUntypedBool(lv, rv, res)
}

func (m *Machine) doOpNeq() {
Expand All @@ -99,9 +108,7 @@ func (m *Machine) doOpNeq() {

// set result in lv.
res := !isEql(m.Store, lv, rv)
lv.T = UntypedBoolType
lv.V = nil
lv.SetBool(res)
setMaybeUntypedBool(lv, rv, res)
}

func (m *Machine) doOpLss() {
Expand All @@ -116,9 +123,7 @@ func (m *Machine) doOpLss() {

// set the result in lv.
res := isLss(lv, rv)
lv.T = UntypedBoolType
lv.V = nil
lv.SetBool(res)
setMaybeUntypedBool(lv, rv, res)
}

func (m *Machine) doOpLeq() {
Expand All @@ -133,9 +138,7 @@ func (m *Machine) doOpLeq() {

// set the result in lv.
res := isLeq(lv, rv)
lv.T = UntypedBoolType
lv.V = nil
lv.SetBool(res)
setMaybeUntypedBool(lv, rv, res)
}

func (m *Machine) doOpGtr() {
Expand All @@ -150,9 +153,7 @@ func (m *Machine) doOpGtr() {

// set the result in lv.
res := isGtr(lv, rv)
lv.T = UntypedBoolType
lv.V = nil
lv.SetBool(res)
setMaybeUntypedBool(lv, rv, res)
}

func (m *Machine) doOpGeq() {
Expand All @@ -167,9 +168,7 @@ func (m *Machine) doOpGeq() {

// set the result in lv.
res := isGeq(lv, rv)
lv.T = UntypedBoolType
lv.V = nil
lv.SetBool(res)
setMaybeUntypedBool(lv, rv, res)
}

func (m *Machine) doOpAdd() {
Expand Down
17 changes: 17 additions & 0 deletions gnovm/tests/files/bool8.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

// results from comparisons should not be untyped bools

var a interface{} = true

func main() {
buf := "hello="
isEqual(a, (buf[len(buf)-1] == '='))
}

func isEqual(v1, v2 interface{}) {
println("v1 == v2", v1 == v2)
}

// Output:
// v1 == v2 true

0 comments on commit 1fb863e

Please sign in to comment.