Skip to content

Commit 4a4434a

Browse files
authored
checker: fix missing check for empty array to generic param (fix #25056) (#25118)
1 parent 51630f1 commit 4a4434a

3 files changed

Lines changed: 36 additions & 2 deletions

File tree

‎vlib/v/checker/fn.v‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1543,8 +1543,11 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
15431543
}
15441544
}
15451545
arg_typ_sym := c.table.sym(arg_typ)
1546-
if arg_typ_sym.kind == .none && param.typ.has_flag(.generic) && !param.typ.has_flag(.option) {
1547-
c.error('cannot use `none` as generic argument', call_arg.pos)
1546+
if param.typ.has_flag(.generic) {
1547+
if arg_typ_sym.kind == .none && !param.typ.has_flag(.option) {
1548+
c.error('cannot use `none` as generic argument', call_arg.pos)
1549+
}
1550+
c.check_unresolved_generic_param(node, call_arg)
15481551
}
15491552
param_typ_sym := c.table.sym(param.typ)
15501553
if func.is_variadic && arg_typ.has_flag(.variadic) && args_len - 1 > i {
@@ -2573,6 +2576,7 @@ fn (mut c Checker) method_call(mut node ast.CallExpr, mut continue_check &bool)
25732576
}
25742577
}
25752578
if exp_arg_typ.has_flag(.generic) {
2579+
c.check_unresolved_generic_param(node, arg)
25762580
method_concrete_types := if method_generic_names_len == rec_concrete_types.len {
25772581
rec_concrete_types
25782582
} else {
@@ -2753,6 +2757,13 @@ fn (mut c Checker) handle_generic_lambda_arg(node &ast.CallExpr, mut lambda ast.
27532757
}
27542758
}
27552759

2760+
fn (mut c Checker) check_unresolved_generic_param(node &ast.CallExpr, arg ast.CallArg) {
2761+
if node.raw_concrete_types.len == 0 && arg.expr is ast.ArrayInit
2762+
&& arg.expr.typ == ast.void_type {
2763+
c.error('cannot use empty array as generic argument', arg.pos)
2764+
}
2765+
}
2766+
27562767
fn (mut c Checker) spawn_expr(mut node ast.SpawnExpr) ast.Type {
27572768
ret_type := c.call_expr(mut node.call_expr)
27582769
if node.call_expr.or_block.kind != .absent {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
vlib/v/checker/tests/empty_arr_to_generic_param_err.vv:8:4: error: cannot use empty array as generic argument
2+
6 |
3+
7 | fn main() {
4+
8 | t([])
5+
| ~~
6+
9 | Foo{}.t([])
7+
10 | }
8+
vlib/v/checker/tests/empty_arr_to_generic_param_err.vv:9:10: error: cannot use empty array as generic argument
9+
7 | fn main() {
10+
8 | t([])
11+
9 | Foo{}.t([])
12+
| ~~
13+
10 | }
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
fn t[T](a []T) {}
2+
3+
struct Foo {}
4+
5+
fn (t &Foo) t[T](a []T) {}
6+
7+
fn main() {
8+
t([])
9+
Foo{}.t([])
10+
}

0 commit comments

Comments
 (0)