checker: fix panic when calling generic function with too few args (#8416)

pull/7529/head
zakuro 2021-01-30 03:14:22 +09:00 committed by GitHub
parent 8398e2f448
commit 2a32dac40d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 18 deletions

View File

@ -421,14 +421,11 @@ pub fn (mut c Checker) infer_fn_types(f table.Fn, mut call_expr ast.CallExpr) {
}
mut typ := table.void_type
for i, param in f.params {
if call_expr.args.len == 0 {
arg_i := if i != 0 && call_expr.is_method { i - 1 } else { i }
if call_expr.args.len <= arg_i {
break
}
arg := if i != 0 && call_expr.is_method {
call_expr.args[i - 1]
} else {
call_expr.args[i]
}
arg := call_expr.args[arg_i]
param_type_sym := c.table.get_type_symbol(param.typ)
if param.typ.has_flag(.generic) && param_type_sym.name == gt_name {
typ = arg.typ

View File

@ -0,0 +1,27 @@
vlib/v/checker/tests/function_count_of_args_mismatch_err.vv:8:13: error: expected 1 arguments, but got 3
6 |
7 | fn main() {
8 | test(true, false, 1)
| ~~~~~~~~
9 | test()
10 | test2(true, false, 1)
vlib/v/checker/tests/function_count_of_args_mismatch_err.vv:9:2: error: expected 1 arguments, but got 0
7 | fn main() {
8 | test(true, false, 1)
9 | test()
| ~~~~~~
10 | test2(true, false, 1)
11 | test2(true)
vlib/v/checker/tests/function_count_of_args_mismatch_err.vv:10:21: error: expected 2 arguments, but got 3
8 | test(true, false, 1)
9 | test()
10 | test2(true, false, 1)
| ^
11 | test2(true)
12 | }
vlib/v/checker/tests/function_count_of_args_mismatch_err.vv:11:2: error: expected 2 arguments, but got 1
9 | test()
10 | test2(true, false, 1)
11 | test2(true)
| ~~~~~~~~~~~
12 | }

View File

@ -0,0 +1,12 @@
fn test(b bool) {
}
fn test2<T>(b bool, v T) {
}
fn main() {
test(true, false, 1)
test()
test2(true, false, 1)
test2(true)
}

View File

@ -1,6 +0,0 @@
vlib/v/checker/tests/function_too_many_args_err.vv:5:16: error: expected 1 arguments, but got 3
3 |
4 | fn main() {
5 | test(true, false, 1)
| ~~~~~~~~
6 | }

View File

@ -1,6 +0,0 @@
fn test(b bool) {
}
fn main() {
test(true, false, 1)
}