From e38ffce1844b5e68c1522e6aa6a2a89d6e3723f8 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 9 Nov 2024 21:22:04 -0300 Subject: [PATCH] fix --- vlib/v/gen/c/str.v | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index 0b3bd067599334..09a273991ff3a1 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -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) { @@ -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) @@ -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)) @@ -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 { @@ -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(')') } }