1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: check assigning generic function to a variable (#16507)

This commit is contained in:
yuyi 2022-11-22 23:56:49 +08:00 committed by GitHub
parent 27cdf5ae0e
commit 7c7ebd648d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 0 deletions

View File

@ -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 {

View File

@ -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 <T> (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<int>(100))
| ~~~~~
7 | }

View File

@ -0,0 +1,7 @@
fn main() {
fun := fn <T> (value T) T {
return value
}
println(fun<int>(100))
}