From 745b03963cd98ee5a750ed482e3651ccb079165d Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sat, 30 Aug 2025 12:08:02 -0300 Subject: [PATCH] fix --- vlib/v/checker/for.v | 3 +++ vlib/v/gen/c/assign.v | 12 +++++++++++- vlib/v/gen/c/for.v | 2 +- vlib/v/gen/c/str.v | 4 ++-- vlib/v/gen/c/str_intp.v | 2 +- vlib/v/markused/walker.v | 4 ++++ 6 files changed, 22 insertions(+), 5 deletions(-) diff --git a/vlib/v/checker/for.v b/vlib/v/checker/for.v index 1643ac1726..bb68ef9898 100644 --- a/vlib/v/checker/for.v +++ b/vlib/v/checker/for.v @@ -254,6 +254,9 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) { } if node.val_is_mut { value_type = value_type.ref() + if value_type.has_flag(.option) { + value_type = value_type.set_flag(.option_mut_param_t) + } match mut node.cond { ast.Ident { if mut node.cond.obj is ast.Var { diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index 9f5d7f94e6..2b4a158437 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -866,9 +866,19 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { g.write('*') } if node_.op == .assign && var_type.has_flag(.option_mut_param_t) { + if val is ast.CastExpr { + g.expr(left) + g.write('->state = ') + g.expr(val) + g.writeln('.state;') + } g.write('memcpy(&') g.expr(left) - g.write('->data, *(${g.styp(val_type)}**)&') + if val is ast.CastExpr { + g.write('->data, ') + } else { + g.write('->data, *(${g.styp(val_type)}**)&') + } } else if var_type.has_flag(.option_mut_param_t) { g.expr(left) g.write(' = ') diff --git a/vlib/v/gen/c/for.v b/vlib/v/gen/c/for.v index 50a802ed6c..0e31f3e689 100644 --- a/vlib/v/gen/c/for.v +++ b/vlib/v/gen/c/for.v @@ -322,7 +322,7 @@ fn (mut g Gen) for_in_stmt(node_ ast.ForInStmt) { g.writeln('\t${styp} ${c_name(node.val_var)};') g.writeln('\tmemcpy(*(${styp}*)${c_name(node.val_var)}, (byte*)${cond_var}[${idx}], sizeof(${styp}));') } else { - styp := g.styp(node.val_type) + mut styp := g.styp(node.val_type) g.write('\t${styp} ${c_name(node.val_var)}') } if !is_fixed_array { diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index 6d3bab7f05..b7c736c606 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -120,7 +120,7 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) { exp_typ := if unwrap_option { typ.clear_flag(.option) } else { typ } is_dump_expr := expr is ast.DumpExpr is_var_mut := expr.is_auto_deref_var() - str_fn_name := g.get_str_fn(exp_typ) + mut str_fn_name := g.get_str_fn(exp_typ) temp_var_needed := expr is ast.CallExpr && (expr.return_type.is_ptr() || g.table.sym(expr.return_type).is_c_struct()) mut tmp_var := '' @@ -171,7 +171,7 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) { } } else if is_ptr && typ.has_flag(.option) { if typ.has_flag(.option_mut_param_t) { - g.write('*') + g.write('/**/*') } else { g.write('*(${g.styp(typ)}*)&') } diff --git a/vlib/v/gen/c/str_intp.v b/vlib/v/gen/c/str_intp.v index 3a342221b0..6d5b56b1d6 100644 --- a/vlib/v/gen/c/str_intp.v +++ b/vlib/v/gen/c/str_intp.v @@ -50,7 +50,7 @@ fn (mut g Gen) str_format(node ast.StringInterLiteral, i int, fmts []u8) (u64, s mut base := 0 // numeric base mut upper_case := false // set uppercase for the result string mut typ := g.unwrap_generic(node.expr_types[i]) - if node.exprs[i].is_auto_deref_var() { + if node.exprs[i].is_auto_deref_var() && !typ.has_flag(.option_mut_param_t) { typ = typ.deref() } typ = g.table.final_type(typ) diff --git a/vlib/v/markused/walker.v b/vlib/v/markused/walker.v index a7a286a72b..130720cc98 100644 --- a/vlib/v/markused/walker.v +++ b/vlib/v/markused/walker.v @@ -1300,6 +1300,10 @@ fn (mut w Walker) mark_resource_dependencies() { w.fn_by_name(builderptr_idx + '.write_string') w.fn_by_name('strings.new_builder') w.uses_free[ast.string_type] = true + + if w.table.dumps.keys().any(ast.Type(u32(it)).clear_flags(.shared_f).has_flag(.option)) { + w.fn_by_name('str_intp') + } } if w.features.auto_str_ptr { w.fn_by_name('isnil')