cgen: fix generic fn return mut parameter (#10662)

pull/10668/head
yuyi 2021-07-04 23:36:37 +08:00 committed by GitHub
parent 2752833a27
commit f246d73d6e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 7 deletions

View File

@ -2280,6 +2280,9 @@ fn (mut g Gen) gen_assign_stmt(assign_stmt ast.AssignStmt) {
g.write('*') g.write('*')
} }
} }
if lx.is_auto_deref_var() {
g.write('*')
}
g.expr(lx) g.expr(lx)
noscan := if is_auto_heap { g.check_noscan(return_type) } else { '' } noscan := if is_auto_heap { g.check_noscan(return_type) } else { '' }
if is_opt { if is_opt {
@ -4622,6 +4625,9 @@ fn (mut g Gen) return_stmt(node ast.Return) {
continue continue
} }
g.write('.arg$arg_idx=') g.write('.arg$arg_idx=')
if expr.is_auto_deref_var() {
g.write('*')
}
g.expr(expr) g.expr(expr)
arg_idx++ arg_idx++
if i < node.exprs.len - 1 { if i < node.exprs.len - 1 {

View File

@ -138,6 +138,9 @@ fn (mut g Gen) gen_expr_to_string(expr ast.Expr, etype ast.Type) {
} else { } else {
str_fn_name := g.gen_str_for_type(typ) str_fn_name := g.gen_str_for_type(typ)
g.write('${str_fn_name}(') g.write('${str_fn_name}(')
if expr.is_auto_deref_var() {
g.write('*')
}
if sym.kind != .function { if sym.kind != .function {
g.expr_with_cast(expr, typ, typ) g.expr_with_cast(expr, typ, typ)
} }

View File

@ -1,28 +1,38 @@
pub struct Two_data {
pub mut:
title string
content string
}
pub struct Page { pub struct Page {
pub mut: pub mut:
lang string lang string
page string page string
var_one string var_one string
var_two string var_two string
var_three Two_data
} }
fn get_keys_and_values<T>(mut keys []string, mut values []string, mut data T) { fn get_keys_and_values<T>(mut keys []string, mut values []string, mut data T) ([]string, []string, T) {
$for field in T.fields { $for field in T.fields {
$if field.typ is string { $if field.typ is string {
keys << field.name keys << field.name
values << data.$(field.name) values << data.$(field.name)
} }
} }
return keys, values, data
} }
fn awesome<T>(mut data T) { fn awesome<T>(mut data T) {
mut keys := []string{} mut keys := []string{}
mut values := []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) println(keys)
assert keys == ['lang', 'page', 'var_one', 'var_two'] assert keys == ['lang', 'page', 'var_one', 'var_two']
println(values) println(values)
assert values == ['vlang', 'one', 'variable one', 'variable two'] assert values == ['vlang', 'one', 'variable one', 'variable two']
println(data)
assert '$data'.contains("title: 'what a title'")
} }
fn test_generic_fn_infer_multi_paras() { fn test_generic_fn_infer_multi_paras() {
@ -31,6 +41,10 @@ fn test_generic_fn_infer_multi_paras() {
page: 'one' page: 'one'
var_one: 'variable one' var_one: 'variable one'
var_two: 'variable two' var_two: 'variable two'
var_three: {
title: 'what a title'
content: 'what a content'
}
} }
awesome(mut page) awesome(mut page)
} }