diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index fdf52fb5d6c37c..43f986ceb0241b 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -313,6 +313,13 @@ fn (a string) clone_static() string { return a.clone() } +// option_clone_static returns an independent copy of a given array when lhs is an option type. +// It should be used only in -autofree generated code. +@[inline; markused] +fn (a string) option_clone_static() ?string { + return ?string(a.clone()) +} + // clone returns a copy of the V string `a`. pub fn (a string) clone() string { if a.len <= 0 { diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index ca2f93f4889954..b5ba993c44cb99 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -652,7 +652,7 @@ fn (mut g Gen) assign_stmt(node_ ast.AssignStmt) { mut cloned := false if g.is_autofree && right_sym.kind in [.array, .string] && !unwrapped_val_type.has_flag(.shared_f) { - if g.gen_clone_assignment(val, unwrapped_val_type, false) { + if g.gen_clone_assignment(var_type, val, unwrapped_val_type, false) { cloned = true } } diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 427e56df73b6f3..2fff17aa6d34ae 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2877,7 +2877,7 @@ fn (mut g Gen) get_ternary_name(name string) string { return g.ternary_names[name] } -fn (mut g Gen) gen_clone_assignment(val ast.Expr, typ ast.Type, add_eq bool) bool { +fn (mut g Gen) gen_clone_assignment(var_type ast.Type, val ast.Expr, typ ast.Type, add_eq bool) bool { if val !in [ast.Ident, ast.SelectorExpr] { return false } @@ -2905,7 +2905,11 @@ fn (mut g Gen) gen_clone_assignment(val ast.Expr, typ ast.Type, add_eq bool) boo } } else if right_sym.kind == .string { // `str1 = str2` => `str1 = str2.clone()` - g.write(' string_clone_static(') + if var_type.has_flag(.option) { + g.write(' string_option_clone_static(') + } else { + g.write(' string_clone_static(') + } g.expr(val) g.write(')') } @@ -2981,11 +2985,7 @@ fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int continue } is_option := obj.typ.has_flag(.option) - if is_option { - // TODO: free options - continue - } - g.autofree_variable(obj) + g.autofree_variable(obj, is_option) } else {} } @@ -3009,7 +3009,7 @@ fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int } } -fn (mut g Gen) autofree_variable(v ast.Var) { +fn (mut g Gen) autofree_variable(v ast.Var, is_option bool) { // filter out invalid variables if v.typ == 0 { return @@ -3093,6 +3093,10 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) { af.write_string(free_fn_name) } af.write_string('(') + if v.typ.has_flag(.option) { + base_type := g.base_type(v.typ) + af.write_string('(${base_type}*)') + } if v.typ.share() == .shared_t { af.write_string('&') } @@ -3101,6 +3105,9 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) { if v.typ.share() == .shared_t { af.write_string('->val') } + if v.typ.has_flag(.option) { + af.write_string('.data)') + } af.writeln('); // autofreed ptr var') } else { @@ -3109,6 +3116,11 @@ fn (mut g Gen) autofree_var_call(free_fn_name string, v ast.Var) { } if v.is_auto_heap { af.writeln('\t${free_fn_name}(${c_name(v.name)}); // autofreed heap var ${g.cur_mod.name} ${g.is_builtin_mod}') + } else if v.typ.has_flag(.option) { + base_type := g.base_type(v.typ) + af.writeln('\tif (${c_name(v.name)}.state != 2) {') + af.writeln('\t\t${free_fn_name}((${base_type}*)${c_name(v.name)}.data); // autofreed option var ${g.cur_mod.name} ${g.is_builtin_mod}') + af.writeln('\t}') } else { af.writeln('\t${free_fn_name}(&${c_name(v.name)}); // autofreed var ${g.cur_mod.name} ${g.is_builtin_mod}') } diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 5dc7c06473ef34..771be7b147f9ca 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -2220,10 +2220,6 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) { // continue // } is_option := obj.typ.has_flag(.option) - if is_option { - // TODO: free options - continue - } is_result := obj.typ.has_flag(.result) if is_result { // TODO: free results @@ -2237,7 +2233,7 @@ fn (mut g Gen) autofree_call_postgen(node_pos int) { continue } obj.is_used = true // TODO bug? sets all vars is_used to true - g.autofree_variable(obj) + g.autofree_variable(obj, is_option) // g.nr_vars_to_free-- } else {} diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index c47ff0929d0aaf..b7ae77c65486f1 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -619,7 +619,7 @@ fn (mut g Gen) struct_init_field(sfield ast.StructInitField, language ast.Langua mut cloned := false if g.is_autofree && !sfield.typ.is_ptr() && field_type_sym.kind in [.array, .string] { g.write('/*clone1*/') - if g.gen_clone_assignment(sfield.expr, sfield.typ, false) { + if g.gen_clone_assignment(sfield.typ, sfield.expr, sfield.typ, false) { cloned = true } }