Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cgen: fix C name mangling with inherited closure vars #22263

Merged
merged 1 commit into from
Sep 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -592,36 +592,37 @@ 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()
if obj.has_inherited {
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},')
}
}
}
}
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) {
Expand All @@ -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},')
}
}
}
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}
}
Expand Down
18 changes: 18 additions & 0 deletions vlib/v/tests/fns/anon_c_keywords_closure_test.v
Original file line number Diff line number Diff line change
@@ -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 }
}
Loading