From 76cc72844fa51072388038f86c7b87285fdbdddc Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 10 Nov 2023 23:53:41 -0300 Subject: [PATCH] fix --- vlib/v/gen/c/cgen.v | 9 ++++++--- vlib/v/gen/c/if.v | 8 ++++++++ vlib/v/gen/c/infix.v | 7 +++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 3771bcb0e5898e..c0e887d6ef651c 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -4349,6 +4349,11 @@ fn (mut g Gen) get_const_name(node ast.Ident) string { } } +fn (mut g Gen) is_interface_var(var ast.ScopeObject) bool { + return var is ast.Var && var.orig_type != 0 && g.table.sym(var.orig_type).kind == .interface_ + && g.table.sym(var.smartcasts.last()).kind != .interface_ +} + fn (mut g Gen) ident(node ast.Ident) { prevent_sum_type_unwrapping_once := g.prevent_sum_type_unwrapping_once g.prevent_sum_type_unwrapping_once = false @@ -4485,9 +4490,7 @@ fn (mut g Gen) ident(node ast.Ident) { g.write('(') if obj_sym.kind == .sum_type && !is_auto_heap { g.write('*') - } else if g.inside_casting_to_str && node.obj.orig_type != 0 - && g.table.sym(node.obj.orig_type).kind == .interface_ - && g.table.sym(node.obj.smartcasts.last()).kind != .interface_ { + } else if g.inside_casting_to_str && g.is_interface_var(node.obj) { g.write('*') } } diff --git a/vlib/v/gen/c/if.v b/vlib/v/gen/c/if.v index ea9a80916d1512..91ae1d1d7afce0 100644 --- a/vlib/v/gen/c/if.v +++ b/vlib/v/gen/c/if.v @@ -378,6 +378,14 @@ fn (mut g Gen) if_expr(node ast.IfExpr) { no_needs_par = true } } + if !g.inside_casting_to_str && branch.cond is ast.Ident + && g.is_interface_var(branch.cond.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 + } + } if no_needs_par { g.write('if ') } else { diff --git a/vlib/v/gen/c/infix.v b/vlib/v/gen/c/infix.v index bbc292f27ed2e3..7db1f2485d7110 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.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()} ')