From f246d73d6e2afb6f968b67231e0b048d01a53343 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 4 Jul 2021 23:36:37 +0800 Subject: [PATCH] cgen: fix generic fn return mut parameter (#10662) --- vlib/v/gen/c/cgen.v | 6 ++++ vlib/v/gen/c/str.v | 3 ++ .../tests/generic_fn_infer_multi_paras_test.v | 28 ++++++++++++++----- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 81f588993e..bfa539e6b4 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2280,6 +2280,9 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) { g.write('*') } } + if lx.is_auto_deref_var() { + g.write('*') + } g.expr(lx) noscan := if is_auto_heap { g.check_noscan(return_type) } else { '' } if is_opt { @@ -4622,6 +4625,9 @@ fn (mut g Gen) return_stmt(node ast.Return) { continue } g.write('.arg$arg_idx=') + if expr.is_auto_deref_var() { + g.write('*') + } g.expr(expr) arg_idx++ if i < node.exprs.len - 1 { diff --git a/vlib/v/gen/c/str.v b/vlib/v/gen/c/str.v index cd2f86a690..a4b9ae43c2 100644 --- a/vlib/v/gen/c/str.v +++ b/vlib/v/gen/c/str.v @@ -138,6 +138,9 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) { } else { str_fn_name := g.gen_str_for_type(typ) g.write('${str_fn_name}(') + if expr.is_auto_deref_var() { + g.write('*') + } if sym.kind != .function { g.expr_with_cast(expr, typ, typ) } diff --git a/vlib/v/tests/generic_fn_infer_multi_paras_test.v b/vlib/v/tests/generic_fn_infer_multi_paras_test.v index 9c3f5bfa74..71928342ff 100644 --- a/vlib/v/tests/generic_fn_infer_multi_paras_test.v +++ b/vlib/v/tests/generic_fn_infer_multi_paras_test.v @@ -1,28 +1,38 @@ -pub struct Page { +pub struct Two_data { pub mut: - lang string - page string - var_one string - var_two string + title string + content string } -fn get_keys_and_values(mut keys []string, mut values []string, mut data T) { +pub struct Page { +pub mut: + lang string + page string + var_one string + var_two string + var_three Two_data +} + +fn get_keys_and_values(mut keys []string, mut values []string, mut data T) ([]string, []string, T) { $for field in T.fields { $if field.typ is string { keys << field.name values << data.$(field.name) } } + return keys, values, data } fn awesome(mut data T) { mut keys := []string{} mut values := []string{} - get_keys_and_values(mut keys, mut values, mut data) + keys, values, data = get_keys_and_values(mut keys, mut values, mut data) println(keys) assert keys == ['lang', 'page', 'var_one', 'var_two'] println(values) assert values == ['vlang', 'one', 'variable one', 'variable two'] + println(data) + assert '$data'.contains("title: 'what a title'") } fn test_generic_fn_infer_multi_paras() { @@ -31,6 +41,10 @@ fn test_generic_fn_infer_multi_paras() { page: 'one' var_one: 'variable one' var_two: 'variable two' + var_three: { + title: 'what a title' + content: 'what a content' + } } awesome(mut page) }