From 7185cefe2e091cb1795aef1d3d4c4d938a81778c Mon Sep 17 00:00:00 2001 From: Morgan Date: Wed, 11 Dec 2024 09:05:18 +0100 Subject: [PATCH] fix(gnovm): in op_binary, return typed booleans where appropriate (#3298) bool8.gno was failing, because the result of the `==` expression is an untyped boolean, while the first value is a typed boolean. This PR ensures that if either of the values in a binary expression is typed, we return a typed bool instead of an untyped bool. --------- Co-authored-by: ltzmaxwell --- gnovm/pkg/gnolang/preprocess.go | 4 ++++ gnovm/tests/files/bool8.gno | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 gnovm/tests/files/bool8.gno diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 15f268f6321..4ff182670cd 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -3574,6 +3574,10 @@ func checkOrConvertType(store Store, last BlockNode, n Node, x *Expr, t Type, au checkOrConvertType(store, last, n, &bx.Left, rt, autoNative) checkOrConvertType(store, last, n, &bx.Right, rt, autoNative) } + // this is not a constant expression; the result here should + // always be a BoolType. (in this scenario, we may have some + // UntypedBoolTypes) + t = BoolType default: // do nothing } diff --git a/gnovm/tests/files/bool8.gno b/gnovm/tests/files/bool8.gno new file mode 100644 index 00000000000..9efbbbe6da2 --- /dev/null +++ b/gnovm/tests/files/bool8.gno @@ -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