Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Nov 11, 2023
1 parent e7cad4f commit 76cc728
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
9 changes: 6 additions & 3 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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('*')
}
}
Expand Down
8 changes: 8 additions & 0 deletions vlib/v/gen/c/if.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/gen/c/infix.v
Original file line number Diff line number Diff line change
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.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

0 comments on commit 76cc728

Please sign in to comment.