From f23466a78c6a228f419027e9d2de252f00926c15 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Fri, 20 Sep 2024 08:29:58 -0300 Subject: [PATCH] fix --- vlib/v/gen/c/fn.v | 27 ++++++++++--------- .../tests/fns/anon_c_keywords_closure_test.v | 18 +++++++++++++ 2 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 vlib/v/tests/fns/anon_c_keywords_closure_test.v diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 4554d739bf251f..7a373d48ba74ac 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -592,6 +592,7 @@ fn (mut g Gen) gen_anon_fn(mut node ast.AnonFn) { for var in node.inherited_vars { mut has_inherited := false mut is_ptr := false + var_name := c_name(var.name) if obj := node.decl.scope.find(var.name) { if obj is ast.Var { is_ptr = obj.typ.is_ptr() @@ -599,13 +600,13 @@ fn (mut g Gen) gen_anon_fn(mut node ast.AnonFn) { has_inherited = true var_sym := g.table.sym(var.typ) if var_sym.info is ast.ArrayFixed { - g.write('.${var.name} = {') + g.write('.${var_name} = {') for i in 0 .. var_sym.info.size { - g.write('${closure_ctx}->${var.name}[${i}],') + g.write('${closure_ctx}->${var_name}[${i}],') } g.writeln('},') } else { - g.writeln('.${var.name} = ${closure_ctx}->${var.name},') + g.writeln('.${var_name} = ${closure_ctx}->${var_name},') } } } @@ -613,15 +614,15 @@ fn (mut g Gen) gen_anon_fn(mut node ast.AnonFn) { if !has_inherited { var_sym := g.table.sym(var.typ) if var_sym.info is ast.ArrayFixed { - g.write('.${var.name} = {') + g.write('.${var_name} = {') for i in 0 .. var_sym.info.size { - g.write('${var.name}[${i}],') + g.write('${var_name}[${i}],') } g.writeln('},') } else if g.is_autofree && !var.is_mut && var_sym.info is ast.Array { - g.writeln('.${var.name} = array_clone(&${var.name}),') + g.writeln('.${var_name} = array_clone(&${var_name}),') } else if g.is_autofree && !var.is_mut && var_sym.kind == .string { - g.writeln('.${var.name} = string_clone(${var.name}),') + g.writeln('.${var_name} = string_clone(${var_name}),') } else { mut is_auto_heap := false if obj := node.decl.scope.parent.find(var.name) { @@ -630,9 +631,9 @@ fn (mut g Gen) gen_anon_fn(mut node ast.AnonFn) { } } if is_auto_heap && !is_ptr { - g.writeln('.${var.name} = *${var.name},') + g.writeln('.${var_name} = *${var_name},') } else { - g.writeln('.${var.name} = ${var.name},') + g.writeln('.${var_name} = ${var_name},') } } } @@ -661,11 +662,11 @@ fn (mut g Gen) gen_anon_fn_decl(mut node ast.AnonFn) { var_sym := g.table.sym(var.typ) if var_sym.info is ast.FnType { sig := g.fn_var_signature(var_sym.info.func.return_type, var_sym.info.func.params.map(it.typ), - var.name) + c_name(var.name)) builder.writeln('\t' + sig + ';') } else { styp := g.typ(var.typ) - builder.writeln('\t${styp} ${var.name};') + builder.writeln('\t${styp} ${c_name(var.name)};') } } builder.writeln('};\n') @@ -2177,7 +2178,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) { mut is_ptr := false if i == 0 { if obj.is_inherited { - g.write(closure_ctx + '->' + node.name) + g.write(closure_ctx + '->' + c_name(node.name)) } else { g.write(node.name) } @@ -2199,7 +2200,7 @@ fn (mut g Gen) fn_call(node ast.CallExpr) { } is_fn_var = true } else if obj.is_inherited { - g.write(closure_ctx + '->' + node.name) + g.write(closure_ctx + '->' + c_name(node.name)) is_fn_var = true } } diff --git a/vlib/v/tests/fns/anon_c_keywords_closure_test.v b/vlib/v/tests/fns/anon_c_keywords_closure_test.v new file mode 100644 index 00000000000000..c5df9ceae91c74 --- /dev/null +++ b/vlib/v/tests/fns/anon_c_keywords_closure_test.v @@ -0,0 +1,18 @@ +fn test_main() { + mut stderr := 0 + mut stdout := 0 + mut stdin := fn () {} + + causes_error := fn [mut stderr, mut stdin, mut stdout] () int { + stderr = 0 + stdin() + stdout = 2 + assert stderr == 0 + assert stdout == 2 + return stderr + stdout + } + assert causes_error() == 2 + assert voidptr(C.stdin) != unsafe { nil } + assert voidptr(C.stdout) != unsafe { nil } + assert voidptr(C.stderr) != unsafe { nil } +}