cgen: fix cross assign in closure (#14549)

yuyi 2022-05-29 01:47:54 +08:00 committed by Chewing_Bever
parent 0a81074b1e
commit e50d73983f
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 4 additions and 4 deletions

View File

@ -625,12 +625,13 @@ fn (mut g Gen) gen_cross_var_assign(node &ast.AssignStmt) {
ast.Ident { ast.Ident {
left_typ := node.left_types[i] left_typ := node.left_types[i]
left_sym := g.table.sym(left_typ) left_sym := g.table.sym(left_typ)
anon_ctx := if g.anon_fn { '$closure_ctx->' } else { '' }
if left_sym.kind == .function { if left_sym.kind == .function {
g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_$left.pos.pos') g.write_fn_ptr_decl(left_sym.info as ast.FnType, '_var_$left.pos.pos')
g.writeln(' = ${c_name(left.name)};') g.writeln(' = $anon_ctx${c_name(left.name)};')
} else { } else {
styp := g.typ(left_typ) styp := g.typ(left_typ)
g.writeln('$styp _var_$left.pos.pos = ${c_name(left.name)};') g.writeln('$styp _var_$left.pos.pos = $anon_ctx${c_name(left.name)};')
} }
} }
ast.IndexExpr { ast.IndexExpr {

View File

@ -10,8 +10,7 @@ fn sma(period int) fn (f64) f64 {
} }
sum += input - storage[i] sum += input - storage[i]
storage[i] = input storage[i], i = input, (i + 1) % period
i = (i + 1) % period
return sum / f64(storage.len) return sum / f64(storage.len)
} }
} }