diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index c5d372d1083299..950a3c21bb0857 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -3568,7 +3568,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as if node.args.len != 1 { c.error('`.contains()` expected 1 argument, but got ${node.args.len}', node.pos) } else if !left_sym.has_method('contains') { - arg_typ := c.expr(mut node.args[0].expr) + arg_typ := c.unwrap_generic(c.expr(mut node.args[0].expr)) c.check_expected_call_arg(arg_typ, c.unwrap_generic(elem_typ), node.language, node.args[0]) or { c.error('${err.msg()} in argument 1 to `.contains()`', node.args[0].pos) @@ -3582,8 +3582,9 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as if node.args.len != 1 { c.error('`.index()` expected 1 argument, but got ${node.args.len}', node.pos) } else if !left_sym.has_method('index') { - arg_typ := c.expr(mut node.args[0].expr) - c.check_expected_call_arg(arg_typ, elem_typ, node.language, node.args[0]) or { + arg_typ := c.unwrap_generic(c.expr(mut node.args[0].expr)) + c.check_expected_call_arg(arg_typ, c.unwrap_generic(elem_typ), node.language, + node.args[0]) or { c.error('${err.msg()} in argument 1 to `.index()`', node.args[0].pos) } } @@ -3625,7 +3626,7 @@ fn (mut c Checker) array_builtin_method_call(mut node ast.CallExpr, left_type as if node.args.len != 1 { c.error('`.delete()` expected 1 argument, but got ${node.args.len}', node.pos) } else { - arg_typ := c.expr(mut node.args[0].expr) + arg_typ := c.unwrap_generic(c.expr(mut node.args[0].expr)) c.check_expected_call_arg(arg_typ, ast.int_type, node.language, node.args[0]) or { c.error('${err.msg()} in argument 1 to `.delete()`', node.args[0].pos) } diff --git a/vlib/v/checker/tests/array_generic_methods_err.out b/vlib/v/checker/tests/array_generic_methods_err.out new file mode 100644 index 00000000000000..1d4108549535db --- /dev/null +++ b/vlib/v/checker/tests/array_generic_methods_err.out @@ -0,0 +1,21 @@ +vlib/v/checker/tests/array_generic_methods_err.vv:3:23: error: cannot use `[]string` as `string` in argument 1 to `.contains()` + 1 | fn generic_fn[T]() { + 2 | mut arr := []T{} + 3 | println(arr.contains(arr)) + | ~~~ + 4 | arr.delete(arr) + 5 | arr.index(arr) +vlib/v/checker/tests/array_generic_methods_err.vv:4:13: error: cannot use `[]string` as `int` in argument 1 to `.delete()` + 2 | mut arr := []T{} + 3 | println(arr.contains(arr)) + 4 | arr.delete(arr) + | ~~~ + 5 | arr.index(arr) + 6 | } +vlib/v/checker/tests/array_generic_methods_err.vv:5:12: error: cannot use `[]string` as `string` in argument 1 to `.index()` + 3 | println(arr.contains(arr)) + 4 | arr.delete(arr) + 5 | arr.index(arr) + | ~~~ + 6 | } + 7 | diff --git a/vlib/v/checker/tests/array_generic_methods_err.vv b/vlib/v/checker/tests/array_generic_methods_err.vv new file mode 100644 index 00000000000000..0736502fd5eb41 --- /dev/null +++ b/vlib/v/checker/tests/array_generic_methods_err.vv @@ -0,0 +1,10 @@ +fn generic_fn[T]() { + mut arr := []T{} + println(arr.contains(arr)) + arr.delete(arr) + arr.index(arr) +} + +fn main() { + generic_fn[string]() +}