From bd5dc047317d02ee9d37a5ab8f9e6ca4f14474b6 Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Sat, 16 Aug 2025 10:56:17 +0800 Subject: [PATCH] fix expr_string remove space --- vlib/v/gen/c/cgen.v | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index dcd0224363..6457cb4be5 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2858,7 +2858,7 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp ast.Ty got_is_ptr bool, got_is_fn bool, got_styp string) { mut rparen_n := 1 mut mutable_idx := 0 - mut mutable_sumtype_ptr := '' + mut mutable_sumtype_ptr_name := '' is_not_ptr_and_fn := !got_is_ptr && !got_is_fn is_sumtype_cast := !got_is_fn && fname.contains('_to_sumtype_') @@ -2916,8 +2916,11 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp ast.Ty } else { old_left_is_opt := g.left_is_opt g.left_is_opt = !exp.has_flag(.option) - mutable_sumtype_ptr = g.expr_string(expr) - g.write(mutable_sumtype_ptr) + // Do not use `g.expr_string()` here, as it trim space, we need keep space here + pos := g.out.len + g.expr(expr) + mutable_sumtype_ptr_name = g.out.cut_to(pos) + g.write(mutable_sumtype_ptr_name) g.left_is_opt = old_left_is_opt } if mutable_idx != 0 { @@ -2925,8 +2928,14 @@ fn (mut g Gen) call_cfn_for_casting_expr(fname string, expr ast.Expr, exp ast.Ty mut sb := strings.new_builder(256) got_sym, exp_sym := g.table.sym(got), g.table.sym(exp) got_cname := g.get_sumtype_variant_type_name(got, got_sym) + mutable_sumtype_ptr_name = mutable_sumtype_ptr_name.trim_space() + ptr_name := if got_is_ptr { + '(${mutable_sumtype_ptr_name})' + } else { + '(&(${mutable_sumtype_ptr_name}))' + } g.write_sumtype_casting_fn_common_fields(mut sb, exp_sym, got_cname, got_sym, - if got_is_ptr { mutable_sumtype_ptr } else { '&${mutable_sumtype_ptr}' }) + ptr_name) g.write(sb.str()) g.write('}') }