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

checker: fix inner functions are called before definition. (fix #15673) (#15719)

This commit is contained in:
shove
2022-09-11 16:10:47 +08:00
committed by GitHub
parent aa3651fa58
commit 8b5f3aa970
3 changed files with 26 additions and 0 deletions

View File

@@ -413,6 +413,12 @@ fn (mut c Checker) anon_fn(mut node ast.AnonFn) ast.Type {
}
pub fn (mut c Checker) call_expr(mut node ast.CallExpr) ast.Type {
// Check whether the inner function definition is before the call
if var := node.scope.find_var(node.name) {
if var.expr is ast.AnonFn && var.pos.pos > node.pos.pos {
c.error('unknown function: $node.name', node.pos)
}
}
// TODO merge logic from method_call and fn_call
// First check everything that applies to both fns and methods
old_inside_fn_arg := c.inside_fn_arg

View File

@@ -0,0 +1,13 @@
vlib/v/checker/tests/inner_functions_call_before_define.vv:2:7: error: unknown function: f
1 | fn main() {
2 | _ := f()
| ~~~
3 | go f()
4 |
vlib/v/checker/tests/inner_functions_call_before_define.vv:3:5: error: unknown function: f
1 | fn main() {
2 | _ := f()
3 | go f()
| ~~~
4 |
5 | f := fn () string { return "hello" }

View File

@@ -0,0 +1,7 @@
fn main() {
_ := f()
go f()
f := fn () string { return "hello" }
_ := f() // avoid warnings
}