diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index ea9a80916d1512..92a0665690190e 100644 --- a/vlib/v/gen/c/if.v +++ b/vlib/v/gen/c/if.v @@ -378,6 +378,11 @@ fn (mut g Gen) if_expr(node ast.IfExpr) { no_needs_par = true } } + inside_casting_to_str_old := g.inside_casting_to_str + if !g.inside_casting_to_str && branch.cond is ast.Ident + && g.table.is_interface_var(branch.cond.obj) { + g.inside_casting_to_str = true + } if no_needs_par { g.write('if ') } else { @@ -389,6 +394,7 @@ fn (mut g Gen) if_expr(node ast.IfExpr) { } else { g.writeln(') {') } + g.inside_casting_to_str = inside_casting_to_str_old } } if needs_tmp_var { diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index bbc292f27ed2e3..7ee4f6d4ca3df4 100644 --- a/vlib/v/gen/c/infix.v +++ b/vlib/v/gen/c/infix.v @@ -1055,6 +1055,13 @@ fn (mut g Gen) gen_plain_infix_expr(node ast.InfixExpr) { } if node.left_type.is_ptr() && node.left.is_auto_deref_var() { g.write('*') + } else if !g.inside_casting_to_str && node.left is ast.Ident + && g.table.is_interface_var(node.left.obj) { + inside_casting_to_str_old := g.inside_casting_to_str + g.inside_casting_to_str = true + defer { + g.inside_casting_to_str = inside_casting_to_str_old + } } g.expr(node.left) g.write(' ${node.op.str()} ') diff --git a/vlib/v/tests/interface_var_test.v b/vlib/v/tests/interface_var_test.v new file mode 100644 index 00000000000000..196d5ecdbe7e19 --- /dev/null +++ b/vlib/v/tests/interface_var_test.v @@ -0,0 +1,28 @@ +interface Param {} + +fn test_main() { + param := Param(false) + + match param { + bool { + println(param) // &false + println(param == true) // false + println(param == false) // true + if param { + assert false + } + if !param { + assert true + } + if param == true { + assert false + } + if param == false { + assert true + } + } + else { + return + } + } +}