From 87f4a7f7d4cd8a401128b1947823d9bf109703b3 Mon Sep 17 00:00:00 2001 From: deelawn Date: Sun, 17 Dec 2023 10:16:24 +0100 Subject: [PATCH 1/2] boolean operations must have boolean operands --- gnovm/pkg/gnolang/preprocess.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 1a1edc41222..2e428cfd344 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -755,6 +755,13 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { resn := Preprocess(store, last, n2) return resn, TRANS_CONTINUE } + + // Left and right hand expressions must evaluate to a boolean typed value if + // the operation is a logical AND or OR. + if (n.Op == LAND || n.Op == LOR) && (lt.Kind() != BoolKind || rt.Kind() != BoolKind) { + panic("operands of boolean operators must evaluate to boolean typed values") + } + // General case. lcx, lic := n.Left.(*ConstExpr) rcx, ric := n.Right.(*ConstExpr) From 207764a20a56c8570d7089d8995d1c85df88fc3a Mon Sep 17 00:00:00 2001 From: deelawn Date: Sun, 17 Dec 2023 12:27:33 +0100 Subject: [PATCH 2/2] added tests --- gnovm/tests/files/bool6.gno | 12 ++++++++++++ gnovm/tests/files/bool7.gno | 16 ++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 gnovm/tests/files/bool6.gno create mode 100644 gnovm/tests/files/bool7.gno diff --git a/gnovm/tests/files/bool6.gno b/gnovm/tests/files/bool6.gno new file mode 100644 index 00000000000..ad4d832036c --- /dev/null +++ b/gnovm/tests/files/bool6.gno @@ -0,0 +1,12 @@ +package main + +func main() { + println(X()) +} + +func X() string { + return "hello" || "world" +} + +// Error: +// main/files/bool6.gno:8: operands of boolean operators must evaluate to boolean typed values diff --git a/gnovm/tests/files/bool7.gno b/gnovm/tests/files/bool7.gno new file mode 100644 index 00000000000..ba8be09dc7c --- /dev/null +++ b/gnovm/tests/files/bool7.gno @@ -0,0 +1,16 @@ +package main + +// Ensure, when comparing evaluated boolean operand types, that the kinds produced +// are the same when one operand is typed and the other is untyped. +func main() { + println(boolAndTrue(true)) + println(boolAndTrue(false)) +} + +func boolAndTrue(b bool) bool { + return b && true +} + +// Output: +// true +// false