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

v: fix generic lambda type binding and resolution #22083

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -1872,6 +1872,7 @@ pub mut:
func &AnonFn = unsafe { nil }
is_checked bool
typ Type
call_ctx &CallExpr = unsafe { nil }
}

pub struct Likely {
Expand Down
13 changes: 13 additions & 0 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -1585,6 +1585,19 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
if c.comptime.type_map.len > 0 {
continue
}
if mut call_arg.expr is ast.LambdaExpr {
// Calling fn is generic and lambda arg also is generic
if node.concrete_types.len > 0
&& call_arg.expr.func.decl.generic_names.len > 0 {
call_arg.expr.call_ctx = unsafe { node }
if c.table.register_fn_concrete_types(call_arg.expr.func.decl.fkey(),
node.concrete_types)
{
call_arg.expr.func.decl.ninstances++
}
}
continue
}
c.error('${err.msg()} in argument ${i + 1} to `${fn_name}`', call_arg.pos)
}
}
Expand Down
10 changes: 8 additions & 2 deletions vlib/v/gen/c/cgen.v
Original file line number Diff line number Diff line change
Expand Up @@ -3565,8 +3565,14 @@ fn (mut g Gen) expr(node_ ast.Expr) {
g.write('/*IsRefType*/ ${is_ref_type}')
}
ast.LambdaExpr {
g.gen_anon_fn(mut node.func)
// g.write('/* lambda expr: ${node_.str()} */')
if node.call_ctx != unsafe { nil } {
save_cur_concrete_types := g.cur_concrete_types
g.cur_concrete_types = node.call_ctx.concrete_types
g.gen_anon_fn(mut node.func)
g.cur_concrete_types = save_cur_concrete_types
} else {
g.gen_anon_fn(mut node.func)
}
}
ast.Likely {
if node.is_likely {
Expand Down
1 change: 1 addition & 0 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,7 @@ fn (mut g Gen) closure_ctx(node ast.FnDecl) string {
fn (mut g Gen) gen_anon_fn(mut node ast.AnonFn) {
g.gen_anon_fn_decl(mut node)
mut fn_name := node.decl.name

if node.decl.generic_names.len > 0 {
fn_name = g.generic_fn_name(g.cur_concrete_types, fn_name)
}
Expand Down
12 changes: 12 additions & 0 deletions vlib/v/tests/lambda_generic_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import arrays

fn abc[T](arg T, call fn (a T) string) string {
return call(arg)
}

fn test_main() {
a := [1, 2, 3, 4]
b := arrays.join_to_string(a, '-', |x| x.str())
assert b == '1-2-3-4'
assert abc(123, |x| x.str()) == '123'
}
Loading