cgen: fix crash for casting bool to int

pull/13844/head
yuyi98 2022-03-27 22:58:13 +08:00
parent 875ad1f6ea
commit d97dd3f04d
2 changed files with 24 additions and 0 deletions

View File

@ -3771,6 +3771,18 @@ fn (mut g Gen) cast_expr(node ast.CastExpr) {
g.write('))')
} else if sym.kind == .alias && g.table.final_sym(node.typ).kind == .array_fixed {
g.expr(node.expr)
} else if node.expr_type != 0 && g.table.final_sym(node.expr_type).kind == .bool
&& node.typ.is_int() {
styp := g.typ(node.typ)
tmp := g.new_tmp_var()
g.empty_line = true
s := g.go_before_stmt(0)
g.write('$styp $tmp = (')
g.expr(node.expr)
g.writeln(')?1:0;')
g.empty_line = false
g.write(s)
g.write(tmp)
} else {
styp := g.typ(node.typ)
if (g.pref.translated || g.file.is_translated) && sym.kind == .function {

View File

@ -0,0 +1,12 @@
module main
fn test_cast_bool_to_int() {
i := true
a := [1, 2, 3]
println(a[int(!i)])
assert a[int(!i)] == 1
println(a[int(i)])
assert a[int(i)] == 2
}