Skip to content

Commit

Permalink
cgen: fix auto deref for interface variable (#19841)
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Nov 14, 2023
1 parent f5bf18c commit 51efe04
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vlib/v/gen/c/if.v
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/gen/c/infix.v
Expand Up @@ -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()} ')
Expand Down
28 changes: 28 additions & 0 deletions 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
}
}
}

0 comments on commit 51efe04

Please sign in to comment.