ast, checker, cgen: fix error for go anon fn variable call (#13776)

pull/13777/head
yuyi 2022-03-20 13:45:23 +08:00 committed by GitHub
parent c05634ebd1
commit 3e40cd5baa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 0 deletions

View File

@ -557,6 +557,7 @@ pub mut:
name string // left.name()
is_method bool
is_field bool // temp hack, remove ASAP when re-impl CallExpr / Selector (joe)
is_fn_var bool // fn variable
is_keep_alive bool // GC must not free arguments before fn returns
is_noreturn bool // whether the function/method is marked as [noreturn]
is_ctor_new bool // if JS ctor calls requires `new` before call, marked as `[use_new]` in V
@ -568,6 +569,7 @@ pub mut:
left_type Type // type of `user`
receiver_type Type // User
return_type Type
fn_var_type Type // fn variable type
should_be_skipped bool // true for calls to `[if someflag?]` functions, when there is no `-d someflag`
concrete_types []Type // concrete types, e.g. <int, string>
concrete_list_pos token.Pos

View File

@ -600,9 +600,13 @@ pub fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool)
match obj {
ast.GlobalField {
typ = obj.typ
node.is_fn_var = true
node.fn_var_type = typ
}
ast.Var {
typ = if obj.smartcasts.len != 0 { obj.smartcasts.last() } else { obj.typ }
node.is_fn_var = true
node.fn_var_type = typ
}
else {}
}

View File

@ -5106,6 +5106,8 @@ fn (mut g Gen) go_expr(node ast.GoExpr) {
} else if mut expr.left is ast.AnonFn {
g.gen_anon_fn_decl(mut expr.left)
name = expr.left.decl.name
} else if expr.is_fn_var {
name = g.table.sym(expr.fn_var_type).name
}
name = util.no_dots(name)
if g.pref.obfuscate && g.cur_mod.name == 'main' && name.starts_with('main__') {

View File

@ -0,0 +1,18 @@
fn sum(a int, b int) int {
// Simply proxy the function into an anonymous function for demo purposes
sum_func := fn (a int, b int) int {
return a + b
}
// and run it concurrently
g := go sum_func(a, b)
result := g.wait()
return result
}
fn test_go_anon_fn_variable_call() {
ret := sum(22, 33)
println(ret)
assert ret == 55
}