cgen: fix fn call with fixed array literal arguments (#13225)

pull/13231/head
yuyi 2022-01-20 20:04:16 +08:00 committed by GitHub
parent d553071e65
commit 7c9cd855b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 0 deletions

View File

@ -78,6 +78,16 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
g.inside_lambda = false g.inside_lambda = false
return return
} }
need_tmp_var := g.inside_call && !g.inside_struct_init
mut stmt_str := ''
mut tmp_var := ''
if need_tmp_var {
tmp_var = g.new_tmp_var()
stmt_str = g.go_before_stmt(0)
ret_typ := g.typ(node.typ)
g.empty_line = true
g.write('$ret_typ $tmp_var = ')
}
g.write('{') g.write('{')
if node.has_val { if node.has_val {
for i, expr in node.exprs { for i, expr in node.exprs {
@ -100,6 +110,11 @@ fn (mut g Gen) array_init(node ast.ArrayInit) {
g.write('0') g.write('0')
} }
g.write('}') g.write('}')
if need_tmp_var {
g.writeln(';')
g.write(stmt_str)
g.write(tmp_var)
}
return return
} }
elem_styp := g.typ(elem_type.typ) elem_styp := g.typ(elem_type.typ)

View File

@ -113,6 +113,7 @@ mut:
inside_match_optional bool inside_match_optional bool
inside_vweb_tmpl bool inside_vweb_tmpl bool
inside_return bool inside_return bool
inside_struct_init bool
inside_or_block bool inside_or_block bool
inside_call bool inside_call bool
inside_for_c_stmt bool inside_for_c_stmt bool
@ -3390,7 +3391,9 @@ fn (mut g Gen) expr(node ast.Expr) {
g.expr(ast.resolve_init(node, g.unwrap_generic(node.typ), g.table)) g.expr(ast.resolve_init(node, g.unwrap_generic(node.typ), g.table))
} else { } else {
// `user := User{name: 'Bob'}` // `user := User{name: 'Bob'}`
g.inside_struct_init = true
g.struct_init(node) g.struct_init(node)
g.inside_struct_init = false
} }
} }
ast.TypeNode { ast.TypeNode {

View File

@ -0,0 +1,9 @@
fn test_fn_call_fixed_array_literal_args() {
ret := get_str([1]!)
assert ret == '[1]'
}
fn get_str(t [1]int) string {
println(t)
return '$t'
}