Skip to content

Commit

Permalink
checker: fix generec fn returning generic closure
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Dec 2, 2024
1 parent b79b4bf commit 2ef88ff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vlib/v/checker/return.v
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ fn (mut c Checker) return_stmt(mut node ast.Return) {
if c.inside_lambda && exp_type.has_flag(.generic) {
continue
}
// ignore return closure
if node.exprs[expr_idxs[i]] is ast.AnonFn
&& node.exprs[expr_idxs[i]].inherited_vars.len > 0 {
continue
}
c.error('cannot use `${got_type_name}` as ${c.error_type_name(exp_type)} in return argument',
pos)
}
Expand Down
19 changes: 19 additions & 0 deletions vlib/v/tests/generics/generics_return_closure_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
fn vectorize[T](op fn (T) T) fn ([]T) []T {
return fn [op] [T](values []T) []T {
mut result := []T{len: values.len}
for i in 0 .. values.len {
result[i] = op(values[i])
}
return result
}
}

fn add_one(x f64) f64 {
return x + 1
}

fn test_return_generic_closure() {
vadd := vectorize[f64](add_one)
v := [1.0, 2, 3, 4]
assert vadd(v) == [2.0, 3, 4, 5]
}

0 comments on commit 2ef88ff

Please sign in to comment.