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: custom boolean types in conditional statements #2147

Merged
merged 12 commits into from
Jun 19, 2024
4 changes: 4 additions & 0 deletions gnovm/cmd/gno/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ func TestRunApp(t *testing.T) {
args: []string{"run", "-debug-addr", "invalidhost:17538", "../../tests/integ/debugger/sample.gno"},
errShouldContain: "listen tcp: lookup invalidhost",
},
{
args: []string{"run", "../../tests/integ/invalid_assign/main.gno"},
recoverShouldContain: "cannot use bool as main.C without explicit conversion",
},
// TODO: a test file
// TODO: args
// TODO: nativeLibs VS stdlibs
Expand Down
31 changes: 27 additions & 4 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -1701,13 +1701,11 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node {

// TRANS_LEAVE -----------------------
case *ForStmt:
// Cond consts become bool *ConstExprs.
checkOrConvertType(store, last, &n.Cond, BoolType, false)
checkOrConvertBoolType(store, last, n.Cond)

// TRANS_LEAVE -----------------------
case *IfStmt:
// Cond consts become bool *ConstExprs.
checkOrConvertType(store, last, &n.Cond, BoolType, false)
checkOrConvertBoolType(store, last, n.Cond)

// TRANS_LEAVE -----------------------
case *RangeStmt:
Expand Down Expand Up @@ -2949,6 +2947,31 @@ func checkIntegerType(xt Type) {
}
}

// like checkOrConvertType() but for bool type.
func checkOrConvertBoolType(store Store, last BlockNode, x Expr) {
if cx, ok := x.(*ConstExpr); ok {
convertConst(store, last, cx, BoolType)
} else if nx, ok := (x).(*NameExpr); ok {
xt := evalStaticTypeOf(store, last, Expr(nx))
if dxt, ok := xt.(*DeclaredType); ok {
checkType(dxt.Base, BoolType, false)
} else {
panic(fmt.Sprintf("expected declared type, but got %v", xt.Kind()))
}
} else if x != nil {
nettijoe96 marked this conversation as resolved.
Show resolved Hide resolved
xt := evalStaticTypeOf(store, last, x)
checkBoolType(xt)
}
thehowl marked this conversation as resolved.
Show resolved Hide resolved
}

func checkBoolType(xt Type) {
if xt.Kind() != BoolKind {
panic(fmt.Sprintf(
"expected bool type, but got %v",
xt.Kind()))
}
}

// predefineNow() pre-defines (with empty placeholders) all
// declaration names, and then preprocesses all type/value decls, and
// partially processes func decls.
Expand Down
25 changes: 25 additions & 0 deletions gnovm/tests/files/for19.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

func main() {
v := T
i := 0
for v {
if i > 2 {
break
}
println(i)
i++
}
}

type C bool

const (
F C = false
T C = true
)

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

func main() {
v := T
if v {
println("true")
}
}

type C bool

const (
F C = false
T C = true
)

// Output:
// true
16 changes: 16 additions & 0 deletions gnovm/tests/integ/invalid_assign/main.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// This should result in a compilation error for assigning a bool to a custom bool type without conversion

package main

func main() {
b := true
var v C = b
fmt.Println(v)
}

type C bool

const (
F C = false
T C = true
)