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

fix: boolean operations must have boolean operands #1451

Merged
merged 3 commits into from
Dec 18, 2023
Merged
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
7 changes: 7 additions & 0 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,13 @@
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")

Check warning on line 762 in gnovm/pkg/gnolang/preprocess.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/preprocess.go#L762

Added line #L762 was not covered by tests
}

// General case.
lcx, lic := n.Left.(*ConstExpr)
rcx, ric := n.Right.(*ConstExpr)
Expand Down
12 changes: 12 additions & 0 deletions gnovm/tests/files/bool6.gno
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions gnovm/tests/files/bool7.gno
Original file line number Diff line number Diff line change
@@ -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
Loading