diff --git a/vlib/v/checker/assign.v b/vlib/v/checker/assign.v index 1e1aafa766..28a459ce53 100644 --- a/vlib/v/checker/assign.v +++ b/vlib/v/checker/assign.v @@ -65,6 +65,11 @@ fn (mut c Checker) assign_stmt(mut node ast.AssignStmt) { c.error('cannot use `none` in `unsafe` blocks', right.expr.pos) } } + if mut right is ast.AnonFn { + if right.decl.generic_names.len > 0 { + c.error('cannot assign generic function to a variable', right.decl.pos) + } + } } if node.left.len != right_len { if right_first is ast.CallExpr { diff --git a/vlib/v/checker/tests/assign_generic_fn_err.out b/vlib/v/checker/tests/assign_generic_fn_err.out new file mode 100644 index 0000000000..ca7fa927a2 --- /dev/null +++ b/vlib/v/checker/tests/assign_generic_fn_err.out @@ -0,0 +1,12 @@ +vlib/v/checker/tests/assign_generic_fn_err.vv:2:9: error: cannot assign generic function to a variable + 1 | fn main() { + 2 | fun := fn (value T) T { + | ~~~~~~~~~~~~~~~~~~~~ + 3 | return value + 4 | } +vlib/v/checker/tests/assign_generic_fn_err.vv:6:13: error: a non generic function called like a generic one + 4 | } + 5 | + 6 | println(fun(100)) + | ~~~~~ + 7 | } diff --git a/vlib/v/checker/tests/assign_generic_fn_err.vv b/vlib/v/checker/tests/assign_generic_fn_err.vv new file mode 100644 index 0000000000..7bf3ad9be2 --- /dev/null +++ b/vlib/v/checker/tests/assign_generic_fn_err.vv @@ -0,0 +1,7 @@ +fn main() { + fun := fn (value T) T { + return value + } + + println(fun(100)) +}