Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp committed Nov 10, 2024
1 parent ed3a9f3 commit e38ffce
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions vlib/v/gen/c/str.v
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
if is_shared {
typ = typ.clear_flag(.shared_f).set_nr_muls(0)
}
// original is_ptr for the typ (aliased type could overwrite it)
is_ptr := typ.is_ptr()
mut sym := g.table.sym(typ)
// when type is non-option alias and doesn't has `str()`, print the aliased value
if mut sym.info is ast.Alias && !sym.has_method('str') && !etype.has_flag(.option) {
Expand Down Expand Up @@ -113,7 +115,6 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
|| sym.kind in [.array, .array_fixed, .map, .struct, .multi_return, .sum_type, .interface] {
unwrap_option := expr is ast.Ident && expr.or_expr.kind == .propagate_option
exp_typ := if unwrap_option { typ.clear_flag(.option) } else { typ }
is_ptr := exp_typ.is_ptr()
is_dump_expr := expr is ast.DumpExpr
is_var_mut := expr.is_auto_deref_var()
str_fn_name := g.get_str_fn(exp_typ)
Expand Down Expand Up @@ -170,7 +171,7 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
if sym.is_c_struct() {
g.write(c_struct_ptr(sym, typ, str_method_expects_ptr))
} else {
g.write('*'.repeat(typ.nr_muls()))
g.write('*'.repeat(etype.nr_muls()))
}
} else if sym.is_c_struct() {
g.write(c_struct_ptr(sym, typ, str_method_expects_ptr))
Expand Down Expand Up @@ -201,11 +202,15 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
g.write('}}}))')
}
} else {
is_ptr := typ.is_ptr()
is_var_mut := expr.is_auto_deref_var()
str_fn_name := g.get_str_fn(typ)
g.write('${str_fn_name}(')
if sym.kind != .function {
if is_ptr && !is_var_mut {
g.write('isnil(')
g.expr_with_cast(expr, typ, typ)
g.write(') ? _SLIT("nil") : ')
}
g.write('${str_fn_name}(')
if str_method_expects_ptr && !is_ptr && !typ.has_flag(.option) {
g.write('&')
} else if (!str_method_expects_ptr && is_ptr && !is_shared) || is_var_mut {
Expand All @@ -216,10 +221,12 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
}
}
g.expr_with_cast(expr, typ, typ)
g.write(')')
} else if typ.has_flag(.option) {
// only Option fn receive argument
g.write('${str_fn_name}(')
g.expr_with_cast(expr, typ, typ)
g.write(')')
}
g.write(')')
}
}

0 comments on commit e38ffce

Please sign in to comment.