diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index fff9170f6a..b284ba72b6 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -3961,9 +3961,9 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) { g.expr(node.expr) } else if node.expr_type == ast.bool_type && node.typ.is_int() { styp := g.typ(node.typ) - g.write('($styp)(') + g.write('($styp[]){(') g.expr(node.expr) - g.write(')') + g.write(')?1:0}[0]') } else { styp := g.typ(node.typ) if (g.pref.translated || g.file.is_translated) && sym.kind == .function { diff --git a/vlib/v/tests/cast_bool_to_int_test.v b/vlib/v/tests/cast_bool_to_int_test.v index 2158366fdd..a34d20f910 100644 --- a/vlib/v/tests/cast_bool_to_int_test.v +++ b/vlib/v/tests/cast_bool_to_int_test.v @@ -2,6 +2,10 @@ module main fn test_cast_bool_to_int() { i := true + x := int(i) + nx := int(!i) + dump(x) + dump(nx) a := [1, 2, 3] println(a[int(!i)])