Skip to content

Commit 4bafc50

Browse files
authored
checker: check generic struct field fn args error (#12373)
1 parent 45c938b commit 4bafc50

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

vlib/v/checker/checker.v

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2277,7 +2277,7 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
22772277
if field := c.table.find_field(left_type_sym, method_name) {
22782278
field_type_sym := c.table.get_type_symbol(c.unwrap_generic(field.typ))
22792279
if field_type_sym.kind == .function {
2280-
// node.is_method = false
2280+
node.is_method = false
22812281
node.is_field = true
22822282
info := field_type_sym.info as ast.FnType
22832283
node.return_type = info.func.return_type
@@ -2288,6 +2288,8 @@ pub fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
22882288
earg_types << targ
22892289
}
22902290
node.expected_arg_types = earg_types
2291+
c.check_expected_arg_count(mut node, info.func) or { return info.func.return_type }
2292+
node.is_method = true
22912293
return info.func.return_type
22922294
}
22932295
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
vlib/v/checker/tests/generics_struct_field_fn_args_err.vv:21:20: error: expected 0 arguments, but got 1
2+
19 | }
3+
20 | println(fun0.call())
4+
21 | println(fun0.call(1234))
5+
| ~~~~
6+
22 | println(fun0.call(1234, 5678))
7+
23 |
8+
vlib/v/checker/tests/generics_struct_field_fn_args_err.vv:22:20: error: expected 0 arguments, but got 2
9+
20 | println(fun0.call())
10+
21 | println(fun0.call(1234))
11+
22 | println(fun0.call(1234, 5678))
12+
| ~~~~~~~~~~
13+
23 |
14+
24 | fun1 := Fun<fn (int) int>{
15+
vlib/v/checker/tests/generics_struct_field_fn_args_err.vv:29:15: error: expected 1 arguments, but got 0
16+
27 |
17+
28 | println(fun1.call(42))
18+
29 | println(fun1.call())
19+
| ~~~~~~
20+
30 | println(fun1.call(42, 43))
21+
31 | }
22+
vlib/v/checker/tests/generics_struct_field_fn_args_err.vv:30:24: error: expected 1 arguments, but got 2
23+
28 | println(fun1.call(42))
24+
29 | println(fun1.call())
25+
30 | println(fun1.call(42, 43))
26+
| ~~
27+
31 | }
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
fn get_int() int {
2+
return 42
3+
}
4+
5+
fn dub_int(i int) int {
6+
return i * 2
7+
}
8+
9+
struct Fun<F> {
10+
mut:
11+
call F
12+
}
13+
14+
type FunZero = fn () int
15+
16+
fn main() {
17+
fun0 := Fun<FunZero>{
18+
call: get_int
19+
}
20+
println(fun0.call())
21+
println(fun0.call(1234))
22+
println(fun0.call(1234, 5678))
23+
24+
fun1 := Fun<fn (int) int>{
25+
call: dub_int
26+
}
27+
28+
println(fun1.call(42))
29+
println(fun1.call())
30+
println(fun1.call(42, 43))
31+
}

0 commit comments

Comments
 (0)